Skip to content

🐛 fix(subprocess): omit implicit helper import paths - #3416

Merged
gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:fix/helper-import-paths
Oct 17, 2026
Merged

gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:fix/helper-import-paths

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Python starts python -m module and python -c code with an implicit entry for the working directory at the front of sys.path: -m prepends the directory itself, -c prepends an empty string that stands for it. Four of virtualenv's helper processes used those forms, so a pip or venv package, a compileall.py or a virtualenv package in the directory virtualenv runs from was imported ahead of the bootstrap wheel selected through PYTHONPATH, ahead of the standard library, and ahead of the installed virtualenv. The pip seeder, the stdlib creator, the symlink image compiler and the periodic update then ran that code. 🐛 After this PR each helper removes the implicit entry before it imports the module it runs, and each keeps PYTHONPATH, PYTHONHOME and the other explicit configuration it had before.

The pip helpers put the working directory ahead of the selected wheel

pip_wheel_env_run sets PYTHONPATH to the pip wheel the resolver selected, so a helper runs pip out of that archive. The download command from #1841 and the install command from #1462 then ran python -m pip. Python builds the path as the working directory, then PYTHONPATH, then the standard library and site-packages, so a pip package in the working directory won over the wheel. The usual remedies do not fit here. -I implies -E, which drops PYTHONPATH and the wheel with it. -P and PYTHONSAFEPATH drop only the implicit entry, but they exist from Python 3.11 and virtualenv runs on 3.9.

Both commands now run PIP_RUN through -c. The code filters the empty entry out of sys.path, then imports runpy and starts pip the way -m does, through runpy.run_module with run_name='__main__' and alter_sys=True. sys is a built-in module, so nothing is resolved through the path before the filter runs. Where PYTHONSAFEPATH has already omitted the entry, the filter removes nothing and the download and install commands behave the same.

flowchart LR
    M["-m pip"] --> M1["sys.path: cwd, PYTHONPATH wheel, stdlib, site"] --> M2["cwd/pip shadows the wheel"]
    I["-I -m pip"] --> I1["sys.path: stdlib, site"] --> I2["-E dropped PYTHONPATH, wheel gone"]
    P["-P -m pip"] --> P1["sys.path: PYTHONPATH wheel, stdlib, site"] --> P2["Python 3.11 and later only"]
    C["-c PIP_RUN"] --> C1["sys.path: '', PYTHONPATH wheel, stdlib, site"] --> C2["'' removed, then runpy runs pip"]
    classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000;
    classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332;
    classDef other fill:#cfe4ff,stroke:#1d4ed8,color:#0b2a6f;
    class M2,I2 bad;
    class P2 other;
    class C2 ok;
Loading

The stdlib creator imported venv from the working directory

When the target interpreter is not the running one, Venv.get_host_create_cmd ran the host interpreter with -m venv. A venv package in the working directory replaced the standard library module, and the creator ran whatever it contained. The command now uses the same -c form: remove the empty entry, then runpy.run_module('venv', run_name='__main__', alter_sys=True), with the venv arguments after the code. The child still reads PYTHONHOME and the other PYTHON* variables, so a PYTHONHOME that points nowhere fails the creation with ProcessCallFailedError as it did before.

sequenceDiagram
    participant V as virtualenv
    participant H as host python
    participant W as working directory
    participant L as stdlib
    V->>H: -m venv --without-pip ...
    H->>H: sys.path[0] = cwd
    rect rgb(255, 214, 214)
    H->>W: import venv finds venv/__init__.py
    Note over H,W: planted package runs as the creator
    end
    V->>H: -c "filter sys.path, then runpy.run_module('venv')"
    H->>H: sys.path[0] = '' removed
    rect rgb(216, 243, 220)
    H->>L: import venv
    Note over H,L: PYTHONHOME still honoured
    end
Loading

The image compiler ran -m compileall and ignored its exit code

SymlinkPipInstall._generate_new_files compiles the shared image with the new environment's interpreter before the image is made read-only. Since #1480 it ran -m compileall and read the output without checking the exit code. A compileall.py in the working directory ran instead of the standard library module, and a compile that failed left the image without bytecode while seeding reported success. The compile now runs with -S and the -c form. -S skips the site module, so the environment's site-packages stays off the compiler's path, and the filter removes the empty entry before compileall is imported. PYTHONHOME still applies. A non-zero exit raises ProcessCallFailedError with the compiler's output.

flowchart LR
    S[compile the image] --> E{exit code}
    E -- "before: not read" --> Q[image without bytecode, seeding succeeds]
    E -- "now: 0" --> OK[bytecode recorded in RECORD]
    E -- "now: other" --> F[ProcessCallFailedError with stdout and stderr]
    classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000;
    classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332;
    class Q bad;
    class OK,F ok;
Loading

The periodic update bootstrap imported virtualenv from the working directory

trigger_update starts a background interpreter with -c and a script that imports virtualenv.report and do_update (old code, from #1841). With the empty entry first, a virtualenv package in the working directory was the one imported, and the update ran its code in the background with the output discarded. The script now filters sys.path right after import sys and before the first virtualenv import.

sequenceDiagram
    participant P as virtualenv (parent)
    participant C as update child
    participant W as working directory
    participant S as site-packages
    P->>C: python -c "from virtualenv.report import ..."
    rect rgb(255, 214, 214)
    C->>W: sys.path[0] = '' resolves virtualenv/ here
    Note over C: planted package runs unattended
    end
    P->>C: python -c "import sys, filter sys.path, from virtualenv.report import ..."
    rect rgb(216, 243, 220)
    C->>S: '' removed, installed virtualenv imported
    end
Loading

Behavior changes

Helper module selection no longer searches the implicit working-directory entry. The pip download and install helpers, the stdlib creator and the periodic update keep PYTHONPATH, PYTHONHOME and normal site initialization. The symlink-image compiler runs with site initialization disabled, keeps PYTHONHOME, and a failed compile now raises ProcessCallFailedError with the compiler's output instead of leaving the image without bytecode. The helper command lines changed from -m <module> to -c <code>, which shows in log output and in error messages that quote the command.

Reported by Anthropic's OSS Scanner: ANT-2026-E55SP93R.

@gaborbernat
gaborbernat force-pushed the fix/helper-import-paths branch from b92d806 to 517f7da Compare October 16, 2026 14:23
@gaborbernat gaborbernat changed the title 🐛 fix: omit the working directory from helper imports 🐛 fix(subprocess): omit implicit helper import paths Oct 17, 2026
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
The update child imported importlib.util through the implicit
working-directory entry before removing it, so a planted importlib
package in the working directory ran instead. Drop the entry first,
right after importing the built-in sys module, in both the installed
and the zipapp bootstrap. The zipapp child no longer runs isolated,
so it keeps PYTHONHOME and other explicit configuration like the
installed one does.

Carry the working-directory tests and the PIP_RUN wrap from pypa#3416.
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
Carry the working-directory tests and the PIP_RUN wrap from pypa#3416, and
order the pip seeder's detach helpers by first call.
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
get_wheel passes an empty version spec for the bundle version, which
the staged-wheel check fed to Specifier and failed with
InvalidSpecifier, so --download no longer seeded pip with either
seeder. Treat an empty spec like None, cover it in the staged-archive
test, and state the raise contract of download_wheel precisely.

Carry the working-directory tests and the PIP_RUN wrap from pypa#3416.
@gaborbernat
gaborbernat force-pushed the fix/helper-import-paths branch from 7ee9c0c to 4064e74 Compare October 17, 2026 10:11
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
The update child imported importlib.util through the implicit
working-directory entry before removing it, so a planted importlib
package in the working directory ran instead. Drop the entry first,
right after importing the built-in sys module, in both the installed
and the zipapp bootstrap. The zipapp child no longer runs isolated,
so it keeps PYTHONHOME and other explicit configuration like the
installed one does.

Carry the working-directory tests and the PIP_RUN wrap from pypa#3416.
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
Carry the working-directory tests and the PIP_RUN wrap from pypa#3416, and
order the pip seeder's detach helpers by first call.
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
get_wheel passes an empty version spec for the bundle version, which
the staged-wheel check fed to Specifier and failed with
InvalidSpecifier, so --download no longer seeded pip with either
seeder. Treat an empty spec like None, cover it in the staged-archive
test, and state the raise contract of download_wheel precisely.

Carry the working-directory tests and the PIP_RUN wrap from pypa#3416.
@gaborbernat
gaborbernat force-pushed the fix/helper-import-paths branch from 4064e74 to 873793f Compare October 17, 2026 10:20
@gaborbernat
gaborbernat merged commit 2dc8bd8 into pypa:main Oct 17, 2026
74 checks passed
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 2026
get_wheel passes an empty version spec for the bundle version, which
the staged-wheel check fed to Specifier and failed with
InvalidSpecifier, so --download no longer seeded pip with either
seeder. Treat an empty spec like None, cover it in the staged-archive
test, and state the raise contract of download_wheel precisely.

Carry the working-directory tests and the PIP_RUN wrap from pypa#3416.
@gaborbernat
gaborbernat deleted the fix/helper-import-paths 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