Skip to content

improve error message for no such subcommand #10924

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

Merged
merged 16 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 11 additions & 1 deletion src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,17 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
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.contains('+') {
anyhow::format_err!("no such subcommand: `{}`{}\n\n\tCargo does not handle `+toolchain` directives itself.\n\tDid you mean to run `cargo` through `rustup` instead?", cmd, did_you_mean)
} else {
if did_you_mean.is_empty() {
anyhow::format_err!("no such subcommand: `{}`\n\n\tA similar Cargo subcommand could not be found\n\tView a list of installed Cargo subcommands using `cargo --list`", cmd)
} else {
anyhow::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean)
}
};

return Err(CliError::new(err, 101));
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ 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`\n\n<tab>A similar Cargo subcommand could not be found\n<tab>View a list of installed Cargo subcommands using `cargo --list`")
.run();
}

Expand Down