Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message for no such subcommand #10924

Merged
merged 16 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,19 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
let command = match path {
Some(command) => command,
None => {
let suggestions = list_commands(config);
let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c);
let err = anyhow::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean);
let err = if cmd.starts_with('+') {
anyhow::format_err!("no such subcommand: `{}`\n\n\tCargo does not handle `+toolchain` directives.\n\tDid you mean to run `cargo` through `rustup` instead?", cmd)
bindsdev marked this conversation as resolved.
Show resolved Hide resolved
} else {
let suggestions = list_commands(config);
let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c);

if did_you_mean.is_empty() {
anyhow::format_err!("no such subcommand: `{}`\n\n\tView all installed commands with `cargo --list`", cmd)
} else {
anyhow::format_err!("no such subcommand: `{}`{}\n\tView all installed commands with `cargo --list`", cmd, did_you_mean)
}
bindsdev marked this conversation as resolved.
Show resolved Hide resolved
};

return Err(CliError::new(err, 101));
}
};
Expand Down
14 changes: 10 additions & 4 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ fn find_closest_dont_correct_nonsense() {
.cwd(&paths::root())
.with_status(101)
.with_stderr(
"[ERROR] no such subcommand: \
`there-is-no-way-that-there-is-a-command-close-to-this`
",
"\
[ERROR] no such subcommand: `there-is-no-way-that-there-is-a-command-close-to-this`

<tab>View all installed commands with `cargo --list`",
)
.run();
}
Expand All @@ -273,7 +274,12 @@ fn find_closest_dont_correct_nonsense() {
fn displays_subcommand_on_error() {
cargo_process("invalid-command")
.with_status(101)
.with_stderr("[ERROR] no such subcommand: `invalid-command`\n")
.with_stderr(
"\
[ERROR] no such subcommand: `invalid-command`

<tab>View all installed commands with `cargo --list`",
)
.run();
}

Expand Down