-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
External subcommands that alias built-in subccommands cause panics #3263
Comments
So my thoughts
Unsure what I'll do for cargo to maintain behavior. |
A big problem with detecting this is the fact that we only populate Without a way to detect this until 4.0:
So the choice is go straight for 4.0 already or apply a surgical hack to unblock cargo until 4.0. I'm leaning towards the surgical hack. |
This is a surgical workaround for clap-rs#3263. It makes `cargo` pass tests!
This is a surgical workaround for clap-rs#3263. It makes `cargo` pass tests!
This is a surgical workaround for clap-rs#3263. It makes `cargo` pass tests!
- One parser change found by `cargo_config::includes` is that clap 2 would ignore any values after a `=` for flags. `cargo config --show-origin` is a flag but the test passed `--show-origin=yes` which happens to give the desired result for that test but is the same as `--show-origin=no` or `--show-origin=alien-invasion`. - The parser now panics when accessing an undefined attribute but clap takes advantage of that for sharing code across commands that have different subsets of arguments defined. I've extended clap so we can "look before you leap" and put the checks at the argument calls to start off with so its very clear what is tenuously shared. This allows us to go in either direction in the future, either addressing how we are sharing between commands or by moving this down into the extension methods and pretending this clap feature doesn't exist - On that topic, a test found clap-rs/clap#3263. For now, there is a hack in clap. Depending on how we fix that in clap for clap 4.0, we might need to re-address things in cargo. - `value_of_os` now requires setting `allow_invalid_utf8`, otherwise it asserts. To help catch this, I updated the argument definitions associated with lookups reported by: - `rg 'values?_os' src/` - `rg 'values?_of_os' src/` - clap now reports `2` for usage errors, so we had to bypass clap's `exit` call to keep the same exit code. BREAKING CHANGE: API now uses clap3
Port cargo to clap3 ### What does this PR try to resolve? This moves cargo to the latest major version of clap. This supersedes #10259 and #10262 ### How should we test and review this PR? For testing, I mostly relied on existing tests. I did manually validate that `cargo run <non-escaped command args>` behaved the same between both ```console $ cargo run release --help Finished dev [unoptimized + debuginfo] target(s) in 0.22s Running `target/debug/cargo-release release --help` cargo-release 0.18.8 ... $ cargo run --manifest-path ../cargo/Cargo.toml -- run release --help Finished dev [unoptimized + debuginfo] target(s) in 0.05s Running `/home/epage/src/personal/cargo/target/debug/cargo run release --help` Finished dev [unoptimized + debuginfo] target(s) in 1.31s Running `target/debug/cargo-release release --help` cargo-release 0.18.8 ... ``` For reviewing, I split out each deprecation resolution into a separate commit so its easy to focus on more mechanical changes (most of the deprecation fixes) from interesting ones (the port, the `Arg::multiple` deprecation) ### Additional information - One parser change found by `cargo_config::includes` is that clap 2 would ignore any values after a `=` for flags. `cargo config --show-origin` is a flag but the test passed `--show-origin=yes` which happens to give the desired result for that test but is the same as `--show-origin=no` or `--show-origin=alien-invasion`. - `ArgMatches` now panics when accessing an undefined argument but clap takes advantage of that for sharing code across commands that have different subsets of arguments defined. I've extended clap so we can "look before you leap" and put the checks at the argument calls to start off with so its very clear what is tenuously shared. This allows us to go in either direction in the future, either addressing how we are sharing between commands or by moving this down into the extension methods and pretending this clap feature doesn't exist - On that topic, a test found clap-rs/clap#3263. For now, there is a hack in clap. Depending on how we fix that in clap for clap 4.0, we might need to re-address things in cargo. - `value_of_os` now requires setting `allow_invalid_utf8`, otherwise it asserts. To help catch this, I updated the argument definitions associated with lookups reported by: - `rg 'values?_os' src/` - `rg 'values?_of_os' src/` - clap now reports `2` for usage errors, so we had to bypass clap's `exit` call to keep the same exit code. - `cargo vendor --sync` did not use `multi_opt` and so it has both multiple occurrences **and** multiple values. If we want to deprecate this, we'll need `unstable-grouped` to be stablized (or pin our clap version) and ensure each group has only 1 value.
This allows distinguishing external subcommands from built-in subcommands which can especially be confusing when escaping subcommands. Fixes clap-rs#3263
Please complete the following tasks
Rust Version
rustc 1.57.0 (f1edd0429 2021-11-29)
Clap Version
master
Minimal reproducible code
Steps to reproduce the bug with the above code
See below
Actual Behaviour
This happens because
--
is escaping the command so it becomes an external subcommand that aliases the built-in. A simplematch
does not catch this and we then try to access"NAME"
in anArgMatches
meant for external subcommands (only""
exists)Expected Behaviour
Its a bit unclear.
cargo
just passes theArgMatches
along, warning about the contents, like:So with the above problem, that'd look like:
Additional Context
Ran into this when porting cargo to clap3 with a test failure from
cargo_alias_config::weird_check
.cargo
takes the approach of running the escaped command but ignoring the arguments. This means we pass an external subcommand'sArgMatches
into a function assuming its gettingcheck
sArgMatches
and panics when it accesses an "undefined" field.Builder users can detect that its an external subcommand by checking for
values_of("")
but derive users can't do that.You have to even know to do that.
Once you do that, the question is what is next. Pass it through? Error? Fallback to the built-in like cargo? Seems like this should be the users choice. The challenge is with the fallback. They now need an
ArgMatches
that has the same shape (ie won't panic) when passing to that part of their program.Debug Output
No response
The text was updated successfully, but these errors were encountered: