Skip to content

Commit c70cbf4

Browse files
authored
env: fix check whether path is relative to system site packages (#9861)
We must not use `SystemEnv`, which is Poetry's own env, but `env.parent_env`, which is the base env where the system site packages (in the context of the virtual env) are located.
1 parent 96ffd5a commit c70cbf4

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/poetry/utils/env/virtual_env.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import json
44
import os
55
import re
6-
import sys
76

87
from contextlib import contextmanager
98
from copy import deepcopy
@@ -17,7 +16,6 @@
1716
from poetry.utils.env.script_strings import GET_ENVIRONMENT_INFO
1817
from poetry.utils.env.script_strings import GET_PATHS
1918
from poetry.utils.env.script_strings import GET_SYS_PATH
20-
from poetry.utils.env.system_env import SystemEnv
2119

2220

2321
if TYPE_CHECKING:
@@ -145,5 +143,5 @@ def includes_system_site_packages(self) -> bool:
145143
def is_path_relative_to_lib(self, path: Path) -> bool:
146144
return super().is_path_relative_to_lib(path) or (
147145
self.includes_system_site_packages
148-
and SystemEnv(Path(sys.prefix)).is_path_relative_to_lib(path)
146+
and self.parent_env.is_path_relative_to_lib(path)
149147
)

tests/utils/env/test_env.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import annotations
22

3-
import contextlib
43
import os
54
import re
6-
import site
75
import subprocess
86
import sys
97

@@ -297,16 +295,31 @@ def test_env_system_packages_are_relative_to_lib(
297295
path=venv_path, flags={"system-site-packages": with_system_site_packages}
298296
)
299297
env = VirtualEnv(venv_path)
300-
site_dir = Path(site.getsitepackages()[-1])
298+
299+
# These are Poetry's own dependencies.
300+
# They should not be relative to the virtualenv's lib directory.
301301
for dist in metadata.distributions():
302-
# Emulate is_relative_to, only available in 3.9+
303-
with contextlib.suppress(ValueError):
304-
dist._path.relative_to(site_dir) # type: ignore[attr-defined]
305-
break
306-
assert (
307-
env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined]
308-
is with_system_site_packages
309-
)
302+
assert not env.is_path_relative_to_lib(
303+
Path(str(dist._path)) # type: ignore[attr-defined]
304+
)
305+
# Checking one package is sufficient
306+
break
307+
else:
308+
pytest.fail("No distributions found in Poetry's own environment")
309+
310+
# These are the virtual environments' base env packages,
311+
# in this case the system site packages.
312+
for dist in metadata.distributions(path=[str(env.parent_env.site_packages.path)]):
313+
assert (
314+
env.is_path_relative_to_lib(
315+
Path(str(dist._path)) # type: ignore[attr-defined]
316+
)
317+
is with_system_site_packages
318+
)
319+
# Checking one package is sufficient
320+
break
321+
else:
322+
pytest.fail("No distributions found in the base environment of the virtualenv")
310323

311324

312325
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)