Repository navigation
🐛 fix(cli): validate arguments before discovery - #3410
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/early-option-validation
branch
from
October 16, 2026 14:23
53e5696 to
3fb9943
Compare
Reported by Anthropic's OSS Scanner: ANT-2026-LSDKPSV9.
gaborbernat
force-pushed
the
fix/early-option-validation
branch
from
October 16, 2026 14:25
3fb9943 to
ee6c8af
Compare
Requiring --python before options registered later rejected the argument order nox (`<dest> --no-periodic-update -p X`) and pipenv (`--prompt=N --python=X <dest>`) emit, along with forms such as `--clear -p X`. argparse never treats a following option as the value of an option that lacks one, so the order only changed when the missing-value error was reported; plugin options are validated once their plugin is registered.
6 of 9 tasks
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.
A built-in option written without its value, as in
virtualenv --prompt --app-data cache env, exited 2 only after virtualenv had created the app-data directory (the cache where it keeps interpreter query results and seed wheels) and had spawned the target interpreter to query it. With--python Xafter the broken option, the query ran againstXand its result landed in the new cache. 🐛 After this PR a built-in option that takes a value and is followed by another option exits 2 before the parser that creates the cache is built and before any interpreter is spawned.Staged parsing acted on later options before it knew the earlier one
build_parserregisters the command line in stages, and each stage runsparse_known_argsover the whole argument list: verbosity, then the app-data options, then discovery, then the creator, seeder and activator groups.session_via_cliruns the finalparse_argsonce every option exists.parse_known_argsmoves an unknown option-like string into the extras list and carries on, so in the app-data stage--promptwas an extra and--app-data cachewas consumed: itstypecallback,make_app_data, created the directory. The option has been registered in that stage since #1841. In the discovery stage--python Xwas consumed anddiscover.interpreterran the discovery, which spawnedXto query it and cached the answer. The activator stage registered--promptlast. Only the finalparse_argssaw--promptfollowed by--app-data, which argparse reads as the next option and never as a value (arguments containing-), and exited 2 withargument --prompt: expected one argument._validate_option_valuesnow runs first inbuild_parser, before theVirtualEnvConfigParserexists. It is a plainArgumentParserthat knows-vand-qas counters, so a cluster such as-vp Xsplits the way the full parser splits it, and the thirteen built-in options that take a value:--app-data,--discovery,-p,--python,--try-first-with,--creator,--seeder,--activators,--extra-search-dir,--pip,--setuptools,--wheeland--prompt. Itsparse_known_argsleaves every other option and the destination in the extras, and it reports a value option followed by another option with the same message and exit code as the full parser, from the same argparse rule. Abbreviation matching (allow_abbrev) stays on, as in the full parser, so--prom Xpasses both. A plugin switch that is a strict prefix of one of these names, a--promflag say, followed by another option is rejected here although the full parser would accept it; no known plugin registers such a flag. An option a plugin adds is checked once the plugin has registered it, in the stage after discovery, as before.The order of the options is not part of the check. argparse never takes a following
-option as a value, so an earlier commit of this PR that required the discovery options to come before later-registered ones only moved where the error was reported, and it rejected the forms nox (env --no-periodic-update -p X) and pipenv (--creator=venv --prompt=N --python=X env) emit. That commit is reverted and both forms are tested, along with the cache and subprocess that must not appear.sequenceDiagram participant U as command line participant P as pre-parser participant A as app-data stage participant D as discovery stage participant C as component stages participant F as final parse_args U->>P: --prompt --app-data cache --python X env rect rgb(255, 214, 214) Note over A: before: --prompt unknown, kept as an extra A->>A: --app-data cache: make_app_data creates the directory D->>D: --python X: spawn X, cache the query result C->>C: register --prompt F-->>U: argument --prompt: expected one argument, exit 2 end rect rgb(216, 243, 220) P-->>U: now: argument --prompt: expected one argument, exit 2 Note over P: no parser built, no directory, no subprocess endBehavior changes
A built-in option that takes a value and is followed by another option fails with exit code 2 before the app-data directory is created and before interpreter discovery starts. The usage line printed with that error lists the options the pre-parser knows, not the full set. A plugin option with a missing value is still reported after its plugin has registered it. Option order is unchanged: the nox and pipenv forms above still create the environment.
Reported by Anthropic's OSS Scanner: ANT-2026-LSDKPSV9.