Skip to content

Commit 6de98d1

Browse files
committed
Auto merge of #8877 - jyn514:rustdoc-map, r=alexcrichton
Set docs.rs as the default extern-map for crates.io Helps address #8296, specifically the bit needed for rust-lang/docs.rs#1177. r? `@ehuss`
2 parents 140fbc9 + 8a48c87 commit 6de98d1

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/cargo/core/compiler/rustdoc.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,36 @@ impl<'de> serde::de::Deserialize<'de> for RustdocExternMode {
5252
}
5353
}
5454

55-
#[derive(serde::Deserialize, Debug, Default)]
55+
#[derive(serde::Deserialize, Debug)]
5656
#[serde(default)]
5757
pub struct RustdocExternMap {
58-
registries: HashMap<String, String>,
58+
#[serde(deserialize_with = "default_crates_io_to_docs_rs")]
59+
pub(crate) registries: HashMap<String, String>,
5960
std: Option<RustdocExternMode>,
6061
}
6162

63+
impl Default for RustdocExternMap {
64+
fn default() -> Self {
65+
let mut registries = HashMap::new();
66+
registries.insert("crates-io".into(), "https://docs.rs/".into());
67+
Self {
68+
registries,
69+
std: None,
70+
}
71+
}
72+
}
73+
74+
fn default_crates_io_to_docs_rs<'de, D: serde::Deserializer<'de>>(
75+
de: D,
76+
) -> Result<HashMap<String, String>, D::Error> {
77+
use serde::Deserialize;
78+
let mut registries = HashMap::deserialize(de)?;
79+
if !registries.contains_key("crates-io") {
80+
registries.insert("crates-io".into(), "https://docs.rs/".into());
81+
}
82+
Ok(registries)
83+
}
84+
6285
impl hash::Hash for RustdocExternMap {
6386
fn hash<H: hash::Hasher>(&self, into: &mut H) {
6487
self.std.hash(into);
@@ -80,10 +103,6 @@ pub fn add_root_urls(
80103
return Ok(());
81104
}
82105
let map = config.doc_extern_map()?;
83-
if map.registries.is_empty() && map.std.is_none() {
84-
// Skip doing unnecessary work.
85-
return Ok(());
86-
}
87106
let mut unstable_opts = false;
88107
// Collect mapping of registry name -> index url.
89108
let name2url: HashMap<&String, Url> = map

tests/testsuite/rustdoc_extern_html.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,10 @@ fn basic_project() -> Project {
3232
.build()
3333
}
3434

35-
fn docs_rs(p: &Project) {
36-
p.change_file(
37-
".cargo/config",
38-
r#"
39-
[doc.extern-map.registries]
40-
crates-io = "https://docs.rs/"
41-
"#,
42-
);
43-
}
44-
4535
#[cargo_test]
4636
fn ignores_on_stable() {
4737
// Requires -Zrustdoc-map to use.
4838
let p = basic_project();
49-
docs_rs(&p);
5039
p.cargo("doc -v --no-deps")
5140
.with_stderr_does_not_contain("[..]--extern-html-root-url[..]")
5241
.run();
@@ -60,7 +49,6 @@ fn simple() {
6049
return;
6150
}
6251
let p = basic_project();
63-
docs_rs(&p);
6452
p.cargo("doc -v --no-deps -Zrustdoc-map")
6553
.masquerade_as_nightly_cargo()
6654
.with_stderr_contains(
@@ -157,7 +145,6 @@ fn renamed_dep() {
157145
"#,
158146
)
159147
.build();
160-
docs_rs(&p);
161148
p.cargo("doc -v --no-deps -Zrustdoc-map")
162149
.masquerade_as_nightly_cargo()
163150
.with_stderr_contains(
@@ -211,7 +198,6 @@ fn lib_name() {
211198
"#,
212199
)
213200
.build();
214-
docs_rs(&p);
215201
p.cargo("doc -v --no-deps -Zrustdoc-map")
216202
.masquerade_as_nightly_cargo()
217203
.with_stderr_contains(
@@ -338,7 +324,6 @@ fn multiple_versions() {
338324
",
339325
)
340326
.build();
341-
docs_rs(&p);
342327
p.cargo("doc -v --no-deps -Zrustdoc-map")
343328
.masquerade_as_nightly_cargo()
344329
.with_stderr_contains(
@@ -364,12 +349,21 @@ fn rebuilds_when_changing() {
364349
let p = basic_project();
365350
p.cargo("doc -v --no-deps -Zrustdoc-map")
366351
.masquerade_as_nightly_cargo()
367-
.with_stderr_does_not_contain("[..]--extern-html-root-url[..]")
352+
.with_stderr_contains("[..]--extern-html-root-url[..]")
368353
.run();
369354

370-
docs_rs(&p);
355+
// This also tests that the map for docs.rs can be overridden.
356+
p.change_file(
357+
".cargo/config",
358+
r#"
359+
[doc.extern-map.registries]
360+
crates-io = "https://example.com/"
361+
"#,
362+
);
371363
p.cargo("doc -v --no-deps -Zrustdoc-map")
372364
.masquerade_as_nightly_cargo()
373-
.with_stderr_contains("[..]--extern-html-root-url[..]")
365+
.with_stderr_contains(
366+
"[RUNNING] `rustdoc [..]--extern-html-root-url [..]bar=https://example.com/bar/1.0.0/[..]",
367+
)
374368
.run();
375369
}

0 commit comments

Comments
 (0)