Skip to content

Commit fa7712d

Browse files
author
Yihai Lin
committed
feat: Make no target found hint more clear.
1 parent bac98aa commit fa7712d

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],
@@ -288,24 +289,42 @@ impl<'a> UnitGenerator<'a, '_> {
288289
CargoResult::Ok(())
289290
};
290291

292+
let unmatched_packages = || match self.spec {
293+
Packages::Default | Packages::OptOut(_) | Packages::All(_) => {
294+
"default-run packages".to_owned()
295+
}
296+
Packages::Packages(packages) => {
297+
let first = packages
298+
.first()
299+
.expect("The number of packages must be at least 1");
300+
if packages.len() == 1 {
301+
format!("`{}` package", first)
302+
} else {
303+
format!("`{}`, ... packages", first)
304+
}
305+
}
306+
};
307+
291308
let mut msg = String::new();
292309
if !suggestion.is_empty() {
293310
write!(
294311
msg,
295-
"no {} target {} `{}`{}",
312+
"no {} target {} `{}` in {}{}",
296313
target_desc,
297314
if is_glob { "matches pattern" } else { "named" },
298315
target_name,
316+
unmatched_packages(),
299317
suggestion,
300318
)?;
301319
append_targets_elsewhere(&mut msg, "\n")?;
302320
} else {
303321
writeln!(
304322
msg,
305-
"no {} target {} `{}`.",
323+
"no {} target {} `{}` in {}.",
306324
target_desc,
307325
if is_glob { "matches pattern" } else { "named" },
308326
target_name,
327+
unmatched_packages()
309328
)?;
310329

311330
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
28392839
[HELP] Available bin in `pattern2` package:
@@ -2858,7 +2858,7 @@ Available binaries:
28582858
p.cargo("run --bin crate2")
28592859
.with_status(101)
28602860
.with_stderr_data(str![[r#"
2861-
[ERROR] no bin target named `crate2`.
2861+
[ERROR] no bin target named `crate2` in default-run packages.
28622862
[HELP] Available bin in `crate2` package:
28632863
crate2
28642864

0 commit comments

Comments
 (0)