Skip to content

Commit

Permalink
Ensure we don't pick up Poetry's virtualenv as the system env
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Jun 18, 2021
1 parent 67c1b34 commit 9246cf3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 10 additions & 0 deletions poetry/locations.py
Original file line number Diff line number Diff line change
@@ -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))
36 changes: 31 additions & 5 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9246cf3

Please sign in to comment.