Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(env): ensure all system site paths are used #9942

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/poetry/utils/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from poetry.utils.env.script_strings import GET_ENV_PATH_ONELINER
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_PATHS_FOR_GENERIC_ENVS
from poetry.utils.env.script_strings import GET_PYTHON_VERSION_ONELINER
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.site_packages import SitePackages
Expand Down Expand Up @@ -97,7 +96,6 @@ def build_environment(
"GET_SYS_PATH",
"GET_ENV_PATH_ONELINER",
"GET_PYTHON_VERSION_ONELINER",
"GET_PATHS_FOR_GENERIC_ENVS",
"EnvError",
"EnvCommandError",
"IncorrectEnvError",
Expand Down
13 changes: 8 additions & 5 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,10 @@ def os(self) -> str:
@property
def site_packages(self) -> SitePackages:
if self._site_packages is None:
# we disable write checks if no user site exist
fallbacks = [self.usersite] if self.usersite else []
self._site_packages = SitePackages(
self.purelib,
self.platlib,
fallbacks,
skip_write_checks=not fallbacks,
self.fallbacks,
)
return self._site_packages

Expand Down Expand Up @@ -214,8 +211,14 @@ def platlib(self) -> Path:

return self._platlib

@cached_property
def fallbacks(self) -> list[Path]:
paths = [Path(path) for path in self.paths.get("fallbacks", [])]
paths += [self.usersite] if self.usersite else []
return paths

def _get_lib_dirs(self) -> list[Path]:
return [self.purelib, self.platlib]
return [self.purelib, self.platlib, *self.fallbacks]

def is_path_relative_to_lib(self, path: Path) -> bool:
for lib_path in self._get_lib_dirs():
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/utils/env/generic_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import TYPE_CHECKING
from typing import Any

from poetry.utils.env.script_strings import GET_PATHS_FOR_GENERIC_ENVS
from poetry.utils.env.script_strings import GET_PATHS
from poetry.utils.env.virtual_env import VirtualEnv


Expand Down Expand Up @@ -78,7 +78,7 @@ def find_executables(self) -> None:
self._pip_executable = pip_executable

def get_paths(self) -> dict[str, str]:
output = self.run_python_script(GET_PATHS_FOR_GENERIC_ENVS)
output = self.run_python_script(GET_PATHS)

paths: dict[str, str] = json.loads(output)
return paths
Expand Down
12 changes: 5 additions & 7 deletions src/poetry/utils/env/script_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,16 @@ def _version_nodot(version):

GET_PATHS = """\
import json
import sysconfig

print(json.dumps(sysconfig.get_paths()))
"""

GET_PATHS_FOR_GENERIC_ENVS = """\
import json
import site
import sysconfig

paths = sysconfig.get_paths().copy()

paths["fallbacks"] = [
p for p in site.getsitepackages()
if p and p not in {paths.get("purelib"), paths.get("platlib")}
abn marked this conversation as resolved.
Show resolved Hide resolved
]

if site.check_enableusersite():
paths["usersite"] = site.getusersitepackages()
paths["userbase"] = site.getuserbase()
Expand Down
3 changes: 1 addition & 2 deletions src/poetry/utils/env/site_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(
purelib: Path,
platlib: Path | None = None,
fallbacks: list[Path] | None = None,
skip_write_checks: bool = False,
) -> None:
self._purelib = purelib
self._platlib = platlib or purelib
Expand All @@ -40,7 +39,7 @@ def __init__(
if path not in self._candidates:
self._candidates.append(path)

self._writable_candidates = None if not skip_write_checks else self._candidates
self._writable_candidates: list[Path] | None = None

@property
def path(self) -> Path:
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/env/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def test_env_system_packages_are_relative_to_lib(

# 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)]):
for dist in env.parent_env.site_packages.distributions():
assert (
env.is_path_relative_to_lib(
Path(str(dist._path)) # type: ignore[attr-defined]
Expand Down
Loading