Skip to content

Commit

Permalink
fix: avoid mixing venv and conda from environment (#804)
Browse files Browse the repository at this point in the history
* fix: avoid mixing venv and conda from environment

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: add test for CONDA_PREFIX

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Apr 2, 2024
1 parent feb2a7e commit 48f82a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ class ProcessEnv(abc.ABC):
allowed_globals: ClassVar[tuple[Any, ...]] = ()

def __init__(
self, bin_paths: None = None, env: Mapping[str, str] | None = None
self, bin_paths: None = None, env: Mapping[str, str | None] | None = None
) -> None:
self._bin_paths = bin_paths
self.env = os.environ.copy()
self._reused = False
env = env or {}

if env is not None:
self.env.update(env)
for k, v in env.items():
if v is None:
self.env.pop(k, None)
else:
self.env[k] = v

for key in _BLACKLISTED_ENV_VARS:
self.env.pop(key, None)
Expand Down Expand Up @@ -249,7 +253,7 @@ def __init__(
self.reuse_existing = reuse_existing
self.venv_params = venv_params or []
self.conda_cmd = conda_cmd
super().__init__(env={"CONDA_PREFIX": self.location})
super().__init__(env={"CONDA_PREFIX": self.location, "VIRTUAL_ENV": None})

def _clean_location(self) -> bool:
"""Deletes existing conda environment"""
Expand Down Expand Up @@ -379,7 +383,7 @@ def __init__(
if venv_backend not in {"virtualenv", "venv", "uv"}:
msg = f"venv_backend {venv_backend} not recognized"
raise ValueError(msg)
super().__init__(env={"VIRTUAL_ENV": self.location})
super().__init__(env={"VIRTUAL_ENV": self.location, "CONDA_PREFIX": None})

def _clean_location(self) -> bool:
"""Deletes any existing virtual environment"""
Expand Down
6 changes: 6 additions & 0 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,15 @@ def test_bin_windows(make_one):


def test_create(monkeypatch, make_one):
monkeypatch.setenv("CONDA_PREFIX", "no-prefix-allowed")
monkeypatch.setenv("NOT_CONDA_PREFIX", "something-else")

venv, dir_ = make_one()
venv.create()

assert "CONDA_PREFIX" not in venv.env
assert "NOT_CONDA_PREFIX" in venv.env

if IS_WINDOWS:
assert dir_.join("Scripts", "python.exe").check()
assert dir_.join("Scripts", "pip.exe").check()
Expand Down

0 comments on commit 48f82a1

Please sign in to comment.