From abb463a7aad694087821b42e04c34e3df6e5f572 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 17 Aug 2022 21:43:27 +0800 Subject: [PATCH 1/2] Improve error message for wrong target names --- src/cargo/ops/cargo_compile.rs | 48 +++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index ced55770ef8..9cca2ee3099 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -23,6 +23,7 @@ //! repeats until the queue is empty. use std::collections::{BTreeSet, HashMap, HashSet}; +use std::fmt::Write; use std::hash::{Hash, Hasher}; use std::sync::Arc; @@ -1526,19 +1527,40 @@ fn find_named_targets<'a>( }; if proposals.is_empty() { - let targets = packages.iter().flat_map(|pkg| { - pkg.targets() - .iter() - .filter(|target| is_expected_kind(target)) - }); - let suggestion = closest_msg(target_name, targets, |t| t.name()); - anyhow::bail!( - "no {} target {} `{}`{}", - target_desc, - if is_glob { "matches pattern" } else { "named" }, - target_name, - suggestion - ); + let targets = packages + .iter() + .flat_map(|pkg| { + pkg.targets() + .iter() + .filter(|target| is_expected_kind(target)) + }) + .collect::>(); + let suggestion = closest_msg(target_name, targets.iter(), |t| t.name()); + if !suggestion.is_empty() { + anyhow::bail!( + "no {} target {} `{}`{}", + target_desc, + if is_glob { "matches pattern" } else { "named" }, + target_name, + suggestion + ); + } else { + let mut msg = String::new(); + writeln!( + msg, + "no {} target {} `{}`.", + target_desc, + if is_glob { "matches pattern" } else { "named" }, + target_name, + )?; + if !targets.is_empty() { + writeln!(msg, "Available {} targets:", target_desc)?; + for target in targets { + writeln!(msg, " {}", target.name())?; + } + } + anyhow::bail!(msg); + } } Ok(proposals) } From 99b3564d0d984af37b96b1ade047d1a8f5ddca7c Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 17 Aug 2022 22:28:36 +0800 Subject: [PATCH 2/2] Update old tests Signed-off-by: hi-rustin --- tests/testsuite/build.rs | 18 ++++++++++++++++-- tests/testsuite/run.rs | 32 ++++++++++++++++++++++++++++---- tests/testsuite/test.rs | 14 ++++++++++++-- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index ace734ba48a..8916340d2c9 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1041,7 +1041,14 @@ fn cargo_compile_with_filename() { p.cargo("build --bin bin.rs") .with_status(101) - .with_stderr("[ERROR] no bin target named `bin.rs`") + .with_stderr( + "\ +[ERROR] no bin target named `bin.rs`. +Available bin targets: + a + +", + ) .run(); p.cargo("build --bin a.rs") @@ -1056,7 +1063,14 @@ fn cargo_compile_with_filename() { p.cargo("build --example example.rs") .with_status(101) - .with_stderr("[ERROR] no example target named `example.rs`") + .with_stderr( + "\ +[ERROR] no example target named `example.rs`. +Available example targets: + a + +", + ) .run(); p.cargo("build --example a.rs") diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index 0ef9d49cdcf..667c851231f 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -503,7 +503,10 @@ 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`. +Available example targets: + do_magic + ", ) .run(); @@ -528,7 +531,14 @@ fn run_example_autodiscover_2015_with_autoexamples_disabled() { let p = autodiscover_examples_project("2015", Some(false)); p.cargo("run --example a") .with_status(101) - .with_stderr("error: no example target named `a`\n") + .with_stderr( + "\ +error: no example target named `a`. +Available example targets: + do_magic + +", + ) .run(); } @@ -600,7 +610,14 @@ fn run_with_filename() { p.cargo("run --bin bin.rs") .with_status(101) - .with_stderr("[ERROR] no bin target named `bin.rs`") + .with_stderr( + "\ +[ERROR] no bin target named `bin.rs`. +Available bin targets: + a + +", + ) .run(); p.cargo("run --bin a.rs") @@ -615,7 +632,14 @@ fn run_with_filename() { p.cargo("run --example example.rs") .with_status(101) - .with_stderr("[ERROR] no example target named `example.rs`") + .with_stderr( + "\ +[ERROR] no example target named `example.rs`. +Available example targets: + a + +", + ) .run(); p.cargo("run --example a.rs") diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index 95e109d452b..f310d0e5c9d 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -2122,11 +2122,21 @@ fn bad_example() { p.cargo("run --example foo") .with_status(101) - .with_stderr("[ERROR] no example target named `foo`") + .with_stderr( + "\ +[ERROR] no example target named `foo`. + +", + ) .run(); p.cargo("run --bin foo") .with_status(101) - .with_stderr("[ERROR] no bin target named `foo`") + .with_stderr( + "\ +[ERROR] no bin target named `foo`. + +", + ) .run(); }