From 43507dd96f409e009bcb5b6fed8438eba0a9979f Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 09:56:13 -0500 Subject: [PATCH 1/9] handling if a subcommand looks like `+toolchain`. very early implementation; very basic and not final --- src/bin/cargo/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index b605d911fe0..f4e2c82981a 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -168,7 +168,12 @@ 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.\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) + } else { + anyhow::format_err!("no such subcommand: `{}`{}\n\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) + }; + return Err(CliError::new(err, 101)); } }; From 9665334c6497f4fe99dd2307d6a692d35b1ba747 Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 10:46:03 -0500 Subject: [PATCH 2/9] improve error message for when subcommand contains `+toolchain` --- src/bin/cargo/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index f4e2c82981a..d0775423bff 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -168,8 +168,9 @@ 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 = if cmd.contains('+') { - anyhow::format_err!("no such subcommand: `{}`{}\n\n\tCargo does not handle `+toolchain` directives.\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) + 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?\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) } else { anyhow::format_err!("no such subcommand: `{}`{}\n\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) }; From b55e8d47a94c5ca99e0b2df645bc726795fa07e9 Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 13:01:32 -0500 Subject: [PATCH 3/9] implemented requested changes; fixed one failing test (need to fix other) --- src/bin/cargo/main.rs | 8 ++++++-- tests/testsuite/cargo_command.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index d0775423bff..667b110de8a 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -170,9 +170,13 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c); 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?\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) + 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 { - anyhow::format_err!("no such subcommand: `{}`{}\n\n\tsuggestion: view a list of all installed Cargo subcommands using `cargo --list`", cmd, did_you_mean) + 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)); diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 3190e8eafbc..4732d184de7 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -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\nA similar Cargo subcommand could not be found\nView a list of installed Cargo subcommands using `cargo --list`") .run(); } From b2f44de83d62a2a2a70c471a45a7228989727282 Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 18:23:55 -0500 Subject: [PATCH 4/9] implemented requested changes; tests pass --- src/bin/cargo/main.rs | 14 +++++++------- tests/testsuite/cargo_command.rs | 10 ++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 667b110de8a..2cc3ab897ef 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -166,16 +166,16 @@ 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 = 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) + 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) } 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\tA similar Cargo subcommand could not be found\n\tView a list of installed Cargo subcommands using `cargo --list`", cmd) + anyhow::format_err!("no such subcommand: `{}`\n\n\tView all installed commands with `cargo --list`", cmd) } else { - anyhow::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean) + anyhow::format_err!("no such subcommand: `{}`{}\n\tView all installed commands with `cargo --list`", cmd, did_you_mean) } }; diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 4732d184de7..adbb7d2bec7 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -262,9 +262,8 @@ 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`\n +View all installed commands with `cargo --list`" ) .run(); } @@ -273,7 +272,10 @@ 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\nA similar Cargo subcommand could not be found\nView a list of installed Cargo subcommands using `cargo --list`") + .with_stderr( + "[ERROR] no such subcommand: `invalid-command`\n +View all installed commands with `cargo --list`" + ) .run(); } From 42df87409b66dd5204243df5c81bc044e75f44f3 Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 18:27:36 -0500 Subject: [PATCH 5/9] fix formatting --- tests/testsuite/cargo_command.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index adbb7d2bec7..5219ae8241f 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -263,7 +263,7 @@ fn find_closest_dont_correct_nonsense() { .with_status(101) .with_stderr( "[ERROR] no such subcommand: `there-is-no-way-that-there-is-a-command-close-to-this`\n -View all installed commands with `cargo --list`" +View all installed commands with `cargo --list`", ) .run(); } @@ -274,7 +274,7 @@ fn displays_subcommand_on_error() { .with_status(101) .with_stderr( "[ERROR] no such subcommand: `invalid-command`\n -View all installed commands with `cargo --list`" +View all installed commands with `cargo --list`", ) .run(); } From 5f3ded124f6b4c1f6b39d8967cfb1eb69225b7b6 Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 19:43:16 -0500 Subject: [PATCH 6/9] stop mixing of newlines and `\n` in tests --- tests/testsuite/cargo_command.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 5219ae8241f..8db1c4c7ebd 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -262,7 +262,9 @@ 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`\n + "\ +[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 +275,9 @@ fn displays_subcommand_on_error() { cargo_process("invalid-command") .with_status(101) .with_stderr( - "[ERROR] no such subcommand: `invalid-command`\n + "\ +[ERROR] no such subcommand: `invalid-command` + View all installed commands with `cargo --list`", ) .run(); From 91bc002f52b053c2661561e885311ce41df88db2 Mon Sep 17 00:00:00 2001 From: akabinds Date: Tue, 2 Aug 2022 22:45:51 -0500 Subject: [PATCH 7/9] improve formatting --- src/bin/cargo/main.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 2cc3ab897ef..d09ce74a78a 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -167,16 +167,22 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli Some(command) => command, None => { 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) + anyhow::format_err!( + "no such subcommand: `{}`\n\n\t\ + Cargo does not handle `+toolchain` directives.\n\t\ + Did you mean to run `cargo` through `rustup` instead?", + cmd + ) } 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) - } + 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)); From 8f5f2ed2a2963c16ae9f3e718f610b1106418de8 Mon Sep 17 00:00:00 2001 From: akabinds Date: Wed, 3 Aug 2022 11:07:17 -0500 Subject: [PATCH 8/9] implement test checking if stderr, after using `+toolchain`, contains message stating that the directive is not handled by Cargo --- src/bin/cargo/main.rs | 2 +- tests/testsuite/cargo_command.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index d09ce74a78a..a29b77b3cdf 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -170,7 +170,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli anyhow::format_err!( "no such subcommand: `{}`\n\n\t\ Cargo does not handle `+toolchain` directives.\n\t\ - Did you mean to run `cargo` through `rustup` instead?", + Did you mean to invoke `cargo` through `rustup` instead?", cmd ) } else { diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 8db1c4c7ebd..4bdb71bed2b 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -386,3 +386,14 @@ 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_contains( + "\ +Cargo does not handle `+toolchain` directives.", + ) + .run(); +} From 222d90b78ee9b21f3b1b65f5a03d7c93a78c16bc Mon Sep 17 00:00:00 2001 From: akabinds Date: Wed, 3 Aug 2022 11:38:41 -0500 Subject: [PATCH 9/9] test checking full stderr output, with new message, resulting from a "did you mean" --- tests/testsuite/cargo_command.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 4bdb71bed2b..a49f56a4153 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -391,9 +391,27 @@ fn closed_output_ok() { fn subcommand_leading_plus_output_contains() { cargo_process("+nightly") .with_status(101) - .with_stderr_contains( + .with_stderr( "\ -Cargo does not handle `+toolchain` directives.", +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(); }