Skip to content

Set docs.rs as the default extern-map for crates.io #8877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}