diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index 37e70fde0b4..7cab50a7070 100644 --- a/src/cargo/core/compiler/rustdoc.rs +++ b/src/cargo/core/compiler/rustdoc.rs @@ -52,13 +52,36 @@ impl<'de> serde::de::Deserialize<'de> for RustdocExternMode { } } -#[derive(serde::Deserialize, Debug, Default)] +#[derive(serde::Deserialize, Debug)] #[serde(default)] pub struct RustdocExternMap { - registries: HashMap, + #[serde(deserialize_with = "default_crates_io_to_docs_rs")] + pub(crate) registries: HashMap, std: Option, } +impl Default for RustdocExternMap { + fn default() -> Self { + let mut registries = HashMap::new(); + registries.insert("crates-io".into(), "https://docs.rs/".into()); + Self { + registries, + std: None, + } + } +} + +fn default_crates_io_to_docs_rs<'de, D: serde::Deserializer<'de>>( + de: D, +) -> Result, D::Error> { + use serde::Deserialize; + let mut registries = HashMap::deserialize(de)?; + if !registries.contains_key("crates-io") { + registries.insert("crates-io".into(), "https://docs.rs/".into()); + } + Ok(registries) +} + impl hash::Hash for RustdocExternMap { fn hash(&self, into: &mut H) { self.std.hash(into); @@ -80,10 +103,6 @@ pub fn add_root_urls( return Ok(()); } let map = config.doc_extern_map()?; - if map.registries.is_empty() && map.std.is_none() { - // Skip doing unnecessary work. - return Ok(()); - } let mut unstable_opts = false; // Collect mapping of registry name -> index url. let name2url: HashMap<&String, Url> = map diff --git a/tests/testsuite/rustdoc_extern_html.rs b/tests/testsuite/rustdoc_extern_html.rs index 368794785fd..b5b970320dd 100644 --- a/tests/testsuite/rustdoc_extern_html.rs +++ b/tests/testsuite/rustdoc_extern_html.rs @@ -32,21 +32,10 @@ fn basic_project() -> Project { .build() } -fn docs_rs(p: &Project) { - p.change_file( - ".cargo/config", - r#" - [doc.extern-map.registries] - crates-io = "https://docs.rs/" - "#, - ); -} - #[cargo_test] fn ignores_on_stable() { // Requires -Zrustdoc-map to use. let p = basic_project(); - docs_rs(&p); p.cargo("doc -v --no-deps") .with_stderr_does_not_contain("[..]--extern-html-root-url[..]") .run(); @@ -60,7 +49,6 @@ fn simple() { return; } let p = basic_project(); - docs_rs(&p); p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() .with_stderr_contains( @@ -157,7 +145,6 @@ fn renamed_dep() { "#, ) .build(); - docs_rs(&p); p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() .with_stderr_contains( @@ -211,7 +198,6 @@ fn lib_name() { "#, ) .build(); - docs_rs(&p); p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() .with_stderr_contains( @@ -338,7 +324,6 @@ fn multiple_versions() { ", ) .build(); - docs_rs(&p); p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() .with_stderr_contains( @@ -364,12 +349,21 @@ fn rebuilds_when_changing() { let p = basic_project(); p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() - .with_stderr_does_not_contain("[..]--extern-html-root-url[..]") + .with_stderr_contains("[..]--extern-html-root-url[..]") .run(); - docs_rs(&p); + // This also tests that the map for docs.rs can be overridden. + p.change_file( + ".cargo/config", + r#" + [doc.extern-map.registries] + crates-io = "https://example.com/" + "#, + ); p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() - .with_stderr_contains("[..]--extern-html-root-url[..]") + .with_stderr_contains( + "[RUNNING] `rustdoc [..]--extern-html-root-url [..]bar=https://example.com/bar/1.0.0/[..]", + ) .run(); }