-
Notifications
You must be signed in to change notification settings - Fork 802
Bug fix: Graceful handling of absent clap arguments #5534
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
Conversation
cca2d4a to
25aa166
Compare
25aa166 to
78e7e8e
Compare
|
If at all possible, suggest migrating to clap4 rather than fixing clap3 stuff (unless there is no other way). Also the PR title is worng =) |
Can you point to that crate where that work is? Didn't realize there was an updated version out.
Is it because it includes the word "Bug Fix"? That is there because |
Yes clap has been at v4 for a while and it provides substantially better ergonomics with derive macros, removes around 50% of the code needed to do parsing and is far more maintainable. Vortexor component uses v4 (it might be the first one that was merged). Switching to v4 is only really justified if you are using the derive macros though, basic API might not have improved much.
|
oh! Will go check out the new library and see if it has the same issue. Thanks for pointing that out.
Omg. Random misspellings like this happen to me a lot 😆 |
|
Talked offline. I was a bit confused on the feedback. The |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5534 +/- ##
========================================
Coverage 83.3% 83.3%
========================================
Files 829 829
Lines 373645 373800 +155
========================================
+ Hits 311549 311691 +142
- Misses 62096 62109 +13 🚀 New features to boost your workflow:
|
alexpyattaev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, suggest you also add some "positive" test cases, as now you are only testing for graceful failure. Probably another PR would be apprpriate for that.
Another commit necessary. Found a number of other instances in the crate.
|
@samkim-crypto would be great to get your eyes on this as it looks like you authored a good number of these. This PR attempts to change the opinion on how absent CLI arguments should be handled. I am under the belief if the argument is not present and is required, that should be enforced by the application defining those commands. The utilities are more in line with the spirit of returning Tried my best to add unit tests where possible. However, think we have a bit of technical debt with test coverage generally in this crate. |
|
Yeah this was a source of a lot of headaches for myself too and we had the same discussion about this last year as well (solana-labs#34801 and more precisely solana-labs#34801 (comment)). Can you point me to a downstream location (token-wrap-cli?) where this early termination with Ideally, we want to stick with the clap-v3 style where the downstream apps explicitly handle the |
|
In terms of upgrading to clap-v4, we are planning to skip it and make an upgrade to clap-v5 once it comes out. This is because upgrade from clap-v2 to clap-v3 was a lot of headache. The reason why this part of the solana-clap-v3-utils parsing logic is complicated is that most existing Solana CLI tools were (and many still are) originally written with clap-v2. With v2, the standard way to parse arguments is to use With clap-v3, the This change made it really difficult to upgrade the CLI tools that use clap-v2 to clap-v3. For something like token-cli, the arguments and logic are intertwined in very complicated/subtle ways, so it was difficult to properly handle for |
Clap v5 timeline is probably "very far away": |
Yeah I'd be very happy to see all the Solana cli tools using clap v4. If one is willing to take charge, then I'd be willing to help out as well. I do want to note that the old interface from clap-v2 are just (opt-in) deprecated in v3, while they are entirely removed in v4. This makes it much easier to upgrade clap-v2 -> clap-v3 -> clap-v4 because we can break up PRs into smaller chunks (e.g. solana-labs/solana-program-library#6376, solana-labs/solana-program-library#7447, solana-labs/solana-program-library#7448). Many of the existing solana cli tools still rely on clap-v2 including the main cli, so it might make sense to upgrade these to clap-v3 first before we create another clap-v4 utils and have different cli tools using 3 different versions of clap. All in all, we should probably create a separate github issue for clap version related discussions and focus on just the changes in this PR here. |
Get in touch with @yihau he is already doing some ground work for this in the validator main. |
| .unwrap_err(); | ||
| assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest we add also positive test cases as well. At the very least they would demonstrate how the things are supposed to work. Could be even within the same functions to avoid boilerplate.
The primary motivation for this PR was that these utils are enforcing the presence of fields that should be considered optional. This behavior has caused consumers to completely side-step using them and create optional versions of the same thing (see token-2022 vs clap-v3-utils). The
Fixing the bug above would be sufficient in unblocking my work. However, in general, it feels like an API requiring optional flags to be present or it raises an error is a strictness mismatch. I think querying for the value of an optional flag and it not being present should default to If folks disagree with that or feel it's a separate issue to discuss, I can revert that in this PR and focus just on the bug. |
|
Yeah if it is okay, I would vote for scoping the changes to the bug fix in the Pubkey branch for now to unblock. The general interface change would probably need further discussion and also involve @t-nelson (btw Trent, do you have opinion on this?).
The way things are handled in the token-cli is due to the fact that it was originally written with clap-v2 and
This is a totally fair view and I agree as well. What I am afraid of is that the clap-v3 parsing functions return |
Ah yes, thank you for making that clarification. The issue is specifically that the flag is not defined in ArgMatches, not passed. That said, it's namely subcommand incompatibility that makes this an issue. For instance, in the token-wrap CLI, I define I think that instance kinda makes the argument that a strict global schema doesn't work in all cases as consumers will define subcommands in ways that is hard to anticipate.
Totally, makes sense. Will revert specifically to the changes to |
9543dae to
b6c2921
Compare
b6c2921 to
8cf1f8d
Compare
samkim-crypto
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think this is a completely reasonable change. I am glad that we don't need to do stuff like this after this!
I think that instance kinda makes the argument that a strict global schema doesn't work in all cases as consumers will define subcommands in ways that is hard to anticipate.
I definitely agree. I think generally, clap is nice when we have a standalone cli application, but when we have an upstream cli-specific utility crate that has multiple downstream consumers as we have here, it is often hard to work with. I'd be happy to discuss more about what an ideal API should be for these parsing functions, but let's get the token-wrap CLI unblocked first.
|
Great! Going to add tests for the rest of the conditions in the Pubkey branch of that function and merge. EDIT: Requires a re-review. |
Problem
While working on CLI multsig support for solana-program/token-wrap, I noticed that
signer_from_source_with_configwas throwing during a parse step intended to return an option. The source of the issue was the internal use of:Which throws
MatchesError::UnknownArgumentif a match is not found. Given the helpers return anOption, I believe it is more in the spirit of these to return aNoneinstead of raising an error and will fix the outersigner_from_source_with_configparse bug. This is the strategy employed in token-2022 as well.Summary of Changes
matches.try_get_many::<String>(name).ok().flatten().Ok(None)without erroring or panicking.