Skip to content

Commit

Permalink
Set docs.rs as the default extern-map for crates.io
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Nov 22, 2020
1 parent 9051345 commit d9f63cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
6 changes: 1 addition & 5 deletions src/cargo/core/compiler/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'de> serde::de::Deserialize<'de> for RustdocExternMode {
#[derive(serde::Deserialize, Debug, Default)]
#[serde(default)]
pub struct RustdocExternMap {
registries: HashMap<String, String>,
pub(crate) registries: HashMap<String, String>,
std: Option<RustdocExternMode>,
}

Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RustdocExternMap>("doc.extern-map"))
self.doc_extern_map.try_borrow_with(|| {
let mut extern_map = self.get::<RustdocExternMap>("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.
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 d9f63cd

Please sign in to comment.