Skip to content

Commit d25f286

Browse files
🐛 fix(create): replace generated file links (#3431)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e3cea6f commit d25f286

11 files changed

Lines changed: 216 additions & 23 deletions

File tree

‎docs/changelog/3431.bugfix.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Replace existing symbolic and hard links when writing environment configuration, activation scripts, import hooks, and
2+
seed launchers. Create generated text files exclusively so a replacement link cannot redirect the final file open.
3+
Before recreating an environment with the stdlib venv creator, replace executable-directory symlinks and Windows
4+
junctions with real directories. Remove symbolic and hard links inside that directory before creating interpreter files,
5+
including when switching from symlinks to copies.

‎src/virtualenv/activation/via_template.py‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import shlex
66
import sys
77
from abc import ABC, abstractmethod
8-
from typing import TYPE_CHECKING
8+
from typing import TYPE_CHECKING, Final
9+
10+
from virtualenv.util.path import write_file
911

1012
from .activator import Activator
1113

@@ -63,18 +65,13 @@ def replacements(self, creator: Creator, dest_folder: Path) -> dict[str, str]:
6365
def _generate(
6466
self, replacements: dict[str, str], templates: Iterable[str], to_folder: Path, creator: Creator
6567
) -> list[Path]:
66-
generated = []
68+
generated: Final[list[Path]] = []
6769
for template in templates:
68-
text = self.instantiate_template(replacements, template, creator)
69-
dest = to_folder / self.as_name(template)
70-
# remove the file if it already exists - this prevents permission
71-
# errors when the dest is not writable
72-
if dest.exists():
73-
dest.unlink()
74-
# Powershell assumes Windows 1252 encoding when reading files without BOM
75-
encoding = "utf-8-sig" if str(template).endswith(".ps1") else "utf-8"
76-
# use write_bytes to avoid platform specific line normalization (\n -> \r\n)
77-
dest.write_bytes(text.encode(encoding))
70+
text: Final = self.instantiate_template(replacements, template, creator)
71+
dest: Final = to_folder / self.as_name(template)
72+
# PowerShell assumes Windows 1252 when the UTF-8 BOM is absent.
73+
encoding: Final = "utf-8-sig" if str(template).endswith(".ps1") else "utf-8"
74+
write_file(dest, text.encode(encoding))
7875
generated.append(dest)
7976
return generated
8077

‎src/virtualenv/create/creator.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from os.path import commonpath
2727

28-
from virtualenv.util.path import safe_delete
28+
from virtualenv.util.path import safe_delete, write_file
2929
from virtualenv.util.subprocess import LogCmd, run_cmd
3030
from virtualenv.util.text import collapse_line_boundaries
3131
from virtualenv.version import __version__
@@ -262,7 +262,7 @@ def add_cachedir_tag(self) -> None:
262262
# For information about cache directory tags, see:
263263
# https://bford.info/cachedir/
264264
""").strip()
265-
cachedir_tag_file.write_text(cachedir_tag_text, encoding="utf-8")
265+
write_file(cachedir_tag_file, cachedir_tag_text.encode("utf-8"))
266266

267267
def set_pyenv_cfg(self) -> None:
268268
self.pyenv_cfg.content = OrderedDict()
@@ -286,7 +286,7 @@ def setup_ignore_vcs(self) -> None:
286286
# mark this folder to be ignored by VCS, handle https://www.python.org/dev/peps/pep-0610/#registered-vcs
287287
git_ignore = self.dest / ".gitignore"
288288
if not git_ignore.exists():
289-
git_ignore.write_text("# created by virtualenv automatically\n*\n", encoding="utf-8")
289+
write_file(git_ignore, b"# created by virtualenv automatically\n*\n")
290290
# Mercurial - does not support the .hgignore file inside a subdirectory directly, but only if included via the
291291
# subinclude directive from root, at which point on might as well ignore the directory itself, see
292292
# https://www.selenic.com/mercurial/hgignore.5.html for more details

‎src/virtualenv/create/pyenv_cfg.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections import OrderedDict
66
from typing import TYPE_CHECKING, Final
77

8+
from virtualenv.util.path import write_file
89
from virtualenv.util.text import collapse_line_boundaries
910

1011
if TYPE_CHECKING:
@@ -67,7 +68,7 @@ def write(self) -> None:
6768
LOGGER.debug("\t%r", line)
6869
text += line
6970
text += "\n"
70-
self.path.write_bytes(text.encode("utf-8"))
71+
write_file(self.path, text.encode("utf-8"))
7172

7273
def refresh(self) -> OrderedDict[str, str]:
7374
self.content = self._read_values(self.path)

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

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

88
from virtualenv.create.creator import Creator, CreatorMeta
99
from virtualenv.info import fs_supports_symlink
10+
from virtualenv.util.path import write_file
1011

1112
if TYPE_CHECKING:
1213
from argparse import ArgumentParser
@@ -120,10 +121,10 @@ def install_patch(self) -> None:
120121
if text:
121122
pth = self.purelib / "_virtualenv.pth"
122123
LOGGER.debug("create virtualenv import hook file %s", pth)
123-
pth.write_text("import _virtualenv", encoding="utf-8")
124+
write_file(pth, b"import _virtualenv")
124125
dest_path = self.purelib / "_virtualenv.py"
125126
LOGGER.debug("create %s", dest_path)
126-
dest_path.write_text(text, encoding="utf-8")
127+
write_file(dest_path, text.encode("utf-8"))
127128

128129
def env_patch_text(self) -> str:
129130
"""Patch the distutils package to not be derailed by its configuration files."""

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from virtualenv.create.via_global_ref.store import handle_store_python
1010
from virtualenv.util.error import ProcessCallFailedError
11-
from virtualenv.util.path import ensure_dir
11+
from virtualenv.util.path import ensure_dir, ensure_within
1212
from virtualenv.util.subprocess import run_cmd
1313
from virtualenv.util.text import collapse_line_boundaries
1414

@@ -49,6 +49,12 @@ def can_create(cls, interpreter: PythonInfo) -> ViaGlobalRefMeta | None:
4949
return None
5050

5151
def create(self) -> None:
52+
# stdlib venv writes interpreter files, configuration and activation scripts in place, so a planted symlink or
53+
# hard link would receive them outside the environment
54+
ensure_within(self.dest, self.bin_dir)
55+
for path in (self.dest / "pyvenv.cfg", self.dest / ".gitignore", *self.bin_dir.iterdir()):
56+
if path.is_symlink() or (path.is_file() and path.stat().st_nlink > 1):
57+
path.unlink()
5258
if self.can_be_inline:
5359
self.create_inline()
5460
else:

‎src/virtualenv/seed/embed/via_app_data/pip_install/base.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ def _write_script(
247247
self, names: set[str], shebang: bytes, script_bytes: bytes, filenames: list[str], ext: str
248248
) -> None:
249249
names.add(f"{self._name}{self.version_info[0]}.{self.version_info[1]}")
250+
for name in names:
251+
path: Final = Path(self.target_dir) / (f"{name}.exe" if os.name == "nt" else name)
252+
if path.is_symlink():
253+
path.unlink()
250254
super()._write_script(names, shebang, script_bytes, filenames, ext)
251255

252256

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
from __future__ import annotations
22

33
from ._permission import make_exe, set_tree
4-
from ._sync import copy, copytree, ensure_dir, safe_delete, symlink
4+
from ._sync import copy, copytree, ensure_dir, ensure_within, safe_delete, symlink, write_file
55
from ._win import get_short_path_name
66

77
__all__ = [
88
"copy",
99
"copytree",
1010
"ensure_dir",
11+
"ensure_within",
1112
"get_short_path_name",
1213
"make_exe",
1314
"safe_delete",
1415
"set_tree",
1516
"symlink",
17+
"write_file",
1618
]

‎src/virtualenv/util/path/_sync.py‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import shutil
66
import sys
77
from pathlib import Path
8-
from stat import S_IXGRP, S_IXOTH, S_IXUSR
8+
from stat import S_ISDIR, S_ISLNK, S_IXGRP, S_IXOTH, S_IXUSR
99
from typing import TYPE_CHECKING, Final
1010

1111
if os.name == "nt":
12+
from stat import IO_REPARSE_TAG_MOUNT_POINT
13+
1214
from ._win import clear_readonly
1315

1416
if TYPE_CHECKING:
@@ -26,6 +28,45 @@ def ensure_dir(path: Path) -> None:
2628
os.makedirs(str(path))
2729

2830

31+
def ensure_within(root: Path, path: Path) -> None:
32+
parts: Final = path.relative_to(root).parts
33+
if ".." in parts:
34+
msg: Final = f"environment directory {path} must remain inside {root}"
35+
raise ValueError(msg)
36+
ensure_dir(root)
37+
folder = root
38+
for part in parts:
39+
folder /= part
40+
try:
41+
metadata: Final = folder.lstat()
42+
except FileNotFoundError:
43+
pass
44+
else:
45+
if S_ISLNK(metadata.st_mode):
46+
LOGGER.warning("replace environment directory link %s", folder)
47+
folder.unlink()
48+
elif os.name == "nt" and metadata.st_reparse_tag == IO_REPARSE_TAG_MOUNT_POINT:
49+
LOGGER.warning("replace environment directory junction %s", folder)
50+
folder.rmdir()
51+
try:
52+
folder.mkdir()
53+
except FileExistsError:
54+
created: Final = folder.lstat()
55+
if not S_ISDIR(created.st_mode) or (
56+
os.name == "nt" and created.st_reparse_tag == IO_REPARSE_TAG_MOUNT_POINT
57+
):
58+
msg: Final = f"environment directory is not a real directory: {folder}"
59+
raise NotADirectoryError(msg) from None
60+
61+
62+
def write_file(path: Path, data: bytes) -> None:
63+
if path.is_symlink() or path.exists():
64+
path.unlink()
65+
# Exclusive creation rejects links introduced after the old entry was removed.
66+
with path.open("xb") as stream:
67+
stream.write(data)
68+
69+
2970
def ensure_safe_to_do(src: Path, dest: Path) -> None:
3071
if src == dest:
3172
msg = f"source and destination is the same {src}"
@@ -102,6 +143,8 @@ def __str__(self) -> str:
102143
"copy",
103144
"copytree",
104145
"ensure_dir",
146+
"ensure_within",
105147
"safe_delete",
106148
"symlink",
149+
"write_file",
107150
]

‎tests/unit/create/test_creator.py‎

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from virtualenv.create.pyenv_cfg import PyEnvCfg
3232
from virtualenv.create.via_global_ref import api
3333
from virtualenv.create.via_global_ref.builtin.cpython.common import is_mac_os_framework, is_macos_brew
34-
from virtualenv.info import IS_PYPY, IS_WIN, fs_is_case_sensitive, fs_supports_symlink
34+
from virtualenv.info import IS_GRAALPY, 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
3737
from virtualenv.util.error import ProcessCallFailedError
@@ -118,6 +118,94 @@ def test_clear_direct_destination(tmp_path: Path, parent_link: bool) -> None:
118118
assert (session.creator.dest, marker.exists(), (target / "pyvenv.cfg").is_file()) == (target.resolve(), False, True)
119119

120120

121+
@pytest.mark.parametrize("creator_name", [pytest.param("builtin", id="builtin"), pytest.param("venv", id="venv")])
122+
@pytest.mark.parametrize("existing", [pytest.param(False, id="dangling"), pytest.param(True, id="existing")])
123+
@pytest.mark.parametrize(
124+
"generated",
125+
[
126+
pytest.param(name, id=name)
127+
for name in ("pyvenv.cfg", "activate", "pip", "CACHEDIR.TAG", ".gitignore", "_virtualenv.py", "_virtualenv.pth")
128+
],
129+
)
130+
def test_creator_replaces_generated_file_links(
131+
tmp_path: Path,
132+
creator_name: str,
133+
existing: bool,
134+
generated: str,
135+
) -> None:
136+
command: Final = [
137+
str(tmp_path / "env"),
138+
"--creator",
139+
creator_name,
140+
"--pip",
141+
"embed",
142+
"--no-periodic-update",
143+
]
144+
if generated.startswith("_virtualenv"):
145+
if (python39 := shutil.which("python3.9")) is None:
146+
pytest.skip("Python 3.9 is unavailable")
147+
command.extend(["--python", python39])
148+
session: Final = session_via_cli(command)
149+
session.creator.bin_dir.mkdir(parents=True)
150+
target: Final = session.creator.dest / "original"
151+
if existing:
152+
target.write_bytes(b"original")
153+
path = (
154+
session.creator.bin_dir / ("pip.exe" if generated == "pip" and os.name == "nt" else generated)
155+
if generated in {"activate", "pip"}
156+
else session.creator.dest / generated
157+
)
158+
if generated.startswith("_virtualenv"):
159+
path = session.creator.purelib / generated
160+
path.parent.mkdir(parents=True, exist_ok=True)
161+
path.symlink_to(target)
162+
with session:
163+
session.run()
164+
if existing and (generated == "CACHEDIR.TAG" or (generated == ".gitignore" and creator_name == "builtin")):
165+
assert path.is_symlink()
166+
else:
167+
assert not path.is_symlink()
168+
assert (target.read_bytes() if existing else target.exists()) == (b"original" if existing else False)
169+
170+
171+
@pytest.mark.parametrize(
172+
"kind",
173+
[
174+
pytest.param("file", id="file"),
175+
pytest.param("symlink", id="symlink"),
176+
pytest.param("dangling", id="dangling"),
177+
pytest.param("junction", id="junction"),
178+
],
179+
)
180+
def test_venv_prepares_real_executable_directory(tmp_path: Path, kind: str) -> None:
181+
session: Final = session_via_cli([str(tmp_path / "env"), "--creator", "venv", "--no-seed", "--activators="])
182+
session.creator.dest.mkdir()
183+
if kind == "file":
184+
session.creator.bin_dir.write_bytes(b"file")
185+
else:
186+
target: Final = session.creator.dest / "empty"
187+
if kind != "dangling":
188+
target.mkdir()
189+
if kind == "junction":
190+
if not IS_WIN:
191+
pytest.skip("requires Windows junctions")
192+
subprocess.run(
193+
["cmd", "/c", "mklink", "/J", str(session.creator.bin_dir), str(target)],
194+
capture_output=True,
195+
check=True,
196+
)
197+
else:
198+
if not fs_supports_symlink():
199+
pytest.skip("requires directory symlinks")
200+
session.creator.bin_dir.symlink_to(target, target_is_directory=True)
201+
if kind == "file":
202+
with pytest.raises(NotADirectoryError, match="environment directory is not a real directory"):
203+
session.run()
204+
else:
205+
session.run()
206+
assert session.creator.bin_dir.resolve() == session.creator.bin_dir
207+
208+
121209
def test_os_path_sep_not_allowed(tmp_path, capsys) -> None:
122210
target = str(tmp_path / f"a{os.pathsep}b")
123211
err = _non_success_exit_code(capsys, target)
@@ -462,6 +550,21 @@ def test_venv_configuration_uses_selected_options(tmp_path: Path, global_package
462550
assert {key: session.creator.pyenv_cfg.content.get(key) for key in expected} == expected
463551

464552

553+
@pytest.mark.skipif(not CURRENT.has_venv or not fs_supports_symlink(), reason="requires venv and symlinks")
554+
@pytest.mark.skipif(
555+
is_mac_os_framework(CURRENT) or is_macos_brew(CURRENT) or (IS_GRAALPY and not IS_WIN),
556+
reason="stdlib creator requires symlinks on this runtime",
557+
)
558+
def test_venv_recreate_with_copied_interpreter(tmp_path: Path) -> None:
559+
args: Final = [str(tmp_path / "env"), "--creator", "venv", "--no-seed"]
560+
cli_run([*args, "--symlinks"])
561+
session: Final = cli_run([*args, "--copies"])
562+
assert (session.creator.exe.is_file(), session.creator.exe.resolve().parent) == (
563+
True,
564+
session.creator.bin_dir.resolve(),
565+
)
566+
567+
465568
@pytest.mark.skipif(not CURRENT.has_venv, reason="requires interpreter with venv")
466569
def test_venv_subprocess_skips_local_venv_module(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
467570
(tmp_path / "venv").mkdir()
@@ -479,6 +582,28 @@ def test_venv_subprocess_skips_local_venv_module(tmp_path: Path, monkeypatch: py
479582
assert (session.creator.dest / "pyvenv.cfg").is_file()
480583

481584

585+
@pytest.mark.skipif(not CURRENT.has_venv, reason="requires interpreter with venv")
586+
@pytest.mark.parametrize("mode", [pytest.param("--copies", id="copies"), pytest.param("--symlinks", id="symlinks")])
587+
def test_venv_replaces_hard_linked_files(tmp_path: Path, mode: str) -> None:
588+
if mode == "--symlinks" and not fs_supports_symlink():
589+
pytest.skip("requires symlinks")
590+
if mode == "--copies" and (is_mac_os_framework(CURRENT) or is_macos_brew(CURRENT) or (IS_GRAALPY and not IS_WIN)):
591+
pytest.skip("stdlib creator requires symlinks on this runtime")
592+
session: Final = session_via_cli([str(tmp_path / "env"), "--creator", "venv", "--no-seed", "--activators=", mode])
593+
session.creator.bin_dir.mkdir(parents=True)
594+
names: Final = (
595+
("python.exe", "pythonw.exe") if IS_WIN else ("python", "python3", f"python3.{CURRENT.version_info.minor}")
596+
)
597+
planted: Final = [session.creator.dest / "pyvenv.cfg", *(session.creator.bin_dir / name for name in names)]
598+
for path in planted:
599+
(tmp_path / f"{path.name}.original").write_bytes(b"original")
600+
os.link(tmp_path / f"{path.name}.original", path)
601+
session.run()
602+
assert [((tmp_path / f"{path.name}.original").read_bytes(), path.lstat().st_nlink) for path in planted] == [
603+
(b"original", 1)
604+
] * len(planted)
605+
606+
482607
@pytest.mark.skipif(not CURRENT.has_venv, reason="requires interpreter with venv")
483608
def test_venv_fails_not_inline(tmp_path, capsys, mocker) -> None:
484609
if hasattr(os, "geteuid") and os.geteuid() == 0:

0 commit comments

Comments
 (0)