From ec3ed20d2ab0d4a056e48d1c44186e0adb46d7b7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 17 Oct 2023 14:46:30 -0500 Subject: [PATCH] fix(cli): Suggest cargo-search on bad commands This is a low-tech solution alternative to the options proposed in #4682 - Search `[[bin]]`s within all packages in the registry (which aren't tracked atm), suggesting to `cargo install` what is found - Check if `cargo-` is in the registry, suggesting `cargo install if it is By suggesting `cargo search`, we are giving them a tool so they can verify if the package is what they want (a `cargo info` would help as a next step). Is is needed? - New users might not know of `cargo search` but they can search on crates.io - New users might not be aware of the `cargo-` naming pattern Seems like this can still offer some benefit Fixes #4682 --- src/bin/cargo/main.rs | 7 +++---- tests/testsuite/cargo_command.rs | 12 +++++++++--- tests/testsuite/script.rs | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 16e4f24f3b0..1fd91c99a05 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -192,10 +192,9 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> C let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c); anyhow::format_err!( - "no such command: `{}`{}\n\n\t\ - View all installed commands with `cargo --list`", - cmd, - did_you_mean + "no such command: `{cmd}`{did_you_mean}\n\n\t\ + View all installed commands with `cargo --list`\n\t\ + Find a package to install `{cmd}` with `cargo search cargo-{cmd}`", ) }; diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 62869387f2a..80885805bde 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -294,7 +294,9 @@ fn find_closest_dont_correct_nonsense() { "\ [ERROR] no such command: `there-is-no-way-that-there-is-a-command-close-to-this` -View all installed commands with `cargo --list`", +View all installed commands with `cargo --list` +Find a package to install `there-is-no-way-that-there-is-a-command-close-to-this` with `cargo search cargo-there-is-no-way-that-there-is-a-command-close-to-this` +", ) .run(); } @@ -307,7 +309,9 @@ fn displays_subcommand_on_error() { "\ [ERROR] no such command: `invalid-command` -View all installed commands with `cargo --list`", +View all installed commands with `cargo --list` +Find a package to install `invalid-command` with `cargo search cargo-invalid-command` +", ) .run(); } @@ -529,7 +533,9 @@ error: no such command: `bluid` Did you mean `build`? -View all installed commands with `cargo --list`", +View all installed commands with `cargo --list` +Find a package to install `bluid` with `cargo search cargo-bluid` +", ) .run(); } diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 96f3a5eb433..0b1e5a6b98e 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -108,6 +108,7 @@ error: no such command: `echo` Did you mean `bench`? View all installed commands with `cargo --list` +Find a package to install `echo` with `cargo search cargo-echo` ", ) .run();