Skip to content

Commit 6196d35

Browse files
authored
🐛 fix(seed): retain the parent installation for updates (#3420)
1 parent fd3f5e6 commit 6196d35

12 files changed

Lines changed: 337 additions & 134 deletions

File tree

‎docs/changelog/3416.bugfix.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Keep helper processes for pip, symlink-seed compilation, stdlib venv creation, and periodic wheel updates from importing
2+
modules through Python's implicit current-directory entry. Pip helpers retain the selected bootstrap wheel on their
3+
import path.
4+
5+
Preserve Python environment-variable overrides for delegated stdlib venv creation while removing the implicit
6+
working-directory import entry.

‎docs/changelog/3420.bugfix.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Start periodic wheel updates with the same virtualenv installation or zipapp that scheduled them, even when the host
2+
interpreter has another virtualenv version installed or no installed copy.
3+
4+
Keep standard-library precedence and explicit Python configuration in update processes, and drop only the implicit
5+
working-directory import entry before importing anything.

‎src/virtualenv/create/via_global_ref/venv.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ def create_via_sub_process(self) -> None:
9292
raise ProcessCallFailedError(code, out, err, cmd)
9393

9494
def get_host_create_cmd(self) -> list[str]:
95-
cmd = [self.interpreter.system_executable, "-m", "venv", "--without-pip"]
95+
cmd = [
96+
self.interpreter.system_executable,
97+
"-c",
98+
(
99+
"import sys; sys.path[:] = [p for p in sys.path if p != '']; "
100+
"import runpy; runpy.run_module('venv', run_name='__main__', alter_sys=True)"
101+
),
102+
"--without-pip",
103+
]
96104
if self.interpreter.version_info >= (3, 13):
97105
cmd.append("--without-scm-ignore-files")
98106
if self.enable_system_site_package:

‎src/virtualenv/seed/embed/pip_invoke.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from virtualenv.seed.embed.base_embed import BaseEmbed
99
from virtualenv.seed.wheels import Version, get_wheel, pip_wheel_env_run
10+
from virtualenv.seed.wheels.acquire import PIP_RUN
1011
from virtualenv.util.subprocess import LogCmd
1112

1213
if TYPE_CHECKING:
@@ -45,8 +46,8 @@ def _execute(cmd: list[str], env: dict[str, str]) -> Popen[bytes]:
4546
def get_pip_install_cmd(self, exe: Path, for_py_version: str) -> Generator[list[str], None, None]:
4647
cmd = [
4748
str(exe),
48-
"-m",
49-
"pip",
49+
"-c",
50+
PIP_RUN,
5051
"-q",
5152
"install",
5253
"--only-binary",

‎src/virtualenv/seed/wheels/acquire.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
LOGGER = logging.getLogger(__name__)
2222

23+
PIP_RUN: Final[str] = (
24+
"import sys; sys.path = [p for p in sys.path if p]; "
25+
"import runpy; runpy.run_module('pip', run_name='__main__', alter_sys=True)"
26+
)
27+
2328
# A private index can legitimately serve a rebuilt wheel under the same distribution, version, and
2429
# filename PyPI also uses - an internal mirror re-signing or re-packaging a release for policy reasons,
2530
# for example. Verifying that file against PyPI's own digest would then reject a wheel that came from
@@ -144,8 +149,8 @@ def download_wheel( # ruff:ignore[too-many-arguments]
144149
LOGGER.debug("download wheel %s %s to %s", to_download, for_py_version, to_folder)
145150
cmd = [
146151
sys.executable,
147-
"-m",
148-
"pip",
152+
"-c",
153+
PIP_RUN,
149154
"download",
150155
"--progress-bar",
151156
"off",
@@ -240,6 +245,7 @@ def _check_version_spec(version_spec: str | None) -> None:
240245

241246

242247
__all__ = [
248+
"PIP_RUN",
243249
"download_wheel",
244250
"find_compatible_in_house",
245251
"get_wheel",

‎src/virtualenv/seed/wheels/periodic_update.py‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pathlib import Path
1515
from shutil import copy2
1616
from subprocess import DEVNULL, Popen
17-
from textwrap import dedent
17+
from textwrap import dedent, indent
1818
from threading import Thread
1919
from time import sleep
2020
from typing import TYPE_CHECKING, Final
@@ -24,6 +24,7 @@
2424
from virtualenv.app_data import AppDataDiskFolder
2525
from virtualenv.config.convert import BoolType
2626
from virtualenv.config.env_var import get_env_var
27+
from virtualenv.info import IS_ZIPAPP, ROOT
2728
from virtualenv.seed.wheels.embed import BUNDLE_SUPPORT
2829
from virtualenv.seed.wheels.util import Wheel
2930
from virtualenv.util.subprocess import CREATE_NO_WINDOW
@@ -236,20 +237,33 @@ def trigger_update( # ruff:ignore[too-many-arguments]
236237
periodic: bool,
237238
) -> None:
238239
wheel_path = None if wheel is None else str(wheel.path)
239-
cmd = [
240-
sys.executable,
241-
"-c",
240+
code = (
242241
dedent(
243-
"""
242+
"""\
244243
from virtualenv.report import setup_report, MAX_LEVEL
245244
from virtualenv.seed.wheels.periodic_update import do_update
246245
setup_report(MAX_LEVEL, show_pid=True)
247246
do_update({!r}, {!r}, {!r}, {!r}, {!r}, {!r})
248-
""",
247+
"""
249248
)
250249
.strip()
251-
.format(distribution, for_py_version, wheel_path, str(app_data), [str(p) for p in search_dirs], periodic),
252-
]
250+
.format(distribution, for_py_version, wheel_path, str(app_data), [str(p) for p in search_dirs], periodic)
251+
)
252+
if IS_ZIPAPP:
253+
code = f"import runpy\nwith runpy.run_path({ROOT!r}, run_name='__loader__')['VersionedFindLoad']():\n" + indent(
254+
code, " "
255+
)
256+
else:
257+
code = (
258+
"import importlib.util\n"
259+
f"spec = importlib.util.spec_from_file_location('virtualenv', {str(Path(ROOT) / 'virtualenv' / '__init__.py')!r})\n"
260+
"package = importlib.util.module_from_spec(spec)\n"
261+
"sys.modules['virtualenv'] = package\n"
262+
"spec.loader.exec_module(package)\n"
263+
f"{code}"
264+
)
265+
# drop the implicit working-directory entry before anything else is imported, as sys is built in
266+
cmd: Final = [sys.executable, "-c", f"import sys\nsys.path = [p for p in sys.path if p]\n{code}"]
253267
debug = env.get("_VIRTUALENV_PERIODIC_UPDATE_INLINE") == "1"
254268
pipe = None if debug else DEVNULL
255269
kwargs = {"stdout": pipe, "stderr": pipe}

‎tasks/__main__zipapp.py‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
ABS_HERE = os.path.abspath(os.path.dirname(__file__))
1919

2020

21+
def run() -> None:
22+
with VersionedFindLoad():
23+
from virtualenv.__main__ import run as run_virtualenv # ruff:ignore[import-outside-top-level]
24+
25+
run_virtualenv()
26+
27+
2128
class VersionPlatformSelect:
2229
def __init__(self) -> None:
2330
zipapp = ABS_HERE
@@ -165,6 +172,19 @@ def locate_file(self, path: str) -> str:
165172

166173

167174
class VersionedFindLoad(VersionPlatformSelect, SourceLoader):
175+
def __enter__(self) -> Self:
176+
sys.path.insert(0, self.archive)
177+
sys.meta_path.insert(0, self)
178+
self._register_distutils_finder()
179+
return self
180+
181+
def __exit__(
182+
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
183+
) -> None:
184+
sys.meta_path.remove(self)
185+
sys.path.remove(self.archive)
186+
super().__exit__(exc_type, exc_val, exc_tb)
187+
168188
def find_spec(self, fullname: str, path: Any, target: ModuleType | None = None) -> ModuleSpec | None: # ruff:ignore[unused-method-argument, any-type]
169189
zip_path = self.find_mod(fullname)
170190
if zip_path is not None:
@@ -175,14 +195,8 @@ def module_repr(self, module: ModuleType) -> str:
175195
raise NotImplementedError
176196

177197

178-
def run() -> None:
179-
with VersionedFindLoad() as finder:
180-
sys.meta_path.insert(0, finder)
181-
finder._register_distutils_finder() # ruff:ignore[private-member-access]
182-
from virtualenv.__main__ import run as run_virtualenv # ruff:ignore[import-outside-top-level]
183-
184-
run_virtualenv()
185-
186-
187198
if __name__ == "__main__":
188199
run()
200+
201+
202+
__all__ = ["VersionedFindLoad", "run"]

‎tests/integration/test_zipapp.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
from __future__ import annotations
22

3+
import importlib.util
4+
import os
35
import shutil
46
import subprocess
7+
import sys
58
import zipfile
69
from contextlib import suppress
710
from pathlib import Path
11+
from typing import Final
812

913
import pytest
1014
from python_discovery import PythonInfo
1115

16+
from virtualenv.app_data import AppDataDiskFolder
1217
from virtualenv.info import fs_supports_symlink
1318
from virtualenv.run import cli_run
19+
from virtualenv.seed.wheels.periodic_update import trigger_update
1420

1521
HERE = Path(__file__).parent
1622
CURRENT = PythonInfo.current_system()
23+
# Repackaged sdists omit the zipapp build sources.
24+
_ZIPAPP_MAIN: Final = pytest.importorskip("tasks.__main__zipapp")
1725

1826

1927
@pytest.fixture(scope="session")
@@ -84,6 +92,75 @@ def zipapp_test_env(tmp_path_factory):
8492
shutil.rmtree(str(base_path))
8593

8694

95+
@pytest.mark.timeout(600)
96+
def test_zipapp_loader_context(zipapp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
97+
monkeypatch.setattr("tasks.__main__zipapp.ABS_HERE", str(zipapp))
98+
monkeypatch.delitem(sys.modules, "filelock", raising=False)
99+
with _ZIPAPP_MAIN.VersionedFindLoad():
100+
spec: Final = importlib.util.find_spec("filelock")
101+
assert spec is not None
102+
assert spec.origin is not None
103+
assert spec.origin.startswith(str(zipapp))
104+
restored: Final = importlib.util.find_spec("filelock")
105+
assert restored is not None
106+
assert restored.origin is not None
107+
assert not restored.origin.startswith(str(zipapp))
108+
109+
110+
@pytest.mark.timeout(600)
111+
def test_zipapp_version(zipapp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
112+
monkeypatch.setattr("tasks.__main__zipapp.ABS_HERE", str(zipapp))
113+
monkeypatch.setattr(sys, "argv", [str(zipapp), "--version"])
114+
with pytest.raises(SystemExit) as context:
115+
_ZIPAPP_MAIN.run()
116+
assert context.value.code == 0
117+
118+
119+
@pytest.mark.timeout(600)
120+
def test_zipapp_update_command(
121+
zipapp: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
122+
) -> None:
123+
monkeypatch.setattr("virtualenv.seed.wheels.periodic_update.IS_ZIPAPP", True)
124+
monkeypatch.setattr("virtualenv.seed.wheels.periodic_update.ROOT", str(zipapp))
125+
monkeypatch.setenv("PIP_NO_INDEX", "1")
126+
trigger_update(
127+
"pip",
128+
f"{sys.version_info.major}.{sys.version_info.minor}",
129+
None,
130+
[],
131+
AppDataDiskFolder(str(tmp_path / "data")),
132+
{"_VIRTUALENV_PERIODIC_UPDATE_INLINE": "1"},
133+
True,
134+
)
135+
assert "done pip" in capfd.readouterr().out
136+
137+
138+
@pytest.mark.timeout(600)
139+
def test_zipapp_update_without_installed_package(zipapp: Path, zipapp_test_env: Path, tmp_path: Path) -> None:
140+
result: Final = subprocess.run(
141+
[
142+
str(zipapp_test_env),
143+
"-I",
144+
"-c",
145+
(
146+
f"import runpy\nwith runpy.run_path({str(zipapp)!r}, run_name='__loader__')['VersionedFindLoad']():\n"
147+
" import sys\n"
148+
" from virtualenv.app_data import AppDataDiskFolder\n"
149+
" from virtualenv.seed.wheels.periodic_update import trigger_update\n"
150+
f" trigger_update('pip', f'{{sys.version_info.major}}.{{sys.version_info.minor}}', None, [], "
151+
f"AppDataDiskFolder({str(tmp_path / 'data')!r}), {{'_VIRTUALENV_PERIODIC_UPDATE_INLINE': '1'}}, True)\n"
152+
),
153+
],
154+
env={**os.environ, "PIP_NO_INDEX": "1"},
155+
capture_output=True,
156+
text=True,
157+
encoding="utf-8",
158+
timeout=60,
159+
check=True,
160+
)
161+
assert "done pip" in result.stdout
162+
163+
87164
@pytest.fixture
88165
def call_zipapp(zipapp, tmp_path, zipapp_test_env, temp_app_data): # ruff:ignore[unused-function-argument]
89166
def _run(*args) -> None:

‎tests/unit/create/test_creator.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from virtualenv.info import IS_PYPY, IS_WIN, fs_is_case_sensitive, fs_supports_symlink
3535
from virtualenv.run import cli_run, session_via_cli
3636
from virtualenv.run.plugin.creators import CreatorSelector
37+
from virtualenv.util.error import ProcessCallFailedError
3738

3839
if TYPE_CHECKING:
3940
from collections.abc import Callable
@@ -461,6 +462,23 @@ def test_venv_configuration_uses_selected_options(tmp_path: Path, global_package
461462
assert {key: session.creator.pyenv_cfg.content.get(key) for key in expected} == expected
462463

463464

465+
@pytest.mark.skipif(not CURRENT.has_venv, reason="requires interpreter with venv")
466+
def test_venv_subprocess_skips_local_venv_module(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
467+
(tmp_path / "venv").mkdir()
468+
(tmp_path / "venv" / "__init__.py").write_text("raise SystemExit('imported from the working directory')", "utf-8")
469+
monkeypatch.chdir(tmp_path)
470+
session: Final = cli_run([
471+
"-p",
472+
str(CURRENT.executable),
473+
str(tmp_path / "env"),
474+
"--without-pip",
475+
"--creator",
476+
"venv",
477+
"--no-periodic-update",
478+
])
479+
assert (session.creator.dest / "pyvenv.cfg").is_file()
480+
481+
464482
@pytest.mark.skipif(not CURRENT.has_venv, reason="requires interpreter with venv")
465483
def test_venv_fails_not_inline(tmp_path, capsys, mocker) -> None:
466484
if hasattr(os, "geteuid") and os.geteuid() == 0:
@@ -1158,3 +1176,12 @@ def test_pyenv_cfg_write_logs_escaped_values(tmp_path: Path, caplog: pytest.LogC
11581176
caplog.set_level(logging.DEBUG)
11591177
PyEnvCfg(OrderedDict({"prompt": "a\x1bb"}), tmp_path / "pyvenv.cfg").write()
11601178
assert "\t'prompt = \"a\\x1bb\"'" in caplog.text
1179+
1180+
1181+
@pytest.mark.skipif(not CURRENT.has_venv, reason="requires interpreter with venv")
1182+
@pytest.mark.skipif(CURRENT.implementation != "CPython", reason="only CPython rejects an invalid PYTHONHOME")
1183+
def test_venv_subprocess_respects_pythonhome(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
1184+
session = session_via_cli(["-p", str(CURRENT.executable), str(tmp_path / "env"), "--no-seed", "--creator", "venv"])
1185+
monkeypatch.setenv("PYTHONHOME", str(tmp_path / "missing-home"))
1186+
with pytest.raises(ProcessCallFailedError):
1187+
session.run()

‎tests/unit/seed/embed/test_pip_invoke.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from __future__ import annotations
22

33
import itertools
4+
import os
5+
import subprocess
46
import sys
57
from shutil import copy2
8+
from typing import TYPE_CHECKING, Final
69

710
import pytest
811

@@ -11,6 +14,37 @@
1114
from virtualenv.seed.wheels.bundle import load_embed_wheel
1215
from virtualenv.seed.wheels.embed import BUNDLE_FOLDER, BUNDLE_SUPPORT
1316

17+
if TYPE_CHECKING:
18+
from pathlib import Path
19+
20+
21+
@pytest.mark.parametrize("safe_path", [pytest.param("", id="default"), pytest.param("1", id="safe-path")])
22+
def test_pip_invoke_uses_selected_wheel(
23+
tmp_path: Path, current_fastest: str, monkeypatch: pytest.MonkeyPatch, safe_path: str
24+
) -> None:
25+
monkeypatch.setenv("PYTHONSAFEPATH", safe_path)
26+
(tmp_path / "pip").mkdir()
27+
(tmp_path / "pip" / "__init__.py").write_text("raise SystemExit('imported from the working directory')", "utf-8")
28+
monkeypatch.chdir(tmp_path)
29+
session: Final = cli_run([
30+
str(tmp_path / "env"),
31+
"--creator",
32+
current_fastest,
33+
"--seeder",
34+
"pip",
35+
"--pip",
36+
"embed",
37+
"--no-periodic-update",
38+
])
39+
result: Final = subprocess.run(
40+
[str(session.creator.exe), "-I", "-m", "pip", "--version"],
41+
capture_output=True,
42+
text=True,
43+
encoding="utf-8",
44+
check=True,
45+
)
46+
assert os.path.normcase(str(session.creator.purelib)) in os.path.normcase(result.stdout)
47+
1448

1549
@pytest.mark.slow
1650
@pytest.mark.parametrize("no", ["pip", "setuptools", ""], ids=["no-pip", "no-setuptools", "all"])

0 commit comments

Comments
 (0)