Skip to content

Commit

Permalink
fix: handle uvx like uv (#920)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Jan 31, 2025
1 parent 223c8c6 commit 97e0fad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
19 changes: 11 additions & 8 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,17 @@ def _run(
if callable(args[0]):
return self._run_func(args[0], args[1:]) # type: ignore[unreachable]

# Using `"uv"` when `uv` is the backend is guaranteed to work, even if it was co-installed with nox.
if (
self.virtualenv.venv_backend == "uv"
and args[0] == "uv"
and nox.virtualenv.UV != "uv"
and shutil.which("uv", path=self.bin) is None # Session uv takes priority
):
args = (nox.virtualenv.UV, *args[1:])
# Using `"uv"` or `"uvx" when `uv` is the backend is guaranteed to
# work, even if it was co-installed with nox.
if self.virtualenv.venv_backend == "uv" and nox.virtualenv.UV != "uv":
if (
args[0] == "uv"
and shutil.which("uv", path=self.bin)
is None # Session uv takes priority
):
args = (nox.virtualenv.UV, *args[1:])
elif args[0] == "uvx" and shutil.which("uvx", path=self.bin) is None:
args = (f"{nox.virtualenv.UV}x", *args[1:])

# Combine the env argument with our virtualenv's env vars.
env = self.virtualenv._get_env(env or {}, include_outer_env=include_outer_env)
Expand Down
2 changes: 1 addition & 1 deletion nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class VirtualEnv(ProcessEnv):
"""

is_sandboxed = True
allowed_globals = (UV,)
allowed_globals = (UV, f"{UV}x")

def __init__(
self,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,17 @@ class SessionNoSlots(nox.sessions.Session):
"urllib3",
)

# User runs uvx
with mock.patch.object(nox.command, "run", autospec=True) as run:
session.run("uvx", "cowsay")
run.assert_called_once()

((call_args,), _) = run.call_args
assert call_args == (
"/some/uvx",
"cowsay",
)

# user installs uv in the session venv
monkeypatch.setattr(
shutil, "which", lambda x, path="": path + "/uv" if x == "uv" else None
Expand Down

0 comments on commit 97e0fad

Please sign in to comment.