Skip to content

Commit c51ea38

Browse files
committed
fix(cli): Suggest fix for scripts that look like external subcommands
1 parent 4033190 commit c51ea38

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/bin/cargo/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,19 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> C
178178
let command = match path {
179179
Some(command) => command,
180180
None => {
181+
let script_suggestion = if config.cli_unstable().script
182+
&& std::path::Path::new(cmd).is_file()
183+
{
184+
let sep = std::path::MAIN_SEPARATOR;
185+
format!("\n\tTo run the file `{cmd}`, provide a relative path like `.{sep}{cmd}`")
186+
} else {
187+
"".to_owned()
188+
};
181189
let err = if cmd.starts_with('+') {
182190
anyhow::format_err!(
183191
"no such command: `{cmd}`\n\n\t\
184192
Cargo does not handle `+toolchain` directives.\n\t\
185-
Did you mean to invoke `cargo` through `rustup` instead?",
193+
Did you mean to invoke `cargo` through `rustup` instead?{script_suggestion}",
186194
)
187195
} else {
188196
let suggestions = list_commands(config);
@@ -191,7 +199,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> C
191199
anyhow::format_err!(
192200
"no such command: `{cmd}`{did_you_mean}\n\n\t\
193201
View all installed commands with `cargo --list`\n\t\
194-
Find a package to install `{cmd}` with `cargo search cargo-{cmd}`",
202+
Find a package to install `{cmd}` with `cargo search cargo-{cmd}`{script_suggestion}",
195203
)
196204
};
197205

tests/testsuite/script.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ error: no such command: `echo`
109109
110110
<tab>View all installed commands with `cargo --list`
111111
<tab>Find a package to install `echo` with `cargo search cargo-echo`
112+
<tab>To run the file `echo`, provide a relative path like `./echo`
112113
",
113114
)
114115
.run();

0 commit comments

Comments
 (0)