From f28f1adfc09e8840b61fb55f5c016ec8186bde32 Mon Sep 17 00:00:00 2001 From: layday Date: Sun, 10 Mar 2024 11:07:16 +0200 Subject: [PATCH] Restore uv `$PATH` lookup with logging message --- src/build/env.py | 17 ++++++++++++++--- tests/test_env.py | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/build/env.py b/src/build/env.py index 92fcfdea..23b8ed7c 100644 --- a/src/build/env.py +++ b/src/build/env.py @@ -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'] diff --git a/tests/test_env.py b/tests/test_env.py index 54f2339e..7bfd8c3c 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -2,6 +2,7 @@ from __future__ import annotations import logging +import shutil import subprocess import sys import sysconfig @@ -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