Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A manifest command is suggested for invalid input #13332

Closed
botahamec opened this issue Jan 21, 2024 · 5 comments · Fixed by #13346
Closed

A manifest command is suggested for invalid input #13332

botahamec opened this issue Jan 21, 2024 · 5 comments · Fixed by #13346
Labels
A-cli Area: Command-line interface, option parsing, etc. C-bug Category: bug S-triage Status: This issue is waiting on initial triage. Z-script Nightly: cargo script

Comments

@botahamec
Copy link

Problem

My friend is learning Rust and was given a typo in an example command. She was told to type:

cargo -q run-https://rust-lang.org

Even though what she wanted was:

cargo -q run -- https://rust-lang.org

The error she got told her to use -Zscript, a nightly flag which runs singular Rust files as scripts. I don't know how Cargo came to the conclusion that this was a manifest command, or how it interpreted the space-separated string as one command name, but that's what it did.

error: running `run-https://rust-lang.org` requires `-Zscript`

Steps

  1. Go into any directly
  2. Run cargo -q run - https://rust-lang.org

Possible Solution(s)

This should probably just give an error, saying that it's not a valid command. Maybe a help message suggesting spaces.

Notes

No response

Version

cargo 1.75.0 (1d8b05cdd 2023-11-20)
release: 1.75.0
commit-hash: 1d8b05cdd1287c64467306cf3ca2c8ac60c11eb0
commit-date: 2023-11-20
host: x86_64-unknown-linux-gnu
libgit2: 1.7.1 (sys:0.18.1 vendored)
libcurl: 8.4.0-DEV (sys:0.4.68 curl-8.4.0 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Ubuntu 22.04 (jammy) [64-bit]
@botahamec botahamec added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Jan 21, 2024
@epage
Copy link
Contributor

epage commented Jan 22, 2024

Note that the alternative would have been that no such subcommand exists.

$ cargo run-gfdklfgjdfjkgjkldg
error: no such command: `run-gfdklfgjdfjkgjkldg`

        View all installed commands with `cargo --list`
        Find a package to install `run-gfdklfgjdfjkgjkldg` with `cargo search cargo-run-gfdklfgjdfjkgjkldg`

The reason we gave the manifest command error, instead of no-such-subcommand error, is because of the slashes in the argument. We interpreted that as a path (seeing as those are illegal for subcommand names).

For myself, I'm having a hard time seeing how this kind of situation would be common enough for us to detect and report for. This requires a lack of spaces between the subcommand name and wherever a path-like may show up. Cases besides cargo run with -- and a path-like would be like check--manifest-path=foo/Cargo.toml seem rare.

@epage epage added A-cli Area: Command-line interface, option parsing, etc. Z-script Nightly: cargo script labels Jan 22, 2024
@weihanglo
Copy link
Member

People might have an alias test-runner and they somehow forgot the space in bewteen:

$ cargo test-runner./snapshots
error: running `test-runner./snapshots` requires `-Zscript`

The message is not ideal for new Rust users to identify what's going on, as it points to a nightly feature they don't know.

Maybe we could give the “no such subcommand” error still, with a bit richer message for -Zscript, so people don't know it can learn about its existence :)

$ cargo test-runner./snapshots
warning: no such subcommand: `test-runner./snapshots`

error the `cargo <single-file-package>` command is unstable, and only available on the nightly channel of Cargo, but this is the `beta` channel
See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information about Rust release channels.
See https://github.com/rust-lang/cargo/issues/12207 for more information about the `cargo <single-file-package>` command.
patchfile

diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs
index 170c7ddf1a0..f8bdc8a0f10 100644
--- a/src/bin/cargo/commands/run.rs
    b/src/bin/cargo/commands/run.rs
@@ -97,8  97,15 @@ pub fn is_manifest_command(arg: &str) -> bool {
 }
 
 pub fn exec_manifest_command(config: &mut Config, cmd: &str, args: &[OsString]) -> CliResult {
-    if !config.cli_unstable().script {
-        return Err(anyhow::anyhow!("running `{cmd}` requires `-Zscript`").into());
     if let Err(e) = config.cli_unstable().fail_if_stable_command(
         config,
         "<single-file-package>",
         12207,
         "script",
         config.cli_unstable().script,
     ) {
         config.shell().warn(format_args!("no such subcommand: {cmd}\n"))?;
         return Err(e.into());
     }
 
     let manifest_path = Path::new(cmd);

Or maybe other ad-hoc message can help

@epage
Copy link
Contributor

epage commented Jan 25, 2024

I'm fine with providing some way to combine the messages pre-stabilization. My comment was more looking forward to when we do stabilize. How should the error look in that case? I feel like we should start there and work backwards or else we'll just mask this until stabilization and then regress.

@botahamec
Copy link
Author

@epage What if we check to make sure the path actually exists first? Then we could say something like we interpreted the command as an attempt to run a script, but the file doesn't exist.

@weihanglo
Copy link
Member

It's hard to guess the intent of a user, and “no such subcommand” is a good choice in the meantime when waiting for feedback from others.

Some ideas:

  • Split the invalid subcommand name by known subcommands, and give a bold guess like did you mean "cargo test-runner ./snapshots"?
  • Walk the current directory, and find if there is a file with a similar name of "test-runner./snapshots".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: Command-line interface, option parsing, etc. C-bug Category: bug S-triage Status: This issue is waiting on initial triage. Z-script Nightly: cargo script
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants