Skip to content

Commit c21dd51

Browse files
committed
Auto merge of #13481 - GuillaumeGomez:extern-map-fix, r=weihanglo
Add all unit's children recursively for `doc.extern-map` option Fixes rust-lang/rust#120993. It allows to fix link generation of reexport of foreign items when deps documentation is not generated. Sometimes, we need to have information about not only direct dependencies but also of dependencies of these direct dependencies and so on.
2 parents 5a5ee9a + e4b0aaa commit c21dd51

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

src/cargo/core/compiler/rustdoc.rs

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,63 @@ impl hash::Hash for RustdocExternMap {
102102
}
103103
}
104104

105+
/// Recursively generate html root url for all units and their children.
106+
///
107+
/// This is needed because in case there is a reexport of foreign reexport, you
108+
/// need to have information about grand-children deps level (deps of your deps).
109+
fn build_all_urls(
110+
build_runner: &BuildRunner<'_, '_>,
111+
rustdoc: &mut ProcessBuilder,
112+
unit: &Unit,
113+
name2url: &HashMap<&String, Url>,
114+
map: &RustdocExternMap,
115+
unstable_opts: &mut bool,
116+
) {
117+
for dep in build_runner.unit_deps(unit) {
118+
if !dep.unit.target.is_linkable() || dep.unit.mode.is_doc() {
119+
continue;
120+
}
121+
for (registry, location) in &map.registries {
122+
let sid = dep.unit.pkg.package_id().source_id();
123+
let matches_registry = || -> bool {
124+
if !sid.is_registry() {
125+
return false;
126+
}
127+
if sid.is_crates_io() {
128+
return registry == CRATES_IO_REGISTRY;
129+
}
130+
if let Some(index_url) = name2url.get(registry) {
131+
return index_url == sid.url();
132+
}
133+
false
134+
};
135+
if matches_registry() {
136+
let mut url = location.clone();
137+
if !url.contains("{pkg_name}") && !url.contains("{version}") {
138+
if !url.ends_with('/') {
139+
url.push('/');
140+
}
141+
url.push_str("{pkg_name}/{version}/");
142+
}
143+
let url = url
144+
.replace("{pkg_name}", &dep.unit.pkg.name())
145+
.replace("{version}", &dep.unit.pkg.version().to_string());
146+
rustdoc.arg("--extern-html-root-url");
147+
rustdoc.arg(format!("{}={}", dep.unit.target.crate_name(), url));
148+
*unstable_opts = true;
149+
}
150+
}
151+
build_all_urls(
152+
build_runner,
153+
rustdoc,
154+
&dep.unit,
155+
name2url,
156+
map,
157+
unstable_opts,
158+
);
159+
}
160+
}
161+
105162
/// Adds unstable flag [`--extern-html-root-url`][1] to the given `rustdoc`
106163
/// invocation. This is for unstable feature [`-Zrustdoc-map`][2].
107164
///
@@ -135,40 +192,14 @@ pub fn add_root_urls(
135192
}
136193
})
137194
.collect();
138-
for dep in build_runner.unit_deps(unit) {
139-
if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
140-
for (registry, location) in &map.registries {
141-
let sid = dep.unit.pkg.package_id().source_id();
142-
let matches_registry = || -> bool {
143-
if !sid.is_registry() {
144-
return false;
145-
}
146-
if sid.is_crates_io() {
147-
return registry == CRATES_IO_REGISTRY;
148-
}
149-
if let Some(index_url) = name2url.get(registry) {
150-
return index_url == sid.url();
151-
}
152-
false
153-
};
154-
if matches_registry() {
155-
let mut url = location.clone();
156-
if !url.contains("{pkg_name}") && !url.contains("{version}") {
157-
if !url.ends_with('/') {
158-
url.push('/');
159-
}
160-
url.push_str("{pkg_name}/{version}/");
161-
}
162-
let url = url
163-
.replace("{pkg_name}", &dep.unit.pkg.name())
164-
.replace("{version}", &dep.unit.pkg.version().to_string());
165-
rustdoc.arg("--extern-html-root-url");
166-
rustdoc.arg(format!("{}={}", dep.unit.target.crate_name(), url));
167-
unstable_opts = true;
168-
}
169-
}
170-
}
171-
}
195+
build_all_urls(
196+
build_runner,
197+
rustdoc,
198+
unit,
199+
&name2url,
200+
map,
201+
&mut unstable_opts,
202+
);
172203
let std_url = match &map.std {
173204
None | Some(RustdocExternMode::Remote) => None,
174205
Some(RustdocExternMode::Local) => {

tests/testsuite/rustdoc_extern_html.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,9 @@ fn alt_registry() {
258258
.run();
259259
let queen = p.read_file("target/doc/foo/fn.queen.html");
260260
assert!(queen.contains(r#"href="https://example.com/bar/1.0.0/bar/struct.Queen.html""#));
261-
// The king example fails to link. Rustdoc seems to want the origin crate
262-
// name (baz) for re-exports. There are many issues in the issue tracker
263-
// for rustdoc re-exports, so I'm not sure, but I think this is maybe a
264-
// rustdoc issue. Alternatively, Cargo could provide mappings for all
265-
// transitive dependencies to fix this.
261+
266262
let king = p.read_file("target/doc/foo/fn.king.html");
267-
assert!(king.contains(r#"-&gt; King"#));
263+
assert!(king.contains(r#"href="https://example.com/baz/1.0.0/baz/struct.King.html""#));
268264

269265
let gold = p.read_file("target/doc/foo/fn.gold.html");
270266
assert!(gold.contains(r#"href="https://docs.rs/grimm/1.0.0/grimm/struct.Gold.html""#));
@@ -413,13 +409,9 @@ fn alt_sparse_registry() {
413409
.run();
414410
let queen = p.read_file("target/doc/foo/fn.queen.html");
415411
assert!(queen.contains(r#"href="https://example.com/bar/1.0.0/bar/struct.Queen.html""#));
416-
// The king example fails to link. Rustdoc seems to want the origin crate
417-
// name (baz) for re-exports. There are many issues in the issue tracker
418-
// for rustdoc re-exports, so I'm not sure, but I think this is maybe a
419-
// rustdoc issue. Alternatively, Cargo could provide mappings for all
420-
// transitive dependencies to fix this.
412+
421413
let king = p.read_file("target/doc/foo/fn.king.html");
422-
assert!(king.contains(r#"-&gt; King"#));
414+
assert!(king.contains(r#"href="https://example.com/baz/1.0.0/baz/struct.King.html""#));
423415

424416
let gold = p.read_file("target/doc/foo/fn.gold.html");
425417
assert!(gold.contains(r#"href="https://docs.rs/grimm/1.0.0/grimm/struct.Gold.html""#));

0 commit comments

Comments
 (0)