diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index b605d911fe0..a29b77b3cdf 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -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)); } }; diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 3190e8eafbc..a49f56a4153 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -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` + +View all installed commands with `cargo --list`", ) .run(); } @@ -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` + +View all installed commands with `cargo --list`", + ) .run(); } @@ -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` + +Cargo does not handle `+toolchain` directives. +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` + +Did you mean `build`? + +View all installed commands with `cargo --list`", + ) + .run(); +}