Skip to content

Commit bc38953

Browse files
committed
Port the output-filename-collision message
1 parent fb571ec commit bc38953

File tree

3 files changed

+95
-108
lines changed

3 files changed

+95
-108
lines changed

src/cargo/core/compiler/build_runner/mod.rs

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::core::compiler::compilation::{self, UnitOutput};
99
use crate::core::compiler::{self, Unit, artifact};
1010
use crate::util::cache_lock::CacheLockMode;
1111
use crate::util::errors::CargoResult;
12+
use annotate_snippets::{Level, Message};
1213
use anyhow::{Context as _, bail};
1314
use cargo_util::paths;
1415
use filetime::FileTime;
@@ -515,58 +516,56 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
515516
#[tracing::instrument(skip_all)]
516517
fn check_collisions(&self) -> CargoResult<()> {
517518
let mut output_collisions = HashMap::new();
518-
let describe_collision = |unit: &Unit, other_unit: &Unit, path: &PathBuf| -> String {
519+
let describe_collision = |unit: &Unit, other_unit: &Unit| -> String {
519520
format!(
520-
"The {} target `{}` in package `{}` has the same output \
521-
filename as the {} target `{}` in package `{}`.\n\
522-
Colliding filename is: {}\n",
521+
"the {} target `{}` in package `{}` has the same output filename as the {} target `{}` in package `{}`",
523522
unit.target.kind().description(),
524523
unit.target.name(),
525524
unit.pkg.package_id(),
526525
other_unit.target.kind().description(),
527526
other_unit.target.name(),
528527
other_unit.pkg.package_id(),
529-
path.display()
530528
)
531529
};
532-
let suggestion = "Consider changing their names to be unique or compiling them separately.\n\
533-
This may become a hard error in the future; see \
534-
<https://github.com/rust-lang/cargo/issues/6313>.";
535-
let rustdoc_suggestion = "This is a known bug where multiple crates with the same name use\n\
536-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.";
530+
let suggestion = [
531+
Level::NOTE.message("this may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>"),
532+
Level::HELP.message("consider changing their names to be unique or compiling them separately")
533+
];
534+
let rustdoc_suggestion = [
535+
Level::NOTE.message("this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>")
536+
];
537537
let report_collision = |unit: &Unit,
538538
other_unit: &Unit,
539539
path: &PathBuf,
540-
suggestion: &str|
540+
messages: &[Message<'_>]|
541541
-> CargoResult<()> {
542542
if unit.target.name() == other_unit.target.name() {
543-
self.bcx.gctx.shell().warn(format!(
544-
"output filename collision.\n\
545-
{}\
546-
The targets should have unique names.\n\
547-
{}",
548-
describe_collision(unit, other_unit, path),
549-
suggestion
550-
))
543+
self.bcx.gctx.shell().print_report(
544+
&[Level::WARNING
545+
.secondary_title(format!("output filename collision at {}", path.display()))
546+
.elements(
547+
[Level::NOTE.message(describe_collision(unit, other_unit))]
548+
.into_iter()
549+
.chain(messages.iter().cloned()),
550+
)],
551+
false,
552+
)
551553
} else {
552-
self.bcx.gctx.shell().warn(format!(
553-
"output filename collision.\n\
554-
{}\
555-
The output filenames should be unique.\n\
556-
{}\n\
557-
If this looks unexpected, it may be a bug in Cargo. Please file a bug report at\n\
558-
https://github.com/rust-lang/cargo/issues/ with as much information as you\n\
559-
can provide.\n\
560-
cargo {} running on `{}` target `{}`\n\
561-
First unit: {:?}\n\
562-
Second unit: {:?}",
563-
describe_collision(unit, other_unit, path),
564-
suggestion,
565-
crate::version(),
566-
self.bcx.host_triple(),
567-
self.bcx.target_data.short_name(&unit.kind),
568-
unit,
569-
other_unit))
554+
self.bcx.gctx.shell().print_report(
555+
&[Level::WARNING
556+
.secondary_title(format!("output filename collision at {}", path.display()))
557+
.elements([
558+
Level::NOTE.message(describe_collision(unit, other_unit)),
559+
Level::NOTE.message("if this looks unexpected, it may be a bug in Cargo. Please file a bug \
560+
report at https://github.com/rust-lang/cargo/issues/ with as much information as you \
561+
can provide."),
562+
Level::NOTE.message(format!("cargo {} running on `{}` target `{}`",
563+
crate::version(), self.bcx.host_triple(), self.bcx.target_data.short_name(&unit.kind))),
564+
Level::NOTE.message(format!("first unit: {unit:?}")),
565+
Level::NOTE.message(format!("second unit: {other_unit:?}")),
566+
])],
567+
false,
568+
)
570569
}
571570
};
572571

@@ -623,26 +622,31 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
623622
if unit.mode.is_doc() {
624623
// See https://github.com/rust-lang/rust/issues/56169
625624
// and https://github.com/rust-lang/rust/issues/61378
626-
report_collision(unit, other_unit, &output.path, rustdoc_suggestion)?;
625+
report_collision(unit, other_unit, &output.path, &rustdoc_suggestion)?;
627626
} else {
628-
report_collision(unit, other_unit, &output.path, suggestion)?;
627+
report_collision(unit, other_unit, &output.path, &suggestion)?;
629628
}
630629
}
631630
if let Some(hardlink) = output.hardlink.as_ref() {
632631
if let Some(other_unit) = output_collisions.insert(hardlink.clone(), unit) {
633-
report_collision(unit, other_unit, hardlink, suggestion)?;
632+
report_collision(unit, other_unit, hardlink, &suggestion)?;
634633
}
635634
}
636635
if let Some(ref export_path) = output.export_path {
637636
if let Some(other_unit) = output_collisions.insert(export_path.clone(), unit) {
638-
self.bcx.gctx.shell().warn(format!(
639-
"`--artifact-dir` filename collision.\n\
640-
{}\
641-
The exported filenames should be unique.\n\
642-
{}",
643-
describe_collision(unit, other_unit, export_path),
644-
suggestion
645-
))?;
637+
self.bcx.gctx.shell().print_report(
638+
&[Level::WARNING
639+
.secondary_title(format!(
640+
"`--artifact-dir` filename collision at {}",
641+
export_path.display()
642+
))
643+
.elements(
644+
[Level::NOTE.message(describe_collision(unit, other_unit))]
645+
.into_iter()
646+
.chain(suggestion.iter().cloned()),
647+
)],
648+
false,
649+
)?;
646650
}
647651
}
648652
}

tests/testsuite/collisions.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ fn collision_dylib() {
5555
p.cargo("build -j=1")
5656
.with_stderr_data(&format!("\
5757
...
58-
[WARNING] output filename collision.
59-
The lib target `a` in package `b v1.0.0 ([ROOT]/foo/b)` has the same output filename as the lib target `a` in package `a v1.0.0 ([ROOT]/foo/a)`.
60-
Colliding filename is: [ROOT]/foo/target/debug/deps/{}a{}
61-
The targets should have unique names.
62-
Consider changing their names to be unique or compiling them separately.
63-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
58+
[WARNING] output filename collision at [ROOT]/foo/target/debug/deps/{}a{}
59+
|
60+
= [NOTE] the lib target `a` in package `b v1.0.0 ([ROOT]/foo/b)` has the same output filename as the lib target `a` in package `a v1.0.0 ([ROOT]/foo/a)`
61+
= [NOTE] this may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>
62+
= [HELP] consider changing their names to be unique or compiling them separately
6463
...
6564
", env::consts::DLL_PREFIX, env::consts::DLL_SUFFIX))
6665
.run();
@@ -88,12 +87,11 @@ fn collision_example() {
8887
p.cargo("build --examples -j=1")
8988
.with_stderr_data(str![[r#"
9089
...
91-
[WARNING] output filename collision.
92-
The example target `ex1` in package `b v1.0.0 ([ROOT]/foo/b)` has the same output filename as the example target `ex1` in package `a v1.0.0 ([ROOT]/foo/a)`.
93-
Colliding filename is: [ROOT]/foo/target/debug/examples/ex1[EXE]
94-
The targets should have unique names.
95-
Consider changing their names to be unique or compiling them separately.
96-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
90+
[WARNING] output filename collision at [ROOT]/foo/target/debug/examples/ex1[EXE]
91+
|
92+
= [NOTE] the example target `ex1` in package `b v1.0.0 ([ROOT]/foo/b)` has the same output filename as the example target `ex1` in package `a v1.0.0 ([ROOT]/foo/a)`
93+
= [NOTE] this may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>
94+
= [HELP] consider changing their names to be unique or compiling them separately
9795
...
9896
9997
"#]])
@@ -119,12 +117,11 @@ fn collision_export() {
119117
p.cargo("build -j1 --artifact-dir=out -Z unstable-options --bins --examples")
120118
.masquerade_as_nightly_cargo(&["artifact-dir"])
121119
.with_stderr_data(str![[r#"
122-
[WARNING] `--artifact-dir` filename collision.
123-
The example target `foo` in package `foo v1.0.0 ([ROOT]/foo)` has the same output filename as the bin target `foo` in package `foo v1.0.0 ([ROOT]/foo)`.
124-
Colliding filename is: [ROOT]/foo/out/foo[EXE]
125-
The exported filenames should be unique.
126-
Consider changing their names to be unique or compiling them separately.
127-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
120+
[WARNING] `--artifact-dir` filename collision at [ROOT]/foo/out/foo[EXE]
121+
|
122+
= [NOTE] the example target `foo` in package `foo v1.0.0 ([ROOT]/foo)` has the same output filename as the bin target `foo` in package `foo v1.0.0 ([ROOT]/foo)`
123+
= [NOTE] this may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>
124+
= [HELP] consider changing their names to be unique or compiling them separately
128125
...
129126
130127
"#]])
@@ -165,12 +162,10 @@ fn collision_doc() {
165162
p.cargo("doc -j=1")
166163
.with_stderr_data(str![[r#"
167164
...
168-
[WARNING] output filename collision.
169-
The lib target `foo` in package `foo2 v0.1.0 ([ROOT]/foo/foo2)` has the same output filename as the lib target `foo` in package `foo v0.1.0 ([ROOT]/foo)`.
170-
Colliding filename is: [ROOT]/foo/target/doc/foo/index.html
171-
The targets should have unique names.
172-
This is a known bug where multiple crates with the same name use
173-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
165+
[WARNING] output filename collision at [ROOT]/foo/target/doc/foo/index.html
166+
|
167+
= [NOTE] the lib target `foo` in package `foo2 v0.1.0 ([ROOT]/foo/foo2)` has the same output filename as the lib target `foo` in package `foo v0.1.0 ([ROOT]/foo)`
168+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
174169
...
175170
176171
"#]])
@@ -449,12 +444,10 @@ fn collision_doc_sources() {
449444
[LOCKING] 2 packages to latest compatible versions
450445
[DOWNLOADING] crates ...
451446
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
452-
[WARNING] output filename collision.
453-
The lib target `bar` in package `bar v1.0.0` has the same output filename as the lib target `bar` in package `bar v1.0.0 ([ROOT]/foo/bar)`.
454-
Colliding filename is: [ROOT]/foo/target/doc/bar/index.html
455-
The targets should have unique names.
456-
This is a known bug where multiple crates with the same name use
457-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
447+
[WARNING] output filename collision at [ROOT]/foo/target/doc/bar/index.html
448+
|
449+
= [NOTE] the lib target `bar` in package `bar v1.0.0` has the same output filename as the lib target `bar` in package `bar v1.0.0 ([ROOT]/foo/bar)`
450+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
458451
[CHECKING] bar v1.0.0 ([ROOT]/foo/bar)
459452
[DOCUMENTING] bar v1.0.0 ([ROOT]/foo/bar)
460453
[DOCUMENTING] bar v1.0.0
@@ -576,12 +569,10 @@ fn collision_with_root() {
576569
[LOCKING] 1 package to latest compatible version
577570
[DOWNLOADING] crates ...
578571
[DOWNLOADED] foo-macro v1.0.0 (registry `dummy-registry`)
579-
[WARNING] output filename collision.
580-
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 ([ROOT]/foo/foo-macro)`.
581-
Colliding filename is: [ROOT]/foo/target/doc/foo_macro/index.html
582-
The targets should have unique names.
583-
This is a known bug where multiple crates with the same name use
584-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
572+
[WARNING] output filename collision at [ROOT]/foo/target/doc/foo_macro/index.html
573+
|
574+
= [NOTE] 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 ([ROOT]/foo/foo-macro)`
575+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
585576
[CHECKING] foo-macro v1.0.0
586577
[DOCUMENTING] foo-macro v1.0.0
587578
[CHECKING] abc v1.0.0 ([ROOT]/foo/abc)

tests/testsuite/doc.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,10 @@ fn doc_multiple_targets_same_name() {
297297

298298
p.cargo("doc --workspace")
299299
.with_stderr_data(str![[r#"
300-
[WARNING] output filename collision.
301-
The bin target `foo_lib` in package `foo v0.1.0 ([ROOT]/foo/foo)` has the same output filename as the lib target `foo_lib` in package `bar v0.1.0 ([ROOT]/foo/bar)`.
302-
Colliding filename is: [ROOT]/foo/target/doc/foo_lib/index.html
303-
The targets should have unique names.
304-
This is a known bug where multiple crates with the same name use
305-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
300+
[WARNING] output filename collision at [ROOT]/foo/target/doc/foo_lib/index.html
301+
|
302+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
303+
= [NOTE] the bin target `foo_lib` in package `foo v0.1.0 ([ROOT]/foo/foo)` has the same output filename as the lib target `foo_lib` in package `bar v0.1.0 ([ROOT]/foo/bar)`
306304
[DOCUMENTING] bar v0.1.0 ([ROOT]/foo/bar)
307305
[DOCUMENTING] foo v0.1.0 ([ROOT]/foo/foo)
308306
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -513,12 +511,10 @@ fn doc_lib_bin_same_name_documents_named_bin_when_requested() {
513511
// The checking/documenting lines are sometimes swapped since they run
514512
// concurrently.
515513
.with_stderr_data(str![[r#"
516-
[WARNING] output filename collision.
517-
The bin target `foo` in package `foo v0.0.1 ([ROOT]/foo)` has the same output filename as the lib target `foo` in package `foo v0.0.1 ([ROOT]/foo)`.
518-
Colliding filename is: [ROOT]/foo/target/doc/foo/index.html
519-
The targets should have unique names.
520-
This is a known bug where multiple crates with the same name use
521-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
514+
[WARNING] output filename collision at [ROOT]/foo/target/doc/foo/index.html
515+
|
516+
= [NOTE] the bin target `foo` in package `foo v0.0.1 ([ROOT]/foo)` has the same output filename as the lib target `foo` in package `foo v0.0.1 ([ROOT]/foo)`
517+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
522518
[CHECKING] foo v0.0.1 ([ROOT]/foo)
523519
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
524520
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -557,12 +553,10 @@ fn doc_lib_bin_same_name_documents_bins_when_requested() {
557553
// The checking/documenting lines are sometimes swapped since they run
558554
// concurrently.
559555
.with_stderr_data(str![[r#"
560-
[WARNING] output filename collision.
561-
The bin target `foo` in package `foo v0.0.1 ([ROOT]/foo)` has the same output filename as the lib target `foo` in package `foo v0.0.1 ([ROOT]/foo)`.
562-
Colliding filename is: [ROOT]/foo/target/doc/foo/index.html
563-
The targets should have unique names.
564-
This is a known bug where multiple crates with the same name use
565-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
556+
[WARNING] output filename collision at [ROOT]/foo/target/doc/foo/index.html
557+
|
558+
= [NOTE] the bin target `foo` in package `foo v0.0.1 ([ROOT]/foo)` has the same output filename as the lib target `foo` in package `foo v0.0.1 ([ROOT]/foo)`
559+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
566560
[CHECKING] foo v0.0.1 ([ROOT]/foo)
567561
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
568562
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -1408,12 +1402,10 @@ fn doc_all_member_dependency_same_name() {
14081402
[LOCKING] 1 package to latest compatible version
14091403
[DOWNLOADING] crates ...
14101404
[DOWNLOADED] bar v0.1.0 (registry `dummy-registry`)
1411-
[WARNING] output filename collision.
1412-
The lib target `bar` in package `bar v0.1.0` has the same output filename as the lib target `bar` in package `bar v0.1.0 ([ROOT]/foo/bar)`.
1413-
Colliding filename is: [ROOT]/foo/target/doc/bar/index.html
1414-
The targets should have unique names.
1415-
This is a known bug where multiple crates with the same name use
1416-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
1405+
[WARNING] output filename collision at [ROOT]/foo/target/doc/bar/index.html
1406+
|
1407+
= [NOTE] the lib target `bar` in package `bar v0.1.0` has the same output filename as the lib target `bar` in package `bar v0.1.0 ([ROOT]/foo/bar)`
1408+
= [NOTE] this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>
14171409
[DOCUMENTING] bar v0.1.0
14181410
[CHECKING] bar v0.1.0
14191411
[DOCUMENTING] bar v0.1.0 ([ROOT]/foo/bar)

0 commit comments

Comments
 (0)