diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 062ab30f444..18746dd64b7 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -293,7 +293,7 @@ fn execute_external_subcommand(gctx: &GlobalContext, cmd: &str, args: &[&OsStr]) ) } else { let suggestions = list_commands(gctx); - let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c); + let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c, "command"); anyhow::format_err!( "no such command: `{cmd}`{did_you_mean}\n\n\t\ diff --git a/src/cargo/core/package_id_spec.rs b/src/cargo/core/package_id_spec.rs index f146214e9df..3796c0d44f7 100644 --- a/src/cargo/core/package_id_spec.rs +++ b/src/cargo/core/package_id_spec.rs @@ -30,7 +30,8 @@ impl PackageIdSpecQuery for PackageIdSpec { { let i: Vec<_> = i.into_iter().collect(); let spec = PackageIdSpec::parse(spec).with_context(|| { - let suggestion = edit_distance::closest_msg(spec, i.iter(), |id| id.name().as_str()); + let suggestion = + edit_distance::closest_msg(spec, i.iter(), |id| id.name().as_str(), "package"); format!("invalid package ID specification: `{}`{}", spec, suggestion) })?; spec.query(i) @@ -98,6 +99,7 @@ impl PackageIdSpecQuery for PackageIdSpec { self.name(), all_ids.iter(), |id| id.name().as_str(), + "package", )); } diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index b7e8fe9c904..cac149efaa7 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -1414,7 +1414,12 @@ fn validate_packages_unmatched( }) .collect(); if name_matches.is_empty() { - let suggestion = closest_msg(&spec.name(), resolve.iter(), |p| p.name().as_str()); + let suggestion = closest_msg( + &spec.name(), + resolve.iter(), + |p| p.name().as_str(), + "package", + ); shell.warn(format!( "profile package spec `{}` in profile `{}` did not match any packages{}", spec, name, suggestion diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 5297430b175..45519bf2a74 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -166,6 +166,7 @@ fn clean_specs( &spec.name(), resolve.iter(), |id| id.name().as_str(), + "package", )); anyhow::bail!( "package ID specification `{}` did not match any packages{}", diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index ce10e173c6c..ae1f3bc56c8 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -266,7 +266,7 @@ impl<'a> UnitGenerator<'a, '_> { .filter(|target| is_expected_kind(target)) }) .collect::>(); - let suggestion = closest_msg(target_name, targets.iter(), |t| t.name()); + let suggestion = closest_msg(target_name, targets.iter(), |t| t.name(), "target"); if !suggestion.is_empty() { anyhow::bail!( "no {} target {} `{}`{}", diff --git a/src/cargo/util/edit_distance.rs b/src/cargo/util/edit_distance.rs index f77e48b4401..303f7a2d609 100644 --- a/src/cargo/util/edit_distance.rs +++ b/src/cargo/util/edit_distance.rs @@ -113,9 +113,13 @@ pub fn closest_msg<'a, T>( choice: &str, iter: impl Iterator, key: impl Fn(&T) -> &'a str, + kind: &str, ) -> String { match closest(choice, iter, &key) { - Some(e) => format!("\n\n\tDid you mean `{}`?", key(&e)), + Some(e) => format!( + "\n\n\thelp: a {kind} with a similar name exists: `{}`", + key(&e) + ), None => String::new(), } } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 6ab5932d1ad..4eed082eb69 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1535,8 +1535,12 @@ pub fn to_real_manifest( .filter(|t| t.is_bin()) .any(|t| t.name() == run) { - let suggestion = - util::closest_msg(run, targets.iter().filter(|t| t.is_bin()), |t| t.name()); + let suggestion = util::closest_msg( + run, + targets.iter().filter(|t| t.is_bin()), + |t| t.name(), + "target", + ); bail!("default-run target `{}` not found{}", run, suggestion); } } diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 5b5ad0dfc3c..d5c2ab898f4 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1350,7 +1350,7 @@ Available bin targets: .with_stderr_data(str![[r#" [ERROR] no bin target named `a.rs` - Did you mean `a`? + [HELP] a target with a similar name exists: `a` "#]]) .run(); @@ -1371,7 +1371,7 @@ Available example targets: .with_stderr_data(str![[r#" [ERROR] no example target named `a.rs` - Did you mean `a`? + [HELP] a target with a similar name exists: `a` "#]]) .run(); @@ -5908,7 +5908,7 @@ fn target_filters_workspace() { .with_stderr_data(str![[r#" [ERROR] no example target named `ex` - Did you mean `ex1`? + [HELP] a target with a similar name exists: `ex1` "#]]) .run(); @@ -5918,7 +5918,7 @@ fn target_filters_workspace() { .with_stderr_data(str![[r#" [ERROR] no example target matches pattern `ex??` - Did you mean `ex1`? + [HELP] a target with a similar name exists: `ex1` "#]]) .run(); diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 79c958972e4..1d8063fe25b 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -186,7 +186,7 @@ fn find_closest_capital_c_to_c() { .with_stderr_data(str![[r#" [ERROR] no such command: `C` - Did you mean `c`? + [HELP] a command with a similar name exists: `c` View all installed commands with `cargo --list` Find a package to install `C` with `cargo search cargo-C` @@ -202,7 +202,7 @@ fn find_closest_capital_b_to_b() { .with_stderr_data(str![[r#" [ERROR] no such command: `B` - Did you mean `b`? + [HELP] a command with a similar name exists: `b` View all installed commands with `cargo --list` Find a package to install `B` with `cargo search cargo-B` @@ -218,7 +218,7 @@ fn find_closest_biuld_to_build() { .with_stderr_data(str![[r#" [ERROR] no such command: `biuld` - Did you mean `build`? + [HELP] a command with a similar name exists: `build` View all installed commands with `cargo --list` Find a package to install `biuld` with `cargo search cargo-biuld` @@ -276,7 +276,7 @@ fn find_closest_alias() { .with_stderr_data(str![[r#" [ERROR] no such command: `myalais` - Did you mean `myalias`? + [HELP] a command with a similar name exists: `myalias` View all installed commands with `cargo --list` Find a package to install `myalais` with `cargo search cargo-myalais` @@ -562,7 +562,7 @@ fn full_did_you_mean() { .with_stderr_data(str![[r#" [ERROR] no such command: `bluid` - Did you mean `build`? + [HELP] a command with a similar name exists: `build` View all installed commands with `cargo --list` Find a package to install `bluid` with `cargo search cargo-bluid` diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index 9503ab85123..433b2a100e0 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -639,7 +639,7 @@ fn clean_spec_version() { .with_stderr_data(str![[r#" [ERROR] package ID specification `baz` did not match any packages - Did you mean `bar`? + [HELP] a package with a similar name exists: `bar` "#]]) .run(); @@ -694,7 +694,7 @@ fn clean_spec_partial_version() { .with_stderr_data(str![[r#" [ERROR] package ID specification `baz` did not match any packages - Did you mean `bar`? + [HELP] a package with a similar name exists: `bar` "#]]) .run(); @@ -749,7 +749,7 @@ fn clean_spec_partial_version_ambiguous() { .with_stderr_data(str![[r#" [ERROR] package ID specification `baz` did not match any packages - Did you mean `bar`? + [HELP] a package with a similar name exists: `bar` "#]]) .run(); diff --git a/tests/testsuite/help.rs b/tests/testsuite/help.rs index b5c2178f235..701e12ca78d 100644 --- a/tests/testsuite/help.rs +++ b/tests/testsuite/help.rs @@ -142,7 +142,7 @@ fn help_alias() { .with_stderr_data(str![[r#" [ERROR] no such command: `empty-alias` - Did you mean `empty-alias`? + [HELP] a command with a similar name exists: `empty-alias` View all installed commands with `cargo --list` Find a package to install `empty-alias` with `cargo search cargo-empty-alias` diff --git a/tests/testsuite/package_features.rs b/tests/testsuite/package_features.rs index a64ed01210a..785d8133a2d 100644 --- a/tests/testsuite/package_features.rs +++ b/tests/testsuite/package_features.rs @@ -737,7 +737,7 @@ fn non_member_feature() { .with_stderr_data(str![[r#" [ERROR] package ID specification `bar` did not match any packages - Did you mean `foo`? + [HELP] a package with a similar name exists: `foo` "#]]) .run(); @@ -796,7 +796,7 @@ fn non_member_feature() { .with_stderr_data(str![[r#" [ERROR] package ID specification `bar` did not match any packages - Did you mean `foo`? + [HELP] a package with a similar name exists: `foo` "#]]) .run(); diff --git a/tests/testsuite/pkgid.rs b/tests/testsuite/pkgid.rs index f4471e04085..7c1ae479d61 100644 --- a/tests/testsuite/pkgid.rs +++ b/tests/testsuite/pkgid.rs @@ -63,7 +63,7 @@ Caused by: .with_stderr_data(str![[r#" [ERROR] invalid package ID specification: `./bar` - Did you mean `bar`? + [HELP] a package with a similar name exists: `bar` Caused by: package ID specification `./bar` looks like a file path, maybe try [ROOTURL]/foo/bar @@ -119,7 +119,7 @@ registry+https://github.com/rust-lang/crates.io-index#crates-io@0.1.0 .with_stderr_data(str![[r#" [ERROR] package ID specification `crates_io` did not match any packages - Did you mean `crates-io`? + [HELP] a package with a similar name exists: `crates-io` "#]]) .run(); diff --git a/tests/testsuite/profile_overrides.rs b/tests/testsuite/profile_overrides.rs index 5db4b1f02c0..fbb90033c36 100644 --- a/tests/testsuite/profile_overrides.rs +++ b/tests/testsuite/profile_overrides.rs @@ -78,7 +78,7 @@ fn profile_override_warnings() { [WARNING] profile package spec `bar@1.2.3` in profile `dev` has a version or URL that does not match any of the packages: bar v0.5.0 ([ROOT]/foo/bar) [WARNING] profile package spec `bart` in profile `dev` did not match any packages - Did you mean `bar`? + [HELP] a package with a similar name exists: `bar` [WARNING] profile package spec `no-suggestion` in profile `dev` did not match any packages [COMPILING] bar v0.5.0 ([ROOT]/foo/bar) [COMPILING] foo v0.0.1 ([ROOT]/foo) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 4e322431bd0..6327377c255 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -2763,7 +2763,7 @@ fn in_package_workspace_not_found() { .with_stderr_data(str![[r#" [ERROR] package ID specification `li` did not match any packages - Did you mean `foo`? + [HELP] a package with a similar name exists: `foo` "#]]) .run(); @@ -2865,7 +2865,7 @@ fn publish_path_dependency_without_workspace() { .with_stderr_data(str![[r#" [ERROR] package ID specification `bar` did not match any packages - Did you mean `foo`? + [HELP] a package with a similar name exists: `foo` "#]]) .run(); diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index 8c014bba8b8..550bd055d8f 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -474,7 +474,7 @@ fn bogus_default_run() { Caused by: default-run target `b` not found - Did you mean `a`? + [HELP] a target with a similar name exists: `a` "#]]) .run(); @@ -756,7 +756,7 @@ Available bin targets: .with_stderr_data(str![[r#" [ERROR] no bin target named `a.rs` - Did you mean `a`? + [HELP] a target with a similar name exists: `a` "#]]) .run(); @@ -777,7 +777,7 @@ Available example targets: .with_stderr_data(str![[r#" [ERROR] no example target named `a.rs` - Did you mean `a`? + [HELP] a target with a similar name exists: `a` "#]]) .run(); diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index c5000ac3077..af776a182d3 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -84,7 +84,7 @@ fn path_required() { .with_stderr_data(str![[r#" [ERROR] no such command: `echo` - Did you mean `bench`? + [HELP] a command with a similar name exists: `bench` View all installed commands with `cargo --list` Find a package to install `echo` with `cargo search cargo-echo` diff --git a/tests/testsuite/tree.rs b/tests/testsuite/tree.rs index 762cc4d9652..2113d6b8afe 100644 --- a/tests/testsuite/tree.rs +++ b/tests/testsuite/tree.rs @@ -1967,7 +1967,7 @@ foo v0.1.0 ([ROOT]/foo) .with_stderr_data(str![[r#" [ERROR] package ID specification `no-dep` did not match any packages - Did you mean `bdep`? + [HELP] a package with a similar name exists: `bdep` "#]]) .with_status(101)