Skip to content

Commit

Permalink
Auto merge of #8877 - jyn514:rustdoc-map, r=alexcrichton
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Dec 1, 2020
2 parents 140fbc9 + 8a48c87 commit 6de98d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
31 changes: 25 additions & 6 deletions src/cargo/core/compiler/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>,
#[serde(deserialize_with = "default_crates_io_to_docs_rs")]
pub(crate) registries: HashMap<String, String>,
std: Option<RustdocExternMode>,
}

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<HashMap<String, String>, 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<H: hash::Hasher>(&self, into: &mut H) {
self.std.hash(into);
Expand All @@ -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
Expand Down
30 changes: 12 additions & 18 deletions tests/testsuite/rustdoc_extern_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}

0 comments on commit 6de98d1

Please sign in to comment.