Skip to content

Commit

Permalink
env: fix check whether path is relative to system site packages (#9861)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
radoering authored Nov 20, 2024
1 parent 96ffd5a commit c70cbf4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import os
import re
import sys

from contextlib import contextmanager
from copy import deepcopy
Expand All @@ -17,7 +16,6 @@
from poetry.utils.env.script_strings import GET_ENVIRONMENT_INFO
from poetry.utils.env.script_strings import GET_PATHS
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.system_env import SystemEnv


if TYPE_CHECKING:
Expand Down Expand Up @@ -145,5 +143,5 @@ def includes_system_site_packages(self) -> bool:
def is_path_relative_to_lib(self, path: Path) -> bool:
return super().is_path_relative_to_lib(path) or (
self.includes_system_site_packages
and SystemEnv(Path(sys.prefix)).is_path_relative_to_lib(path)
and self.parent_env.is_path_relative_to_lib(path)
)
35 changes: 24 additions & 11 deletions tests/utils/env/test_env.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

import contextlib
import os
import re
import site
import subprocess
import sys

Expand Down Expand Up @@ -297,16 +295,31 @@ def test_env_system_packages_are_relative_to_lib(
path=venv_path, flags={"system-site-packages": with_system_site_packages}
)
env = VirtualEnv(venv_path)
site_dir = Path(site.getsitepackages()[-1])

# These are Poetry's own dependencies.
# They should not be relative to the virtualenv's lib directory.
for dist in metadata.distributions():
# Emulate is_relative_to, only available in 3.9+
with contextlib.suppress(ValueError):
dist._path.relative_to(site_dir) # type: ignore[attr-defined]
break
assert (
env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined]
is with_system_site_packages
)
assert not env.is_path_relative_to_lib(
Path(str(dist._path)) # type: ignore[attr-defined]
)
# Checking one package is sufficient
break
else:
pytest.fail("No distributions found in Poetry's own environment")

# These are the virtual environments' base env packages,
# in this case the system site packages.
for dist in metadata.distributions(path=[str(env.parent_env.site_packages.path)]):
assert (
env.is_path_relative_to_lib(
Path(str(dist._path)) # type: ignore[attr-defined]
)
is with_system_site_packages
)
# Checking one package is sufficient
break
else:
pytest.fail("No distributions found in the base environment of the virtualenv")


@pytest.mark.parametrize(
Expand Down

0 comments on commit c70cbf4

Please sign in to comment.