From d9f63cd684db457ec7a18c70c5de8c976707cd6a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 21 Nov 2020 21:05:47 -0500 Subject: [PATCH 1/3] Set `docs.rs` as the default extern-map for crates.io --- src/cargo/core/compiler/rustdoc.rs | 6 +----- src/cargo/util/config/mod.rs | 11 ++++++++-- tests/testsuite/rustdoc_extern_html.rs | 30 +++++++++++--------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index 37e70fde0b4..18c6898ee27 100644 --- a/src/cargo/core/compiler/rustdoc.rs +++ b/src/cargo/core/compiler/rustdoc.rs @@ -55,7 +55,7 @@ impl<'de> serde::de::Deserialize<'de> for RustdocExternMode { #[derive(serde::Deserialize, Debug, Default)] #[serde(default)] pub struct RustdocExternMap { - registries: HashMap, + pub(crate) registries: HashMap, std: Option, } @@ -80,10 +80,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/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 9a3c5b0df43..24ac86e8312 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1217,8 +1217,15 @@ impl Config { // Note: This does not support environment variables. The `Unit` // fundamentally does not have access to the registry name, so there is // nothing to query. Plumbing the name into SourceId is quite challenging. - self.doc_extern_map - .try_borrow_with(|| self.get::("doc.extern-map")) + self.doc_extern_map.try_borrow_with(|| { + let mut extern_map = self.get::("doc.extern-map"); + if let Ok(map) = &mut extern_map { + map.registries + .entry("crates-io".into()) + .or_insert("https://docs.rs/".into()); + } + extern_map + }) } /// Returns the `[target]` table definition for the given target triple. diff --git a/tests/testsuite/rustdoc_extern_html.rs b/tests/testsuite/rustdoc_extern_html.rs index 368794785fd..0542be772aa 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(); } From d85fd93329b17cc746ee157ceed6866649a25dea Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 21 Nov 2020 22:51:30 -0500 Subject: [PATCH 2/3] Fix inconsistency with quoting Locally, the output looks like `'bar=...'`, but in CI the quotes are missing. Allow either. --- tests/testsuite/rustdoc_extern_html.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/rustdoc_extern_html.rs b/tests/testsuite/rustdoc_extern_html.rs index 0542be772aa..b5b970320dd 100644 --- a/tests/testsuite/rustdoc_extern_html.rs +++ b/tests/testsuite/rustdoc_extern_html.rs @@ -363,7 +363,7 @@ fn rebuilds_when_changing() { p.cargo("doc -v --no-deps -Zrustdoc-map") .masquerade_as_nightly_cargo() .with_stderr_contains( - "[RUNNING] `rustdoc [..]--extern-html-root-url 'bar=https://example.com/bar/1.0.0/[..]' [..]", + "[RUNNING] `rustdoc [..]--extern-html-root-url [..]bar=https://example.com/bar/1.0.0/[..]", ) .run(); } From 8a48c872bc4d77fbd858ea4dd4494daad245730c Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 26 Nov 2020 00:46:30 -0500 Subject: [PATCH 3/3] Use `deserialize_with` instead of specializing `doc_extern_map` --- src/cargo/core/compiler/rustdoc.rs | 25 ++++++++++++++++++++++++- src/cargo/util/config/mod.rs | 11 ++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index 18c6898ee27..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 { + #[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); diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 24ac86e8312..9a3c5b0df43 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1217,15 +1217,8 @@ impl Config { // Note: This does not support environment variables. The `Unit` // fundamentally does not have access to the registry name, so there is // nothing to query. Plumbing the name into SourceId is quite challenging. - self.doc_extern_map.try_borrow_with(|| { - let mut extern_map = self.get::("doc.extern-map"); - if let Ok(map) = &mut extern_map { - map.registries - .entry("crates-io".into()) - .or_insert("https://docs.rs/".into()); - } - extern_map - }) + self.doc_extern_map + .try_borrow_with(|| self.get::("doc.extern-map")) } /// Returns the `[target]` table definition for the given target triple.