Skip to content

Commit 471b80d

Browse files
committed
Auto merge of #10924 - akabinds:better-no-such-subcommand, r=epage
improve error message for `no such subcommand` Closes #10900
2 parents 4fd148c + 222d90b commit 471b80d

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

src/bin/cargo/main.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,25 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
166166
let command = match path {
167167
Some(command) => command,
168168
None => {
169-
let suggestions = list_commands(config);
170-
let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c);
171-
let err = anyhow::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean);
169+
let err = if cmd.starts_with('+') {
170+
anyhow::format_err!(
171+
"no such subcommand: `{}`\n\n\t\
172+
Cargo does not handle `+toolchain` directives.\n\t\
173+
Did you mean to invoke `cargo` through `rustup` instead?",
174+
cmd
175+
)
176+
} else {
177+
let suggestions = list_commands(config);
178+
let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c);
179+
180+
anyhow::format_err!(
181+
"no such subcommand: `{}`{}\n\n\t\
182+
View all installed commands with `cargo --list`",
183+
cmd,
184+
did_you_mean
185+
)
186+
};
187+
172188
return Err(CliError::new(err, 101));
173189
}
174190
};

tests/testsuite/cargo_command.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,10 @@ fn find_closest_dont_correct_nonsense() {
262262
.cwd(&paths::root())
263263
.with_status(101)
264264
.with_stderr(
265-
"[ERROR] no such subcommand: \
266-
`there-is-no-way-that-there-is-a-command-close-to-this`
267-
",
265+
"\
266+
[ERROR] no such subcommand: `there-is-no-way-that-there-is-a-command-close-to-this`
267+
268+
<tab>View all installed commands with `cargo --list`",
268269
)
269270
.run();
270271
}
@@ -273,7 +274,12 @@ fn find_closest_dont_correct_nonsense() {
273274
fn displays_subcommand_on_error() {
274275
cargo_process("invalid-command")
275276
.with_status(101)
276-
.with_stderr("[ERROR] no such subcommand: `invalid-command`\n")
277+
.with_stderr(
278+
"\
279+
[ERROR] no such subcommand: `invalid-command`
280+
281+
<tab>View all installed commands with `cargo --list`",
282+
)
277283
.run();
278284
}
279285

@@ -380,3 +386,32 @@ fn closed_output_ok() {
380386
assert!(status.success());
381387
assert!(s.is_empty(), "{}", s);
382388
}
389+
390+
#[cargo_test]
391+
fn subcommand_leading_plus_output_contains() {
392+
cargo_process("+nightly")
393+
.with_status(101)
394+
.with_stderr(
395+
"\
396+
error: no such subcommand: `+nightly`
397+
398+
<tab>Cargo does not handle `+toolchain` directives.
399+
<tab>Did you mean to invoke `cargo` through `rustup` instead?",
400+
)
401+
.run();
402+
}
403+
404+
#[cargo_test]
405+
fn full_did_you_mean() {
406+
cargo_process("bluid")
407+
.with_status(101)
408+
.with_stderr(
409+
"\
410+
error: no such subcommand: `bluid`
411+
412+
<tab>Did you mean `build`?
413+
414+
<tab>View all installed commands with `cargo --list`",
415+
)
416+
.run();
417+
}

0 commit comments

Comments
 (0)