Skip to content

Commit a2b1cb7

Browse files
committed
Fix UV_PYTHON_PREFERENCE reading breaks tox when env_site_packages_dir is in set_env
Signed-off-by: Bernát Gábor <[email protected]>
1 parent eb4134d commit a2b1cb7

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/tox_uv/_venv.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import json
66
import logging
7+
import os
78
import sys
89
from abc import ABC
910
from functools import cached_property
@@ -73,7 +74,9 @@ def register_config(self) -> None:
7374
self.conf.add_config(
7475
keys=["uv_python_preference"],
7576
of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # noqa: UP006
76-
default=lambda conf, name: self.environment_variables.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
77+
# use os.environ here instead of self.environment_variables as this value is needed to create the virtual
78+
# environment, if environment variables use env_site_packages_dir we would run into a chicken-egg problem.
79+
default=lambda conf, name: os.environ.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
7780
desc=(
7881
"Whether to prefer using Python installations that are already"
7982
" present on the system, or those that are downloaded and"
@@ -240,10 +243,10 @@ def env_python(self) -> Path:
240243
def env_site_package_dir(self) -> Path:
241244
if sys.platform == "win32": # pragma: win32 cover
242245
return self.venv_dir / "Lib" / "site-packages"
243-
else: # pragma: win32 no cover # noqa: RET505
244-
py = self._py_info
245-
impl = "pypy" if py.implementation == "pypy" else "python"
246-
return self.venv_dir / "lib" / f"{impl}{py.version_dot}" / "site-packages"
246+
# pragma: win32 no cover
247+
py = self._py_info
248+
impl = "pypy" if py.implementation == "pypy" else "python"
249+
return self.venv_dir / "lib" / f"{impl}{py.version_dot}" / "site-packages"
247250

248251
def env_version_spec(self) -> str:
249252
base = self.base_python.version_info

tests/test_tox_uv_venv.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ def test_uv_venv_preference_override_via_env_var(
7171
assert got == "only-managed"
7272

7373

74+
def test_uv_venv_preference_override_via_env_var_and_set_env_depends_on_py(
75+
tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch
76+
) -> None:
77+
project = tox_project({"tox.ini": "[testenv]\nset_env=A={env_site_packages_dir}"})
78+
monkeypatch.setenv("UV_PYTHON_PREFERENCE", "only-managed")
79+
80+
result = project.run("c", "-k", "set_env")
81+
result.assert_success()
82+
83+
parser = ConfigParser()
84+
parser.read_string(result.out)
85+
got = parser["testenv:py"]["set_env"]
86+
87+
assert str(project.path) in got
88+
89+
7490
def test_uv_venv_spec(tox_project: ToxProjectCreator) -> None:
7591
ver = sys.version_info
7692
project = tox_project({"tox.ini": f"[testenv]\npackage=skip\nbase_python={ver.major}.{ver.minor}"})

0 commit comments

Comments
 (0)