Skip to content

Commit

Permalink
feat: Make no target found hint more clear.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yihai Lin committed Feb 25, 2025
1 parent ac56062 commit 685f0fd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ pub fn create_bcx<'a, 'gctx>(
let generator = UnitGenerator {
ws,
packages: &to_builds,
spec,
target_data: &target_data,
filter,
requested_kinds: &build_config.requested_kinds,
Expand Down
23 changes: 21 additions & 2 deletions src/cargo/ops/cargo_compile/unit_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Proposal<'a> {
pub(super) struct UnitGenerator<'a, 'gctx> {
pub ws: &'a Workspace<'gctx>,
pub packages: &'a [&'a Package],
pub spec: &'a Packages,
pub target_data: &'a RustcTargetData<'gctx>,
pub filter: &'a CompileFilter,
pub requested_kinds: &'a [CompileKind],
Expand Down Expand Up @@ -282,14 +283,31 @@ impl<'a> UnitGenerator<'a, '_> {
CargoResult::Ok(())
};

let unmatched_packages = || match self.spec {
Packages::Default | Packages::OptOut(_) | Packages::All(_) => {
"default-run packages".to_owned()
}
Packages::Packages(packages) => {
let first = packages
.first()
.expect("The number of packages must be at least 1");
if packages.len() == 1 {
format!("`{}` package", first)
} else {
format!("`{}`, ... packages", first)
}
}
};

let mut msg = String::new();
if !suggestion.is_empty() {
write!(
msg,
"no {} target {} `{}`{}{}",
"no {} target {} `{}` in {}{}{}",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
unmatched_packages(),
suggestion,
if need_append_targets_elsewhere {
"\n"
Expand All @@ -301,10 +319,11 @@ impl<'a> UnitGenerator<'a, '_> {
} else {
writeln!(
msg,
"no {} target {} `{}`.",
"no {} target {} `{}` in {}.",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
unmatched_packages()
)?;

append_targets_elsewhere(&mut msg)?;
Expand Down
12 changes: 6 additions & 6 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ fn cargo_compile_with_filename() {
p.cargo("build --bin bin.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `bin.rs`.
[ERROR] no bin target named `bin.rs` in default-run packages.
Available bin targets:
a
Expand All @@ -1348,7 +1348,7 @@ Available bin targets:
p.cargo("build --bin a.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `a.rs`
[ERROR] no bin target named `a.rs` in default-run packages
[HELP] a target with a similar name exists: `a`
Expand All @@ -1358,7 +1358,7 @@ Available bin targets:
p.cargo("build --example example.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `example.rs`.
[ERROR] no example target named `example.rs` in default-run packages.
Available example targets:
a
Expand All @@ -1369,7 +1369,7 @@ Available example targets:
p.cargo("build --example a.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `a.rs`
[ERROR] no example target named `a.rs` in default-run packages
[HELP] a target with a similar name exists: `a`
Expand Down Expand Up @@ -5906,7 +5906,7 @@ fn target_filters_workspace() {
ws.cargo("build -v --example ex")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `ex`
[ERROR] no example target named `ex` in default-run packages
[HELP] a target with a similar name exists: `ex1`
Expand All @@ -5916,7 +5916,7 @@ fn target_filters_workspace() {
ws.cargo("build -v --example 'ex??'")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target matches pattern `ex??`
[ERROR] no example target matches pattern `ex??` in default-run packages
[HELP] a target with a similar name exists: `ex1`
Expand Down
12 changes: 6 additions & 6 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ automatically infer them to be a target, such as in subfolders.
For more information on this warning you can consult
https://github.com/rust-lang/cargo/issues/5330
[ERROR] no example target named `a`.
[ERROR] no example target named `a` in default-run packages.
Available example targets:
do_magic
Expand Down Expand Up @@ -655,7 +655,7 @@ fn run_example_autodiscover_2015_with_autoexamples_disabled() {
p.cargo("run --example a")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `a`.
[ERROR] no example target named `a` in default-run packages.
Available example targets:
do_magic
Expand Down Expand Up @@ -743,7 +743,7 @@ fn run_with_filename() {
p.cargo("run --bin bin.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `bin.rs`.
[ERROR] no bin target named `bin.rs` in default-run packages.
Available bin targets:
a
Expand All @@ -754,7 +754,7 @@ Available bin targets:
p.cargo("run --bin a.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `a.rs`
[ERROR] no bin target named `a.rs` in default-run packages
[HELP] a target with a similar name exists: `a`
Expand All @@ -764,7 +764,7 @@ Available bin targets:
p.cargo("run --example example.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `example.rs`.
[ERROR] no example target named `example.rs` in default-run packages.
Available example targets:
a
Expand All @@ -775,7 +775,7 @@ Available example targets:
p.cargo("run --example a.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `a.rs`
[ERROR] no example target named `a.rs` in default-run packages
[HELP] a target with a similar name exists: `a`
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2447,15 +2447,15 @@ fn bad_example() {
p.cargo("run --example foo")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `foo`.
[ERROR] no example target named `foo` in default-run packages.
"#]])
.run();
p.cargo("run --bin foo")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `foo`.
[ERROR] no bin target named `foo` in default-run packages.
"#]])
Expand Down
10 changes: 5 additions & 5 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,7 @@ Available binaries:
p.cargo("run -p crate1 --bin crate2")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `crate2`
[ERROR] no bin target named `crate2` in `crate1` package
[HELP] a target with a similar name exists: `crate1`
[HELP] Available bin in `crate2` package:
Expand All @@ -2810,7 +2810,7 @@ Available binaries:
p.cargo("check -p crate1 -p pattern1 -p pattern2 --bin crate2")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `crate2`
[ERROR] no bin target named `crate2` in `crate1`, ... packages
[HELP] a target with a similar name exists: `crate1`
[HELP] Available bin in `crate2` package:
Expand All @@ -2823,7 +2823,7 @@ Available binaries:
p.cargo("run --bin crate2")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `crate2`
[ERROR] no bin target named `crate2` in default-run packages
[HELP] a target with a similar name exists: `crate1`
[HELP] Available bin in `crate2` package:
Expand All @@ -2836,7 +2836,7 @@ Available binaries:
p.cargo("check --bin pattern*")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target matches pattern `pattern*`.
[ERROR] no bin target matches pattern `pattern*` in default-run packages.
[HELP] Available bin in `pattern1` package:
pattern1
[HELP] Available bin in `pattern2` package:
Expand All @@ -2862,7 +2862,7 @@ Available binaries:
p.cargo("run --bin crate2")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `crate2`.
[ERROR] no bin target named `crate2` in default-run packages.
[HELP] Available bin in `crate2` package:
crate2
Expand Down

0 comments on commit 685f0fd

Please sign in to comment.