Skip to content

Commit e50292a

Browse files
committed
Fix doc duplicate removal of root units.
1 parent 1e47626 commit e50292a

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

src/cargo/ops/cargo_compile.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1558,8 +1558,11 @@ fn remove_duplicate_doc(
15581558
// Keep track of units to remove so that they can be efficiently removed
15591559
// from the unit_deps.
15601560
let mut removed_units: HashSet<Unit> = HashSet::new();
1561-
let mut remove = |units: Vec<Unit>, reason: &str| {
1562-
for unit in units {
1561+
let mut remove = |units: Vec<Unit>, reason: &str, cb: &dyn Fn(&Unit) -> bool| -> Vec<Unit> {
1562+
let (to_remove, remaining_units): (Vec<Unit>, Vec<Unit>) = units
1563+
.into_iter()
1564+
.partition(|unit| cb(unit) && !root_units.contains(unit));
1565+
for unit in to_remove {
15631566
log::debug!(
15641567
"removing duplicate doc due to {} for package {} target `{}`",
15651568
reason,
@@ -1569,6 +1572,7 @@ fn remove_duplicate_doc(
15691572
unit_graph.remove(&unit);
15701573
removed_units.insert(unit);
15711574
}
1575+
remaining_units
15721576
};
15731577
// Iterate over the duplicates and try to remove them from unit_graph.
15741578
for (_crate_name, mut units) in all_docs {
@@ -1581,14 +1585,11 @@ fn remove_duplicate_doc(
15811585
.iter()
15821586
.all(CompileKind::is_host)
15831587
{
1584-
let (to_remove, remaining_units): (Vec<Unit>, Vec<Unit>) =
1585-
units.into_iter().partition(|unit| unit.kind.is_host());
15861588
// Note these duplicates may not be real duplicates, since they
15871589
// might get merged in rebuild_unit_graph_shared. Either way, it
15881590
// shouldn't hurt to remove them early (although the report in the
15891591
// log might be confusing).
1590-
remove(to_remove, "host/target merger");
1591-
units = remaining_units;
1592+
units = remove(units, "host/target merger", &|unit| unit.kind.is_host());
15921593
if units.len() == 1 {
15931594
continue;
15941595
}
@@ -1610,10 +1611,9 @@ fn remove_duplicate_doc(
16101611
units.sort_by(|a, b| a.pkg.version().partial_cmp(b.pkg.version()).unwrap());
16111612
// Remove any entries with version < newest.
16121613
let newest_version = units.last().unwrap().pkg.version().clone();
1613-
let (to_remove, keep_units): (Vec<Unit>, Vec<Unit>) = units
1614-
.into_iter()
1615-
.partition(|unit| unit.pkg.version() < &newest_version);
1616-
remove(to_remove, "older version");
1614+
let keep_units = remove(units, "older version", &|unit| {
1615+
unit.pkg.version() < &newest_version
1616+
});
16171617
remaining_units.extend(keep_units);
16181618
} else {
16191619
remaining_units.extend(units);

tests/testsuite/collisions.rs

+66
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,69 @@ fn collision_doc_target() {
478478
)
479479
.run();
480480
}
481+
482+
#[cargo_test]
483+
fn collision_with_root() {
484+
// Check for a doc collision between a root package and a dependency.
485+
// In this case, `foo-macro` comes from both the workspace and crates.io.
486+
// This checks that the duplicate correction code doesn't choke on this
487+
// by removing the root unit.
488+
Package::new("foo-macro", "1.0.0").publish();
489+
490+
let p = project()
491+
.file(
492+
"Cargo.toml",
493+
r#"
494+
[workspace]
495+
members = ["abc", "foo-macro"]
496+
"#,
497+
)
498+
.file(
499+
"abc/Cargo.toml",
500+
r#"
501+
[package]
502+
name = "abc"
503+
version = "1.0.0"
504+
505+
[dependencies]
506+
foo-macro = "1.0"
507+
"#,
508+
)
509+
.file("abc/src/lib.rs", "")
510+
.file(
511+
"foo-macro/Cargo.toml",
512+
r#"
513+
[package]
514+
name = "foo-macro"
515+
version = "1.0.0"
516+
517+
[lib]
518+
proc-macro = true
519+
520+
[dependencies]
521+
abc = {path="../abc"}
522+
"#,
523+
)
524+
.file("foo-macro/src/lib.rs", "")
525+
.build();
526+
527+
p.cargo("doc")
528+
.with_stderr_unordered("\
529+
[UPDATING] [..]
530+
[DOWNLOADING] crates ...
531+
[DOWNLOADED] foo-macro v1.0.0 [..]
532+
warning: output filename collision.
533+
The lib target `foo-macro` in package `foo-macro v1.0.0` has the same output filename as the lib target `foo-macro` in package `foo-macro v1.0.0 [..]`.
534+
Colliding filename is: [CWD]/target/doc/foo_macro/index.html
535+
The targets should have unique names.
536+
This is a known bug where multiple crates with the same name use
537+
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
538+
[CHECKING] foo-macro v1.0.0
539+
[DOCUMENTING] foo-macro v1.0.0
540+
[CHECKING] abc v1.0.0 [..]
541+
[DOCUMENTING] foo-macro v1.0.0 [..]
542+
[DOCUMENTING] abc v1.0.0 [..]
543+
[FINISHED] [..]
544+
")
545+
.run();
546+
}

0 commit comments

Comments
 (0)