Skip to content

Commit

Permalink
fix: ensure 'uv' always works in a uv venv
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Apr 12, 2024
1 parent 55c7eaf commit ccb0ec4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
11 changes: 10 additions & 1 deletion nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ def _run(
if callable(args[0]):
return self._run_func(args[0], args[1:], kwargs) # type: ignore[unreachable]

# Using `"uv"` when `uv` is the backend is guaranteed to work, even if it was co-installed with nox.
if (
isinstance(self.virtualenv, VirtualEnv)
and self.virtualenv.venv_backend == "uv"
and args[0] == "uv"
and UV != "uv"
):
args = (UV, *args[1:])

# Combine the env argument with our virtualenv's env vars.
if include_outer_env:
overlay_env = env
Expand Down Expand Up @@ -670,7 +679,7 @@ def install(self, *args: str, **kwargs: Any) -> None:
kwargs["silent"] = True

if isinstance(venv, VirtualEnv) and venv.venv_backend == "uv":
self._run(UV, "pip", "install", *args, external="error", **kwargs)
self._run("uv", "pip", "install", *args, external="error", **kwargs)
else:
self._run(
"python", "-m", "pip", "install", *args, external="error", **kwargs
Expand Down
5 changes: 2 additions & 3 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ def find_uv() -> tuple[bool, str]:
return True, find_uv_bin()

# Fall back to PATH.
uv = shutil.which("uv")
if uv is not None:
return True, uv
if shutil.which("uv") is not None:
return True, "uv"

return False, "uv"

Expand Down
3 changes: 2 additions & 1 deletion tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,11 @@ class SessionNoSlots(nox.sessions.Session):

session = SessionNoSlots(runner=runner)

uv = "uv" if shutil.which("uv") is not None else nox.virtualenv.UV
with mock.patch.object(session, "_run", autospec=True) as run:
session.install("requests", "urllib3", silent=False)
run.assert_called_once_with(
nox.virtualenv.UV,
uv,
"pip",
"install",
"requests",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def test_create_reuse_uv_environment(make_one):
["which_result", "find_uv_bin_result", "expected"],
[
("/usr/bin/uv", UV_IN_PIPX_VENV, (True, UV_IN_PIPX_VENV)),
("/usr/bin/uv", FileNotFoundError, (True, "/usr/bin/uv")),
("/usr/bin/uv", FileNotFoundError, (True, "uv")),
(None, UV_IN_PIPX_VENV, (True, UV_IN_PIPX_VENV)),
(None, FileNotFoundError, (False, "uv")),
],
Expand Down

0 comments on commit ccb0ec4

Please sign in to comment.