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

Consider system packages as installed if the venv includes them #8359

Merged
merged 6 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/poetry/utils/env/system_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import platform
import site
import sys
import sysconfig

Expand Down Expand Up @@ -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()]
13 changes: 13 additions & 0 deletions src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import re
import sys

from contextlib import contextmanager
from copy import deepcopy
Expand All @@ -20,6 +21,7 @@
from poetry.utils.env.script_strings import GET_PYTHON_VERSION
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.script_strings import GET_SYS_TAGS
from poetry.utils.env.system_env import SystemEnv


if TYPE_CHECKING:
Expand Down Expand Up @@ -133,3 +135,14 @@ def temp_environ(self) -> Iterator[None]:

def _updated_path(self) -> str:
return os.pathsep.join([str(self._bin_dir), os.environ.get("PATH", "")])

def includes_system_site_packages(self) -> bool:
pyvenv_cfg = self._path / "pyvenv.cfg"
return "include-system-site-packages = true" in pyvenv_cfg.read_text()
radoering marked this conversation as resolved.
Show resolved Hide resolved
radoering marked this conversation as resolved.
Show resolved Hide resolved

def is_path_relative_to_lib(self, path: Path) -> bool:
system_env = SystemEnv(Path(sys.prefix))
return (
self.includes_system_site_packages()
and system_env.is_path_relative_to_lib(path)
) or super().is_path_relative_to_lib(path)
abompard marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import contextlib
import os
import site
import subprocess
import sys

Expand All @@ -18,6 +20,7 @@
from poetry.repositories.installed_repository import InstalledRepository
from poetry.toml.file import TOMLFile
from poetry.utils._compat import WINDOWS
from poetry.utils._compat import metadata
from poetry.utils.env import GET_BASE_PREFIX
from poetry.utils.env import GET_PYTHON_VERSION_ONELINER
from poetry.utils.env import EnvCommandError
Expand Down Expand Up @@ -1462,8 +1465,25 @@ def test_env_system_packages(tmp_path: Path, poetry: Poetry) -> None:
pyvenv_cfg = venv_path / "pyvenv.cfg"

EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True})
env = VirtualEnv(venv_path)

assert "include-system-site-packages = true" in pyvenv_cfg.read_text()
assert env.includes_system_site_packages()


def test_env_system_packages_are_relative_to_lib(
tmp_path: Path, poetry: Poetry
) -> None:
venv_path = tmp_path / "venv"
EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True})
env = VirtualEnv(venv_path)
site_dir = Path(site.getsitepackages()[-1])
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]


@pytest.mark.parametrize(
Expand Down