From 9e7d6e31229bc77674c1408948de8383f562e706 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 25 Mar 2024 13:32:15 -0700 Subject: [PATCH 1/2] Add test for doc lib/bin collision with a dash. --- tests/testsuite/doc.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 40ad5f45108..98cee9c2fbc 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -478,6 +478,42 @@ fn doc_lib_bin_same_name_documents_lib_when_requested() { assert!(!doc_html.contains("Binary")); } +#[cargo_test] +fn doc_lib_bin_same_name_with_dash() { + // Checks `doc` behavior when there is a dash in the package name, and + // there is a lib and bin, and the lib name is inferred. + let p = project() + .file("Cargo.toml", &basic_manifest("foo-bar", "1.0.0")) + .file("src/lib.rs", "") + .file("src/main.rs", "fn main() {}") + .build(); + p.cargo("doc") + .with_stderr_unordered( + "\ +warning: output filename collision. +The bin target `foo-bar` in package `foo-bar v1.0.0 ([ROOT]/foo)` has the same \ +output filename as the lib target `foo_bar` in package `foo-bar v1.0.0 ([ROOT]/foo)`. +Colliding filename is: [ROOT]/foo/target/doc/foo_bar/index.html +The output filenames should be unique. +This is a known bug where multiple crates with the same name use +the same path; see . +If this looks unexpected, it may be a bug in Cargo. Please file a bug report at +https://github.com/rust-lang/cargo/issues/ with as much information as you +can provide. +cargo [..] +First unit: [..] +Second unit: [..] +[CHECKING] foo-bar v1.0.0 ([ROOT]/foo) +[DOCUMENTING] foo-bar v1.0.0 ([ROOT]/foo) +[FINISHED] [..] +[GENERATED] [ROOT]/foo/target/doc/foo_bar/index.html and 1 other file +", + ) + .run(); + assert!(p.build_dir().join("doc/foo_bar/index.html").exists()); + assert!(p.build_dir().join("doc/foo_bar/fn.main.html").exists()); +} + #[cargo_test] fn doc_lib_bin_same_name_documents_named_bin_when_requested() { let p = project() From 3adb796e873503ea9b52c2ebc97eab79ce634d9c Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 25 Mar 2024 13:35:52 -0700 Subject: [PATCH 2/2] Fix doc collision for lib/bin with a dash in the inferred name. --- src/cargo/ops/cargo_compile/unit_generator.rs | 4 +++- tests/testsuite/doc.rs | 20 +++---------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index 18bbab12dc1..b2d86b7531c 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -198,7 +198,9 @@ impl<'a> UnitGenerator<'a, '_> { .filter(|t| { t.documented() && (!t.is_bin() - || !targets.iter().any(|l| l.is_lib() && l.name() == t.name())) + || !targets + .iter() + .any(|l| l.is_lib() && l.crate_name() == t.crate_name())) }) .collect() } diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 98cee9c2fbc..a809f306cfe 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -488,30 +488,16 @@ fn doc_lib_bin_same_name_with_dash() { .file("src/main.rs", "fn main() {}") .build(); p.cargo("doc") - .with_stderr_unordered( + .with_stderr( "\ -warning: output filename collision. -The bin target `foo-bar` in package `foo-bar v1.0.0 ([ROOT]/foo)` has the same \ -output filename as the lib target `foo_bar` in package `foo-bar v1.0.0 ([ROOT]/foo)`. -Colliding filename is: [ROOT]/foo/target/doc/foo_bar/index.html -The output filenames should be unique. -This is a known bug where multiple crates with the same name use -the same path; see . -If this looks unexpected, it may be a bug in Cargo. Please file a bug report at -https://github.com/rust-lang/cargo/issues/ with as much information as you -can provide. -cargo [..] -First unit: [..] -Second unit: [..] -[CHECKING] foo-bar v1.0.0 ([ROOT]/foo) [DOCUMENTING] foo-bar v1.0.0 ([ROOT]/foo) [FINISHED] [..] -[GENERATED] [ROOT]/foo/target/doc/foo_bar/index.html and 1 other file +[GENERATED] [ROOT]/foo/target/doc/foo_bar/index.html ", ) .run(); assert!(p.build_dir().join("doc/foo_bar/index.html").exists()); - assert!(p.build_dir().join("doc/foo_bar/fn.main.html").exists()); + assert!(!p.build_dir().join("doc/foo_bar/fn.main.html").exists()); } #[cargo_test]