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

Fix panic in component list --toolchain stable #3548

Merged
merged 1 commit into from
Dec 2, 2023

Conversation

majaha
Copy link
Contributor

@majaha majaha commented Dec 1, 2023

Closes #3547

Fixes a panic caused by the signature of the parser passed to Claps's Arg.value_parser() having PartialToolchainDesc, not matching ArgMatches.get_one::<ResolvableToolchainName>() in explicit_or_dir_toolchain()

Also, renames explicit_or_dir_toolchain() -> explicit_desc_or_dir_toolchain() and consolidates its use across various CLI subcommands.

Also fixes a similar panic in rustup man --toolchain stable

It seems like Clap is very type-unsafe in this regard. I made a small effort to find other similar problems by eyeball, but there's just so many subcommands to cross-reference... Probably we need to have more rigorous testing of all CLI arguments that have a .value_parser().

Fixes a panic caused by the signature of the parser passed to
Claps's `Arg.value_parser()` having PartialToolchainDesc, not matching
`ArgMatches.get_one::<ResolvableToolchainName>()` in
`explicit_or_dir_toolchain()`

Also, rename `explicit_or_dir_toolchain()` ->
`explicit_desc_or_dir_toolchain()` and consolidate its use across
various CLI subcommands.

Also fixes a similar panic in `rustup man --toolchain stable`
Copy link
Contributor

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me!

How hard would it be to add a unit test for this?

@majaha
Copy link
Contributor Author

majaha commented Dec 1, 2023

I can't think of a way to unit test this.
Essentially the problem is that two different parts of the code need to be type-checked against each other, the .value_parser() bits and the .get_one() bits. But that type checking happens at runtime.
The only way I think we can test that is in the integration tests, by making sure every argument of every subcommand is tested, and checking that it doesn't panic.

@rami3l
Copy link
Member

rami3l commented Dec 1, 2023

It seems like Clap is very type-unsafe in this regard. I made a small effort to find other similar problems by eyeball, but there's just so many subcommands to cross-reference... Probably we need to have more rigorous testing of all CLI arguments that have a .value_parser().

@majaha Do you think migrating to clap_derive will help with that?

I imagine having something like the following will be better for cross-referencing:

#[arg(..., value_parser = ...)]
toolchain: Toolchain,

Type safety-wise I don't know much either, but maybe clap.rs already provides some sort of mechanism to avoid this kind of error? (cc @epage)

PS: The ValueParser selection mechanism looks like a potential candidate to me.

@epage
Copy link

epage commented Dec 1, 2023

One way to unit test is to have clap::Command created by a factory function and wrapper functions around ArgMatches so you can have tests that parse arbitrary arguments and ensure the lookup doesn't panic independent of the rest of the behavior. Cargo does the first half of this but instead focuses on integration tests.

@epage
Copy link

epage commented Dec 1, 2023

As for avoiding this completely, its either don't use value parsers or use the derive with inferred value parsers. Value parsers were primarily designed / optimized for the derive API.

@rami3l
Copy link
Member

rami3l commented Dec 2, 2023

@epage Thanks a lot for the help!

In that case, I suggest merging this one first and, eventually, addressing #3549.

@rami3l rami3l enabled auto-merge (rebase) December 2, 2023 01:38
@rami3l rami3l merged commit d0ec36b into rust-lang:master Dec 2, 2023
16 checks passed
@rami3l rami3l mentioned this pull request Jan 29, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustup component list --toolchain stable panics
4 participants