Skip to content

Commit 6066463

Browse files
committed
Auto merge of #10999 - hi-rustin:rustin-patch-wrong-names, r=epage
Improve error message for wrong target names ### What does this PR try to resolve? Fixes #10982 We can print all available targets for users. ### How should we test and review this PR? - [x] unit test
2 parents 0c57749 + 99b3564 commit 6066463

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//! repeats until the queue is empty.
2424
2525
use std::collections::{BTreeSet, HashMap, HashSet};
26+
use std::fmt::Write;
2627
use std::hash::{Hash, Hasher};
2728
use std::sync::Arc;
2829

@@ -1526,19 +1527,40 @@ fn find_named_targets<'a>(
15261527
};
15271528

15281529
if proposals.is_empty() {
1529-
let targets = packages.iter().flat_map(|pkg| {
1530-
pkg.targets()
1531-
.iter()
1532-
.filter(|target| is_expected_kind(target))
1533-
});
1534-
let suggestion = closest_msg(target_name, targets, |t| t.name());
1535-
anyhow::bail!(
1536-
"no {} target {} `{}`{}",
1537-
target_desc,
1538-
if is_glob { "matches pattern" } else { "named" },
1539-
target_name,
1540-
suggestion
1541-
);
1530+
let targets = packages
1531+
.iter()
1532+
.flat_map(|pkg| {
1533+
pkg.targets()
1534+
.iter()
1535+
.filter(|target| is_expected_kind(target))
1536+
})
1537+
.collect::<Vec<_>>();
1538+
let suggestion = closest_msg(target_name, targets.iter(), |t| t.name());
1539+
if !suggestion.is_empty() {
1540+
anyhow::bail!(
1541+
"no {} target {} `{}`{}",
1542+
target_desc,
1543+
if is_glob { "matches pattern" } else { "named" },
1544+
target_name,
1545+
suggestion
1546+
);
1547+
} else {
1548+
let mut msg = String::new();
1549+
writeln!(
1550+
msg,
1551+
"no {} target {} `{}`.",
1552+
target_desc,
1553+
if is_glob { "matches pattern" } else { "named" },
1554+
target_name,
1555+
)?;
1556+
if !targets.is_empty() {
1557+
writeln!(msg, "Available {} targets:", target_desc)?;
1558+
for target in targets {
1559+
writeln!(msg, " {}", target.name())?;
1560+
}
1561+
}
1562+
anyhow::bail!(msg);
1563+
}
15421564
}
15431565
Ok(proposals)
15441566
}

tests/testsuite/build.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,14 @@ fn cargo_compile_with_filename() {
10411041

10421042
p.cargo("build --bin bin.rs")
10431043
.with_status(101)
1044-
.with_stderr("[ERROR] no bin target named `bin.rs`")
1044+
.with_stderr(
1045+
"\
1046+
[ERROR] no bin target named `bin.rs`.
1047+
Available bin targets:
1048+
a
1049+
1050+
",
1051+
)
10451052
.run();
10461053

10471054
p.cargo("build --bin a.rs")
@@ -1056,7 +1063,14 @@ fn cargo_compile_with_filename() {
10561063

10571064
p.cargo("build --example example.rs")
10581065
.with_status(101)
1059-
.with_stderr("[ERROR] no example target named `example.rs`")
1066+
.with_stderr(
1067+
"\
1068+
[ERROR] no example target named `example.rs`.
1069+
Available example targets:
1070+
a
1071+
1072+
",
1073+
)
10601074
.run();
10611075

10621076
p.cargo("build --example a.rs")

tests/testsuite/run.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,10 @@ automatically infer them to be a target, such as in subfolders.
503503
504504
For more information on this warning you can consult
505505
https://github.com/rust-lang/cargo/issues/5330
506-
error: no example target named `a`
506+
error: no example target named `a`.
507+
Available example targets:
508+
do_magic
509+
507510
",
508511
)
509512
.run();
@@ -528,7 +531,14 @@ fn run_example_autodiscover_2015_with_autoexamples_disabled() {
528531
let p = autodiscover_examples_project("2015", Some(false));
529532
p.cargo("run --example a")
530533
.with_status(101)
531-
.with_stderr("error: no example target named `a`\n")
534+
.with_stderr(
535+
"\
536+
error: no example target named `a`.
537+
Available example targets:
538+
do_magic
539+
540+
",
541+
)
532542
.run();
533543
}
534544

@@ -600,7 +610,14 @@ fn run_with_filename() {
600610

601611
p.cargo("run --bin bin.rs")
602612
.with_status(101)
603-
.with_stderr("[ERROR] no bin target named `bin.rs`")
613+
.with_stderr(
614+
"\
615+
[ERROR] no bin target named `bin.rs`.
616+
Available bin targets:
617+
a
618+
619+
",
620+
)
604621
.run();
605622

606623
p.cargo("run --bin a.rs")
@@ -615,7 +632,14 @@ fn run_with_filename() {
615632

616633
p.cargo("run --example example.rs")
617634
.with_status(101)
618-
.with_stderr("[ERROR] no example target named `example.rs`")
635+
.with_stderr(
636+
"\
637+
[ERROR] no example target named `example.rs`.
638+
Available example targets:
639+
a
640+
641+
",
642+
)
619643
.run();
620644

621645
p.cargo("run --example a.rs")

tests/testsuite/test.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,11 +2122,21 @@ fn bad_example() {
21222122

21232123
p.cargo("run --example foo")
21242124
.with_status(101)
2125-
.with_stderr("[ERROR] no example target named `foo`")
2125+
.with_stderr(
2126+
"\
2127+
[ERROR] no example target named `foo`.
2128+
2129+
",
2130+
)
21262131
.run();
21272132
p.cargo("run --bin foo")
21282133
.with_status(101)
2129-
.with_stderr("[ERROR] no bin target named `foo`")
2134+
.with_stderr(
2135+
"\
2136+
[ERROR] no bin target named `foo`.
2137+
2138+
",
2139+
)
21302140
.run();
21312141
}
21322142

0 commit comments

Comments
 (0)