Skip to content

Commit e3cea6f

Browse files
authored
🐛 fix(seed): contain metadata cleanup (#3428)
1 parent 2bfdc02 commit e3cea6f

3 files changed

Lines changed: 117 additions & 15 deletions

File tree

‎docs/changelog/3428.bugfix.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Limit seed-package cleanup to entries inside the environment. Ignore metadata entries that name the environment itself,
2+
a directory holding the metadata (a blank ``top_level.txt`` line names ``site-packages``), or resolve outside the
3+
environment, with a warning. Launcher entries inside the environment remain supported.

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from itertools import chain
1212
from pathlib import Path
1313
from tempfile import mkdtemp
14-
from typing import TYPE_CHECKING
14+
from typing import TYPE_CHECKING, Final
1515

1616
from distlib.scripts import ScriptMaker, enquote_executable
1717

@@ -182,35 +182,48 @@ def _uninstall_previous_version(self) -> None:
182182
paths = (p for p in in_folders if p.stem.split("-")[0] == dist_name and p.suffix == ".dist-info" and p.is_dir())
183183
existing_dist = next(paths, None)
184184
if existing_dist is not None:
185-
self._uninstall_dist(existing_dist)
185+
self._uninstall_dist(existing_dist, self._creator.dest)
186186

187187
@staticmethod
188-
def _uninstall_dist(dist: Path) -> None:
189-
dist_base = dist.parent
188+
def _uninstall_dist(dist: Path, env_dir: Path) -> None:
189+
dist_base: Final = dist.parent
190190
LOGGER.debug("uninstall existing distribution %s from %s", dist.stem, dist_base)
191191

192-
top_txt = dist / "top_level.txt" # add top level packages at folder level
193-
paths = (
192+
top_txt: Final = dist / "top_level.txt"
193+
paths: Final = (
194194
{dist.parent / i.strip() for i in top_txt.read_text(encoding="utf-8").splitlines()}
195195
if top_txt.exists()
196196
else set()
197197
)
198-
paths.add(dist) # add the dist-info folder itself
198+
paths.add(dist)
199199

200-
base_dirs, record = paths.copy(), dist / "RECORD" # collect entries in record that we did not register yet
200+
base_dirs: Final = paths.copy()
201+
record: Final = dist / "RECORD"
201202
for name in (
202203
(i.split(",")[0] for i in record.read_text(encoding="utf-8").splitlines()) if record.exists() else ()
203204
):
204-
path = dist_base / name
205-
if not any(p in base_dirs for p in path.parents): # only add if not already added as a base dir
205+
path: Final = dist_base / name
206+
if not any(p in base_dirs for p in path.parents):
206207
paths.add(path)
207208

208-
for path in sorted(paths): # actually remove stuff in a stable order
209-
if path.exists():
210-
if path.is_dir() and not path.is_symlink():
211-
safe_delete(path)
209+
root: Final = env_dir.resolve()
210+
metadata_parents: Final = set(dist.resolve().parents)
211+
for entry in sorted(paths):
212+
if entry.exists() or entry.is_symlink():
213+
linked: Final = entry.is_symlink()
214+
# Unlinking the final link leaves its target intact; parent links still affect containment.
215+
resolved: Final = entry.parent.resolve() / entry.name if linked else entry.resolve()
216+
if root not in resolved.parents or resolved in metadata_parents:
217+
LOGGER.warning(
218+
"skip removal of %s listed by %s: not inside environment %s or a parent of the metadata",
219+
entry,
220+
dist.name,
221+
root,
222+
)
223+
elif entry.is_dir() and not linked:
224+
safe_delete(entry)
212225
else:
213-
path.unlink()
226+
entry.unlink()
214227

215228
def clear(self) -> None:
216229
if self._image_dir.exists():

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,92 @@ def test_seed_image_compiles_in_isolation(
5151
assert not marker.exists()
5252

5353

54+
@pytest.mark.parametrize(
55+
("metadata_file", "content"),
56+
[
57+
pytest.param("top_level.txt", "pip\n{dest}\n", id="top-level-env-root"),
58+
pytest.param("top_level.txt", "pip\n\n", id="top-level-blank"),
59+
pytest.param("RECORD", "{record}\n{outside},,", id="record-traversal"),
60+
],
61+
)
62+
def test_seed_metadata_cleanup_stays_inside_package(
63+
tmp_path: Path, caplog: pytest.LogCaptureFixture, metadata_file: str, content: str
64+
) -> None:
65+
args: Final = [str(tmp_path / "env"), "--pip", "embed", "--no-periodic-update"]
66+
session: Final = cli_run(args, setup_logging=False)
67+
purelib: Final = session.creator.purelib
68+
markers: Final = [tmp_path / "keep.txt", session.creator.dest / "keep.txt", purelib / "keep.txt"]
69+
for marker in markers:
70+
marker.write_text("keep", encoding="utf-8")
71+
dist_info: Final = next(purelib.glob("pip-*.dist-info"))
72+
(dist_info / metadata_file).write_text(
73+
content.format(
74+
dest=session.creator.dest,
75+
outside=os.path.relpath(markers[0], purelib),
76+
record=(dist_info / "RECORD").read_text(encoding="utf-8"),
77+
),
78+
encoding="utf-8",
79+
)
80+
cli_run(args, setup_logging=False)
81+
assert (
82+
[i.exists() for i in markers],
83+
(purelib / "pip" / "__init__.py").is_file(),
84+
"skip removal of" in caplog.text,
85+
) == (
86+
[True, True, True],
87+
True,
88+
True,
89+
)
90+
91+
92+
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable")
93+
def test_seed_metadata_unlinks_symlink_without_following(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
94+
args: Final = [str(tmp_path / "env"), "--pip", "embed", "--no-periodic-update"]
95+
session: Final = cli_run(args, setup_logging=False)
96+
outside: Final = tmp_path / "outside"
97+
outside.mkdir()
98+
(outside / "keep.txt").write_text("keep", encoding="utf-8")
99+
link: Final = session.creator.purelib / "linked"
100+
link.symlink_to(outside, target_is_directory=True)
101+
record: Final = next(session.creator.purelib.glob("pip-*.dist-info")) / "RECORD"
102+
record.write_text(f"{record.read_text(encoding='utf-8')}\nlinked,,", encoding="utf-8")
103+
cli_run(args, setup_logging=False)
104+
assert (link.is_symlink(), (outside / "keep.txt").exists(), "skip removal of" in caplog.text) == (
105+
False,
106+
True,
107+
False,
108+
)
109+
110+
111+
@pytest.mark.parametrize(
112+
"linked",
113+
[
114+
pytest.param(False, id="copy"),
115+
pytest.param(
116+
True, id="symlink", marks=pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable")
117+
),
118+
],
119+
)
120+
def test_seed_metadata_keeps_reseeding_supported(
121+
tmp_path: Path, linked: bool, caplog: pytest.LogCaptureFixture
122+
) -> None:
123+
args: Final = [
124+
str(tmp_path / "env"),
125+
"--pip",
126+
"embed",
127+
"--no-periodic-update",
128+
*(["--symlink-app-data"] if linked else []),
129+
]
130+
cli_run(args, setup_logging=False)
131+
session: Final = cli_run(args, setup_logging=False)
132+
package: Final = session.creator.purelib / "pip"
133+
assert ((package / "__init__.py").is_file(), package.is_symlink(), "skip removal of" in caplog.text) == (
134+
True,
135+
linked,
136+
False,
137+
)
138+
139+
54140
@pytest.mark.slow
55141
@pytest.mark.parametrize("copies", [False, True] if fs_supports_symlink() else [True])
56142
def test_seed_link_via_app_data(tmp_path, coverage_env, current_fastest, copies) -> None:

0 commit comments

Comments
 (0)