Skip to content

Commit

Permalink
Restore uv $PATH lookup with logging message
Browse files Browse the repository at this point in the history
  • Loading branch information
layday authored and gaborbernat committed Mar 10, 2024
1 parent 3d934d3 commit f28f1ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/build/env.py
Expand Up @@ -279,13 +279,24 @@ def create(self, path: str) -> None:
import venv

self._env_path = path

try:
import uv

self._uv_bin = uv.find_uv_bin()
except (ModuleNotFoundError, FileNotFoundError):
self._uv_bin = shutil.which('uv')
if self._uv_bin is None:
msg = 'uv executable not found'
raise RuntimeError(msg) from None

_ctx.log(f'Using external uv from {self._uv_bin}')

venv.EnvBuilder(symlinks=_fs_supports_symlink(), with_pip=False).create(self._env_path)
self.python_executable, self.scripts_dir, _ = _find_executable_and_scripts(self._env_path)

def install_requirements(self, requirements: Collection[str]) -> None:
import uv

cmd = [uv.find_uv_bin(), 'pip']
cmd = [self._uv_bin, 'pip']
if _ctx.verbosity > 1:
# uv doesn't support doubling up -v unlike pip.
cmd += ['-v']
Expand Down
26 changes: 26 additions & 0 deletions tests/test_env.py
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import logging
import shutil
import subprocess
import sys
import sysconfig
Expand Down Expand Up @@ -262,3 +263,28 @@ def test_requirement_installation(
):
with build.env.DefaultIsolatedEnv(installer=installer) as env:
env.install([f'test-flit @ {Path(package_test_flit).as_uri()}'])


def test_external_uv_detection_success(
caplog: pytest.LogCaptureFixture,
mocker: pytest_mock.MockerFixture,
):
mocker.patch.dict(sys.modules, {'uv': None})

with build.env.DefaultIsolatedEnv(installer='uv'):
pass

assert any(
r.message == f'Using external uv from {shutil.which("uv", path=sysconfig.get_path("scripts"))}' for r in caplog.records
)


def test_external_uv_detection_failure(
mocker: pytest_mock.MockerFixture,
):
mocker.patch.dict(sys.modules, {'uv': None})
mocker.patch('shutil.which', return_value=None)

with pytest.raises(RuntimeError, match='uv executable not found'):
with build.env.DefaultIsolatedEnv(installer='uv'):
pass

0 comments on commit f28f1ad

Please sign in to comment.