Skip to content

🐛 fix(cli): validate arguments before discovery - #3410

Merged
gaborbernat merged 7 commits into
pypa:mainfrom
gaborbernat:fix/early-option-validation
Oct 17, 2026
Merged

gaborbernat merged 7 commits into
pypa:mainfrom
gaborbernat:fix/early-option-validation

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

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 X after the broken option, the query ran against X and 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_parser registers the command line in stages, and each stage runs parse_known_args over the whole argument list: verbosity, then the app-data options, then discovery, then the creator, seeder and activator groups. session_via_cli runs the final parse_args once every option exists. parse_known_args moves an unknown option-like string into the extras list and carries on, so in the app-data stage --prompt was an extra and --app-data cache was consumed: its type callback, make_app_data, created the directory. The option has been registered in that stage since #1841. In the discovery stage --python X was consumed and discover.interpreter ran the discovery, which spawned X to query it and cached the answer. The activator stage registered --prompt last. Only the final parse_args saw --prompt followed by --app-data, which argparse reads as the next option and never as a value (arguments containing -), and exited 2 with argument --prompt: expected one argument.

_validate_option_values now runs first in build_parser, before the VirtualEnvConfigParser exists. It is a plain ArgumentParser that knows -v and -q as counters, so a cluster such as -vp X splits 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, --wheel and --prompt. Its parse_known_args leaves 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 X passes both. A plugin switch that is a strict prefix of one of these names, a --prom flag 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
    end
Loading

Behavior 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.

@gaborbernat
gaborbernat force-pushed the fix/early-option-validation branch from 53e5696 to 3fb9943 Compare October 16, 2026 14:23
@gaborbernat
gaborbernat force-pushed the fix/early-option-validation branch from 3fb9943 to ee6c8af Compare October 16, 2026 14:25
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.
@gaborbernat gaborbernat changed the title 🐛 fix(cli): reject missing values before setup 🐛 fix(cli): validate arguments before discovery Oct 17, 2026
@gaborbernat
gaborbernat merged commit 94efa83 into pypa:main Oct 17, 2026
76 checks passed
@gaborbernat
gaborbernat deleted the fix/early-option-validation branch October 17, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant