From 9246cf3a88cbe921f42feaf50f42dff55e5797b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Fri, 18 Jun 2021 11:42:24 +0200 Subject: [PATCH] Ensure we don't pick up Poetry's virtualenv as the system env --- poetry/locations.py | 10 ++++++++++ poetry/utils/env.py | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/poetry/locations.py b/poetry/locations.py index 003950d500d..001e1a9ef66 100644 --- a/poetry/locations.py +++ b/poetry/locations.py @@ -1,9 +1,19 @@ +import os + from .utils._compat import Path from .utils.appdirs import user_cache_dir from .utils.appdirs import user_config_dir +from .utils.appdirs import user_data_dir CACHE_DIR = user_cache_dir("pypoetry") CONFIG_DIR = user_config_dir("pypoetry") REPOSITORY_CACHE_DIR = Path(CACHE_DIR) / "cache" / "repositories" + + +def data_dir(): # type: () -> Path + if os.getenv("POETRY_HOME"): + return Path(os.getenv("POETRY_HOME")).expanduser() + + return Path(user_data_dir("pypoetry", roaming=True)) diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 681027ac3a4..d6875245eb7 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -472,7 +472,7 @@ def get(self, reload=False): # type: (bool) -> Env create_venv = self._poetry.config.get("virtualenvs.create", True) if not create_venv: - return SystemEnv(Path(sys.prefix)) + return self.get_system_env() venv_path = self._poetry.config.get("virtualenvs.path") if venv_path is None: @@ -485,7 +485,7 @@ def get(self, reload=False): # type: (bool) -> Env venv = venv_path / name if not venv.exists(): - return SystemEnv(Path(sys.prefix)) + return self.get_system_env() return VirtualEnv(venv) @@ -790,7 +790,7 @@ def create_venv( p_venv = os.path.normcase(str(venv)) if any(p.startswith(p_venv) for p in paths): # Running properly in the virtualenv, don't need to do anything - return SystemEnv(Path(sys.prefix), self.get_base_prefix()) + return SystemEnv(Path(sys.prefix), Path(self.get_base_prefix())) return VirtualEnv(venv) @@ -833,7 +833,33 @@ def remove_venv(cls, path): # type: (Union[Path,str]) -> None elif file_path.is_dir(): shutil.rmtree(str(file_path)) - def get_base_prefix(self): # type: () -> Path + @classmethod + def get_system_env(cls, naive=False): # type: (bool) -> "SystemEnv" + """ + Retrieve the current Python environment. + This can be the base Python environment or an activated virtual environment. + This method also works around the issue that the virtual environment + used by Poetry internally (when installed via the custom installer) + is incorrectly detected as the system environment. Note that this workaround + happens only when `naive` is False since there are times where we actually + want to retrieve Poetry's custom virtual environment + (e.g. plugin installation or self update). + """ + prefix, base_prefix = Path(sys.prefix), Path(cls.get_base_prefix()) + if naive is False: + from poetry.locations import data_dir + + try: + prefix.relative_to(data_dir()) + except ValueError: + pass + else: + prefix = base_prefix + + return SystemEnv(prefix) + + @classmethod + def get_base_prefix(cls): # type: () -> str if hasattr(sys, "real_prefix"): return sys.real_prefix @@ -993,7 +1019,7 @@ def supported_tags(self): # type: () -> List[Tag] return self._supported_tags @classmethod - def get_base_prefix(cls): # type: () -> Path + def get_base_prefix(cls): # type: () -> str if hasattr(sys, "real_prefix"): return sys.real_prefix