Fix argument parsing for volta run to properly handle flags#1857
Merged
rwjblue merged 1 commit intovolta-cli:mainfrom Aug 16, 2024
Merged
Fix argument parsing for volta run to properly handle flags#1857rwjblue merged 1 commit intovolta-cli:mainfrom
volta run to properly handle flags#1857rwjblue merged 1 commit intovolta-cli:mainfrom
Conversation
Clap's parsing for variable-length argument lists has precedence rules for what happens when a potential vararg overlaps with a known flag value (see https://docs.rs/clap/latest/clap/struct.Arg.html#method.allow_hyphen_values): 1. If we are already in a list of variable args, the args take precedence and any potential flags are ignored (flags are treated as part of the vararg) 2. If we are _not_ already in a list of variable args, then known flags take precedence and are parsed as such, rather than starting the vararg list As a result of this precedence, with `volta run` set up to have `args` be separate from `command`, we could run into parsing issues with commands that have flags that overlap with Volta's. For example: ``` volta run node -h ``` When parsing this, Clap would see `node` as the `command`, and then it would parse `-h` as _Volta's_ `-h` (help) flag and print Volta's help. This is because the variable-length `args` hadn't started yet (we hadn't run into any unknown positional arguments), so Clap treated `-h` as a known flag with higher precedence. This is exactly the opposite of what we actually want for `volta run` Instead, what we want is that everything after the `command` argument should be treated as part of the arguments to the command; regardless of whether the flag overlaps with Volta's flags. To achieve that, we combine `command` and `args` into a _single_ vararg, so that by virtue of having a command in the first place, the vararg has started parsing and Clap will treat any flags as more argument values, rather than as flags.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1852
Info
Clap's parsing for variable-length argument lists has precedence rules for what happens when a potential vararg overlaps with a known flag value (see https://docs.rs/clap/latest/clap/struct.Arg.html#method.allow_hyphen_values):
As a result of this precedence, with
volta runset up to haveargsbe separate fromcommand, we could run into parsing issues with commands that have flags that overlap with Volta's. For example:When parsing this, Clap would see
nodeas thecommand, and then it would parse-has Volta's-h(help) flag and print Volta's help. This is because the variable-lengthargshadn't started yet (we hadn't run into any unknown positional arguments), so Clap treated-has a known flag with higher precedence. This is exactly the opposite of what we actually want forvolta runInstead, what we want is that everything after the
commandargument should be treated as part of the arguments to the command; regardless of whether the flag overlaps with Volta's flags.To achieve that, we combine
commandandargsinto a single vararg, so that by virtue of having a command in the first place, the vararg has started parsing and Clap will treat any flags as more argument values, rather than as flags.Changes
Runto havecommand_and_argsas a single list instead of a value and a list.Command::runso that they can still be passed toexecute_toolin the same manner as beforeTested
volta run node -hand verified that it correctly runsnode -h, rather than showing the help output forvolta runvolta run nodeto make sure the arg processing is handled correctly when there are no additional arguments.volta runto validate that Clap shows a "missing required argument" message rather than creating aRuninstance with an emptycommand_and_argsvector.Note
volta run, however it is a higher priority now because we switched the 3rd-party shims on Windows to usevolta runinternally in Replace Windows symlinks withvolta runscripts #1755