Skip to content

Commit

Permalink
Auto merge of #10924 - akabinds:better-no-such-subcommand, r=epage
Browse files Browse the repository at this point in the history
improve error message for `no such subcommand`

Closes #10900
  • Loading branch information
bors committed Aug 3, 2022
2 parents 4fd148c + 222d90b commit 471b80d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
22 changes: 19 additions & 3 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,25 @@ 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\t\
Cargo does not handle `+toolchain` directives.\n\t\
Did you mean to invoke `cargo` through `rustup` instead?",
cmd
)
} else {
let suggestions = list_commands(config);
let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c);

anyhow::format_err!(
"no such subcommand: `{}`{}\n\n\t\
View all installed commands with `cargo --list`",
cmd,
did_you_mean
)
};

return Err(CliError::new(err, 101));
}
};
Expand Down
43 changes: 39 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 Expand Up @@ -380,3 +386,32 @@ fn closed_output_ok() {
assert!(status.success());
assert!(s.is_empty(), "{}", s);
}

#[cargo_test]
fn subcommand_leading_plus_output_contains() {
cargo_process("+nightly")
.with_status(101)
.with_stderr(
"\
error: no such subcommand: `+nightly`
<tab>Cargo does not handle `+toolchain` directives.
<tab>Did you mean to invoke `cargo` through `rustup` instead?",
)
.run();
}

#[cargo_test]
fn full_did_you_mean() {
cargo_process("bluid")
.with_status(101)
.with_stderr(
"\
error: no such subcommand: `bluid`
<tab>Did you mean `build`?
<tab>View all installed commands with `cargo --list`",
)
.run();
}

0 comments on commit 471b80d

Please sign in to comment.