Repository navigation
🐛 fix(subprocess): omit implicit helper import paths - #3416
Merged
Merged
Conversation
This was referenced Oct 16, 2026
gaborbernat
force-pushed
the
fix/helper-import-paths
branch
from
October 16, 2026 14:23
b92d806 to
517f7da
Compare
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
force-pushed
the
fix/helper-import-paths
branch
from
October 17, 2026 10:11
7ee9c0c to
4064e74
Compare
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
force-pushed
the
fix/helper-import-paths
branch
from
October 17, 2026 10:20
4064e74 to
873793f
Compare
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.
This was referenced Nov 20, 2026
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.
Python starts
python -m moduleandpython -c codewith an implicit entry for the working directory at the front ofsys.path:-mprepends the directory itself,-cprepends an empty string that stands for it. Four of virtualenv's helper processes used those forms, so apiporvenvpackage, acompileall.pyor avirtualenvpackage in the directory virtualenv runs from was imported ahead of the bootstrap wheel selected throughPYTHONPATH, 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 keepsPYTHONPATH,PYTHONHOMEand the other explicit configuration it had before.The pip helpers put the working directory ahead of the selected wheel
pip_wheel_env_runsetsPYTHONPATHto 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 ranpython -m pip. Python builds the path as the working directory, thenPYTHONPATH, then the standard library and site-packages, so apippackage in the working directory won over the wheel. The usual remedies do not fit here.-Iimplies-E, which dropsPYTHONPATHand the wheel with it.-PandPYTHONSAFEPATHdrop only the implicit entry, but they exist from Python 3.11 and virtualenv runs on 3.9.Both commands now run
PIP_RUNthrough-c. The code filters the empty entry out ofsys.path, then importsrunpyand starts pip the way-mdoes, throughrunpy.run_modulewithrun_name='__main__'andalter_sys=True.sysis a built-in module, so nothing is resolved through the path before the filter runs. WherePYTHONSAFEPATHhas 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;The stdlib creator imported
venvfrom the working directoryWhen the target interpreter is not the running one,
Venv.get_host_create_cmdran the host interpreter with-m venv. Avenvpackage in the working directory replaced the standard library module, and the creator ran whatever it contained. The command now uses the same-cform: remove the empty entry, thenrunpy.run_module('venv', run_name='__main__', alter_sys=True), with the venv arguments after the code. The child still readsPYTHONHOMEand the otherPYTHON*variables, so aPYTHONHOMEthat points nowhere fails the creation withProcessCallFailedErroras 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 endThe image compiler ran
-m compilealland ignored its exit codeSymlinkPipInstall._generate_new_filescompiles the shared image with the new environment's interpreter before the image is made read-only. Since #1480 it ran-m compilealland read the output without checking the exit code. Acompileall.pyin 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-Sand the-cform.-Sskips thesitemodule, so the environment's site-packages stays off the compiler's path, and the filter removes the empty entry beforecompileallis imported.PYTHONHOMEstill applies. A non-zero exit raisesProcessCallFailedErrorwith 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;The periodic update bootstrap imported
virtualenvfrom the working directorytrigger_updatestarts a background interpreter with-cand a script that importsvirtualenv.reportanddo_update(old code, from #1841). With the empty entry first, avirtualenvpackage in the working directory was the one imported, and the update ran its code in the background with the output discarded. The script now filterssys.pathright afterimport sysand before the firstvirtualenvimport.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 endBehavior 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,PYTHONHOMEand normal site initialization. The symlink-image compiler runs with site initialization disabled, keepsPYTHONHOME, and a failed compile now raisesProcessCallFailedErrorwith 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.