cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Improve error message for `no such subcommand`

Open CAD97 opened this issue 3 years ago • 3 comments

Problem

With the rustup changes to set $RUSTC et al, it's become more likely that people will accidentally use +toolchain on cargo/rustc/etc directly rather than through the rustup shims. While we shouldn't try to handle the toolchain flag ourselves, it would be beneficial to recognize it in the "no such subcommand" path and print a help note notifying the user that cargo received the +toolchain argument but can't handle it.

〉main:pegcel〉rustup which cargo
D:\.rust\rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe
〉main:pegcel〉D:\.rust\rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe +nightly check
error: no such subcommand: `+nightly`

Proposed Solution

〉main:pegcel〉rustup which cargo
D:\.rust\rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe
〉main:pegcel〉D:\.rust\rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe +nightly check
error: no such subcommand: `+nightly`

        Did you mean to run cargo through rustup?
        Cargo does not handle `+toolchain` directives itself.

Notes

Some rustup context:

  • https://github.com/rust-lang/rustup/issues/3029
  • https://github.com/rust-lang/rustup/issues/3031
  • https://github.com/rust-lang/rustup/issues/3035
  • https://github.com/rust-lang/rustup/issues/3036

CAD97 avatar Jul 25 '22 04:07 CAD97

Thanks for the suggestion! This is essentially a duplicate of #9652. However, since this has more detail I'll close the other issue. However, #9652 contains a few different experience reports.

ehuss avatar Jul 25 '22 04:07 ehuss

The change should IIUC slot in right here:

https://github.com/rust-lang/cargo/blob/85b500ccad8cd0b63995fd94a03ddd4b83f7905b/src/bin/cargo/main.rs#L164-L174

CAD97 avatar Jul 25 '22 04:07 CAD97

Another thought I had that would be useful (unrelated to +toolchain) is to also print a note, recommending the user run cargo --list to get a complete list of subcommands. Otherwise the error can be a bit terse:

> cargo cargo
error: no such subcommand: `cargo`

ehuss avatar Jul 30 '22 18:07 ehuss

@rustbot claim

bindsdev avatar Aug 02 '22 13:08 bindsdev