diff --git a/src/poetry/utils/env/base_env.py b/src/poetry/utils/env/base_env.py index 487a0aa2276..eb8dac6cf97 100644 --- a/src/poetry/utils/env/base_env.py +++ b/src/poetry/utils/env/base_env.py @@ -219,8 +219,11 @@ def platlib(self) -> Path: return self._platlib + def _get_lib_dirs(self) -> list[Path]: + return [self.purelib, self.platlib] + def is_path_relative_to_lib(self, path: Path) -> bool: - for lib_path in [self.purelib, self.platlib]: + for lib_path in self._get_lib_dirs(): with contextlib.suppress(ValueError): path.relative_to(lib_path) return True diff --git a/src/poetry/utils/env/system_env.py b/src/poetry/utils/env/system_env.py index 81ec7400d67..e088e684480 100644 --- a/src/poetry/utils/env/system_env.py +++ b/src/poetry/utils/env/system_env.py @@ -2,6 +2,7 @@ import os import platform +import site import sys import sysconfig @@ -87,3 +88,6 @@ def get_pip_version(self) -> Version: def is_venv(self) -> bool: return self._path != self._base + + def _get_lib_dirs(self) -> list[Path]: + return super()._get_lib_dirs() + [Path(d) for d in site.getsitepackages()] diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 1ca5893e816..64258587a70 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import site import subprocess import sys @@ -1475,8 +1476,13 @@ def test_env_system_packages_are_relative_to_lib( venv_path = tmp_path / "venv" EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True}) env = VirtualEnv(venv_path) - pytest_dist = metadata.distribution("pytest") - assert env.is_path_relative_to_lib(pytest_dist._path) # type: ignore[attr-defined] + site_dir = Path(site.getsitepackages()[-1]) + dist = next( + p + for p in metadata.distributions() + if p._path.relative_to(site_dir) is not None # type: ignore[attr-defined] + ) + assert env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined] @pytest.mark.parametrize(