Skip to content

Commit d9f63cd

Browse files
committed
Set docs.rs as the default extern-map for crates.io
1 parent 9051345 commit d9f63cd

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

src/cargo/core/compiler/rustdoc.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'de> serde::de::Deserialize<'de> for RustdocExternMode {
5555
#[derive(serde::Deserialize, Debug, Default)]
5656
#[serde(default)]
5757
pub struct RustdocExternMap {
58-
registries: HashMap<String, String>,
58+
pub(crate) registries: HashMap<String, String>,
5959
std: Option<RustdocExternMode>,
6060
}
6161

@@ -80,10 +80,6 @@ pub fn add_root_urls(
8080
return Ok(());
8181
}
8282
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-
}
8783
let mut unstable_opts = false;
8884
// Collect mapping of registry name -> index url.
8985
let name2url: HashMap<&String, Url> = map

src/cargo/util/config/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,15 @@ impl Config {
12171217
// Note: This does not support environment variables. The `Unit`
12181218
// fundamentally does not have access to the registry name, so there is
12191219
// nothing to query. Plumbing the name into SourceId is quite challenging.
1220-
self.doc_extern_map
1221-
.try_borrow_with(|| self.get::<RustdocExternMap>("doc.extern-map"))
1220+
self.doc_extern_map.try_borrow_with(|| {
1221+
let mut extern_map = self.get::<RustdocExternMap>("doc.extern-map");
1222+
if let Ok(map) = &mut extern_map {
1223+
map.registries
1224+
.entry("crates-io".into())
1225+
.or_insert("https://docs.rs/".into());
1226+
}
1227+
extern_map
1228+
})
12221229
}
12231230

12241231
/// Returns the `[target]` table definition for the given target triple.

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)