Skip to content

Commit

Permalink
fix: Update generic 'did you meann' reports to not say that
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 3, 2025
1 parent c2023a1 commit 5408bd9
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -98,6 +99,7 @@ impl PackageIdSpecQuery for PackageIdSpec {
self.name(),
all_ids.iter(),
|id| id.name().as_str(),
"package",
));
}

Expand Down
7 changes: 6 additions & 1 deletion src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{}",
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_compile/unit_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl<'a> UnitGenerator<'a, '_> {
.filter(|target| is_expected_kind(target))
})
.collect::<Vec<_>>();
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 {} `{}`{}",
Expand Down
6 changes: 5 additions & 1 deletion src/cargo/util/edit_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ pub fn closest_msg<'a, T>(
choice: &str,
iter: impl Iterator<Item = T>,
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(),
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/package_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/profile_overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5408bd9

Please sign in to comment.