Skip to content

Commit 3ba29cd

Browse files
author
Yihai Lin
committed
feat: Make no target found hint more clear.
1 parent bd2d4e2 commit 3ba29cd

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ pub fn create_bcx<'a, 'gctx>(
374374
let generator = UnitGenerator {
375375
ws,
376376
packages: &to_builds,
377+
spec,
377378
target_data: &target_data,
378379
filter,
379380
requested_kinds: &build_config.requested_kinds,

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct Proposal<'a> {
4848
pub(super) struct UnitGenerator<'a, 'gctx> {
4949
pub ws: &'a Workspace<'gctx>,
5050
pub packages: &'a [&'a Package],
51+
pub spec: &'a Packages,
5152
pub target_data: &'a RustcTargetData<'gctx>,
5253
pub filter: &'a CompileFilter,
5354
pub requested_kinds: &'a [CompileKind],
@@ -282,14 +283,31 @@ impl<'a> UnitGenerator<'a, '_> {
282283
CargoResult::Ok(())
283284
};
284285

286+
let unmatched_packages = || match self.spec {
287+
Packages::Default | Packages::OptOut(_) | Packages::All(_) => {
288+
"default-run packages".to_owned()
289+
}
290+
Packages::Packages(packages) => {
291+
let first = packages
292+
.first()
293+
.expect("The number of packages must be at least 1");
294+
if packages.len() == 1 {
295+
format!("`{}` package", first)
296+
} else {
297+
format!("`{}`, ... packages", first)
298+
}
299+
}
300+
};
301+
285302
let mut msg = String::new();
286303
if !suggestion.is_empty() {
287304
write!(
288305
msg,
289-
"no {} target {} `{}`{}{}",
306+
"no {} target {} `{}` in {}{}{}",
290307
target_desc,
291308
if is_glob { "matches pattern" } else { "named" },
292309
target_name,
310+
unmatched_packages(),
293311
suggestion,
294312
if need_append_targets_elsewhere {
295313
"\n"
@@ -301,10 +319,11 @@ impl<'a> UnitGenerator<'a, '_> {
301319
} else {
302320
writeln!(
303321
msg,
304-
"no {} target {} `{}`.",
322+
"no {} target {} `{}` in {}.",
305323
target_desc,
306324
if is_glob { "matches pattern" } else { "named" },
307325
target_name,
326+
unmatched_packages()
308327
)?;
309328

310329
append_targets_elsewhere(&mut msg)?;

tests/testsuite/build.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ fn cargo_compile_with_filename() {
13371337
p.cargo("build --bin bin.rs")
13381338
.with_status(101)
13391339
.with_stderr_data(str![[r#"
1340-
[ERROR] no bin target named `bin.rs`.
1340+
[ERROR] no bin target named `bin.rs` in default-run packages.
13411341
Available bin targets:
13421342
a
13431343
@@ -1348,7 +1348,7 @@ Available bin targets:
13481348
p.cargo("build --bin a.rs")
13491349
.with_status(101)
13501350
.with_stderr_data(str![[r#"
1351-
[ERROR] no bin target named `a.rs`
1351+
[ERROR] no bin target named `a.rs` in default-run packages
13521352
13531353
[HELP] a target with a similar name exists: `a`
13541354
@@ -1358,7 +1358,7 @@ Available bin targets:
13581358
p.cargo("build --example example.rs")
13591359
.with_status(101)
13601360
.with_stderr_data(str![[r#"
1361-
[ERROR] no example target named `example.rs`.
1361+
[ERROR] no example target named `example.rs` in default-run packages.
13621362
Available example targets:
13631363
a
13641364
@@ -1369,7 +1369,7 @@ Available example targets:
13691369
p.cargo("build --example a.rs")
13701370
.with_status(101)
13711371
.with_stderr_data(str![[r#"
1372-
[ERROR] no example target named `a.rs`
1372+
[ERROR] no example target named `a.rs` in default-run packages
13731373
13741374
[HELP] a target with a similar name exists: `a`
13751375
@@ -5906,7 +5906,7 @@ fn target_filters_workspace() {
59065906
ws.cargo("build -v --example ex")
59075907
.with_status(101)
59085908
.with_stderr_data(str![[r#"
5909-
[ERROR] no example target named `ex`
5909+
[ERROR] no example target named `ex` in default-run packages
59105910
59115911
[HELP] a target with a similar name exists: `ex1`
59125912
@@ -5916,7 +5916,7 @@ fn target_filters_workspace() {
59165916
ws.cargo("build -v --example 'ex??'")
59175917
.with_status(101)
59185918
.with_stderr_data(str![[r#"
5919-
[ERROR] no example target matches pattern `ex??`
5919+
[ERROR] no example target matches pattern `ex??` in default-run packages
59205920
59215921
[HELP] a target with a similar name exists: `ex1`
59225922

tests/testsuite/run.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ automatically infer them to be a target, such as in subfolders.
623623
624624
For more information on this warning you can consult
625625
https://github.com/rust-lang/cargo/issues/5330
626-
[ERROR] no example target named `a`.
626+
[ERROR] no example target named `a` in default-run packages.
627627
Available example targets:
628628
do_magic
629629
@@ -655,7 +655,7 @@ fn run_example_autodiscover_2015_with_autoexamples_disabled() {
655655
p.cargo("run --example a")
656656
.with_status(101)
657657
.with_stderr_data(str![[r#"
658-
[ERROR] no example target named `a`.
658+
[ERROR] no example target named `a` in default-run packages.
659659
Available example targets:
660660
do_magic
661661
@@ -743,7 +743,7 @@ fn run_with_filename() {
743743
p.cargo("run --bin bin.rs")
744744
.with_status(101)
745745
.with_stderr_data(str![[r#"
746-
[ERROR] no bin target named `bin.rs`.
746+
[ERROR] no bin target named `bin.rs` in default-run packages.
747747
Available bin targets:
748748
a
749749
@@ -754,7 +754,7 @@ Available bin targets:
754754
p.cargo("run --bin a.rs")
755755
.with_status(101)
756756
.with_stderr_data(str![[r#"
757-
[ERROR] no bin target named `a.rs`
757+
[ERROR] no bin target named `a.rs` in default-run packages
758758
759759
[HELP] a target with a similar name exists: `a`
760760
@@ -764,7 +764,7 @@ Available bin targets:
764764
p.cargo("run --example example.rs")
765765
.with_status(101)
766766
.with_stderr_data(str![[r#"
767-
[ERROR] no example target named `example.rs`.
767+
[ERROR] no example target named `example.rs` in default-run packages.
768768
Available example targets:
769769
a
770770
@@ -775,7 +775,7 @@ Available example targets:
775775
p.cargo("run --example a.rs")
776776
.with_status(101)
777777
.with_stderr_data(str![[r#"
778-
[ERROR] no example target named `a.rs`
778+
[ERROR] no example target named `a.rs` in default-run packages
779779
780780
[HELP] a target with a similar name exists: `a`
781781

tests/testsuite/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,15 +2447,15 @@ fn bad_example() {
24472447
p.cargo("run --example foo")
24482448
.with_status(101)
24492449
.with_stderr_data(str![[r#"
2450-
[ERROR] no example target named `foo`.
2450+
[ERROR] no example target named `foo` in default-run packages.
24512451
24522452
24532453
"#]])
24542454
.run();
24552455
p.cargo("run --bin foo")
24562456
.with_status(101)
24572457
.with_stderr_data(str![[r#"
2458-
[ERROR] no bin target named `foo`.
2458+
[ERROR] no bin target named `foo` in default-run packages.
24592459
24602460
24612461
"#]])

tests/testsuite/workspaces.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,7 +2797,7 @@ Available binaries:
27972797
p.cargo("run -p crate1 --bin crate2")
27982798
.with_status(101)
27992799
.with_stderr_data(str![[r#"
2800-
[ERROR] no bin target named `crate2`
2800+
[ERROR] no bin target named `crate2` in `crate1` package
28012801
28022802
[HELP] a target with a similar name exists: `crate1`
28032803
[HELP] Available bin in `crate2` package:
@@ -2809,7 +2809,7 @@ Available binaries:
28092809
p.cargo("check -p crate1 -p pattern1 -p pattern2 --bin crate2")
28102810
.with_status(101)
28112811
.with_stderr_data(str![[r#"
2812-
[ERROR] no bin target named `crate2`
2812+
[ERROR] no bin target named `crate2` in `crate1`, ... packages
28132813
28142814
[HELP] a target with a similar name exists: `crate1`
28152815
[HELP] Available bin in `crate2` package:
@@ -2821,7 +2821,7 @@ Available binaries:
28212821
p.cargo("run --bin crate2")
28222822
.with_status(101)
28232823
.with_stderr_data(str![[r#"
2824-
[ERROR] no bin target named `crate2`
2824+
[ERROR] no bin target named `crate2` in default-run packages
28252825
28262826
[HELP] a target with a similar name exists: `crate1`
28272827
[HELP] Available bin in `crate2` package:
@@ -2833,7 +2833,7 @@ Available binaries:
28332833
p.cargo("check --bin pattern*")
28342834
.with_status(101)
28352835
.with_stderr_data(str![[r#"
2836-
[ERROR] no bin target matches pattern `pattern*`.
2836+
[ERROR] no bin target matches pattern `pattern*` in default-run packages.
28372837
[HELP] Available bin in `pattern1` package:
28382838
pattern1[HELP] Available bin in `pattern2` package:
28392839
pattern2
@@ -2857,7 +2857,7 @@ Available binaries:
28572857
p.cargo("run --bin crate2")
28582858
.with_status(101)
28592859
.with_stderr_data(str![[r#"
2860-
[ERROR] no bin target named `crate2`.
2860+
[ERROR] no bin target named `crate2` in default-run packages.
28612861
[HELP] Available bin in `crate2` package:
28622862
crate2
28632863

0 commit comments

Comments
 (0)