Skip to content

Commit

Permalink
Consider system packages as installed if the venv includes them
Browse files Browse the repository at this point in the history
This changesets adds a getter to `env.virtual_env.VirtualEnv` to check
whether it has access to system packages, and overrides the
`is_path_relative_to_lib` method to take it into account.

This will prevent Poetry from reinstalling system packages in the venv
when they are already installed with a compatible version.

Fixes: #6035

Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Aug 24, 2023
1 parent f5b0228 commit 610abf7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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()

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)
13 changes: 13 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 +1463,20 @@ 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)
pytest_dist = metadata.distribution("pytest")
assert env.is_path_relative_to_lib(pytest_dist._path) # type: ignore[attr-defined]


@pytest.mark.parametrize(
Expand Down

0 comments on commit 610abf7

Please sign in to comment.