From f1daf3ec2feee4852f040a8ee4eccd4cbe61183c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:13:39 -0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(uv):=20absolutize=20rela?= =?UTF-8?q?tive=20install=20dir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Discovery searched a relative UV_PYTHON_INSTALL_DIR as written and returned the interpreters it found with relative paths, so a returned executable depended on the directory the caller was in when it used it. uv anchors a relative store at the working directory, so do the same and return absolute paths. Fixes #284. Reported by oss-scanner as ANT-2026-JQLRJOIU. --- .../+uv-install-dir-absolute.bugfix.rst | 2 ++ docs/explanation.rst | 9 ++++--- src/python_discovery/_discovery.py | 3 ++- tests/python_discovery/test_discovery.py | 27 +++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 docs/changelog/+uv-install-dir-absolute.bugfix.rst diff --git a/docs/changelog/+uv-install-dir-absolute.bugfix.rst b/docs/changelog/+uv-install-dir-absolute.bugfix.rst new file mode 100644 index 0000000..a79fc17 --- /dev/null +++ b/docs/changelog/+uv-install-dir-absolute.bugfix.rst @@ -0,0 +1,2 @@ +Interpreters found under a relative ``UV_PYTHON_INSTALL_DIR`` are now returned with absolute paths, so they +still resolve after the working directory changes. diff --git a/docs/explanation.rst b/docs/explanation.rst index 403f532..938e2f2 100644 --- a/docs/explanation.rst +++ b/docs/explanation.rst @@ -108,10 +108,11 @@ uv follows the XDG convention on macOS too, and falls back to the legacy directo exists. python-discovery searches both, in the same order, so a leftover legacy directory cannot hide the store uv installs into. A relative ``XDG_DATA_HOME`` does not count, as the XDG specification requires, and Windows ignores it outright. An empty or relative ``APPDATA`` uses the profile default when ``USERPROFILE`` is present. Discovery skips -relative store roots. Every one of these variables comes from the ``env`` mapping passed to -:func:`~python_discovery.get_interpreter`, rather than from the process environment. If the mapping omits ``HOME`` or -``USERPROFILE``, discovery skips the corresponding home-derived roots but still searches an absolute mapped ``APPDATA`` -or ``XDG_DATA_HOME``. +relative store roots. A relative ``UV_PYTHON_INSTALL_DIR`` resolves against the current directory, as it does in uv, and +interpreters found there are returned with absolute paths. Every one of these variables comes from the ``env`` mapping +passed to :func:`~python_discovery.get_interpreter`, rather than from the process environment. If the mapping omits +``HOME`` or ``USERPROFILE``, discovery skips the corresponding home-derived roots but still searches an absolute mapped +``APPDATA`` or ``XDG_DATA_HOME``. Each install lives in its own subdirectory, but the actual binary location varies by OS and implementation: diff --git a/src/python_discovery/_discovery.py b/src/python_discovery/_discovery.py index 00a70c2..01ae416 100644 --- a/src/python_discovery/_discovery.py +++ b/src/python_discovery/_discovery.py @@ -365,7 +365,8 @@ def _propose_from_uv( def _uv_python_roots(env: Mapping[str, str]) -> Generator[Path, None, None]: # Yield uv's store roots, legacy first; searching both keeps a stale legacy directory from hiding the live one. if install_dir := env.get("UV_PYTHON_INSTALL_DIR"): - yield Path(install_dir).expanduser() + # uv anchors a relative store at the working directory; absolute here so the returned path survives a chdir + yield Path(install_dir).expanduser().absolute() return for state_dir in _uv_state_dirs(env): if state_dir.is_absolute(): # a relative profile path would search the caller's working directory diff --git a/tests/python_discovery/test_discovery.py b/tests/python_discovery/test_discovery.py index a6f6885..bde2e68 100644 --- a/tests/python_discovery/test_discovery.py +++ b/tests/python_discovery/test_discovery.py @@ -947,6 +947,33 @@ def test_discovery_uv_store_install_dir_overrides_the_platform_default( assert uv_probed() == [executable] +def test_discovery_uv_store_relative_install_dir_returns_absolute_path( + uv_home: Path, + monkeypatch: pytest.MonkeyPatch, + mocker: MockerFixture, + session_cache: DiskCache, + uv_plant: Callable[[Path], str], +) -> None: + monkeypatch.setenv("UV_PYTHON_INSTALL_DIR", "store") + monkeypatch.chdir(uv_home) + executable = uv_plant(uv_home / "store") + + def _from_exe(path: str, *_args: object, **_kwargs: object) -> PythonInfo: + info = deepcopy(PythonInfo.current_system(session_cache)) + info.executable = info.system_executable = path + return info + + mocker.patch("python_discovery._discovery.PathPythonInfo.from_exe", autospec=True, side_effect=_from_exe) + result = get_interpreter( + f"{sys.version_info.major}.{sys.version_info.minor}", + cache=session_cache, + predicate=lambda info: info.executable != sys.executable, + ) + + assert result is not None + assert result.executable == executable + + @pytest.mark.parametrize( ("platform", "extra_env", "expected"), [ From 0afc60701e405dca5e8c8d7647d502d99ed6b8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:21:52 -0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=85=20test(uv):=20reuse=20uv=5Fprobed?= =?UTF-8?q?=20for=20relative=20store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hand-built from_exe mock only checked the path it received, which the uv_probed fixture already records. --- tests/python_discovery/test_discovery.py | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/tests/python_discovery/test_discovery.py b/tests/python_discovery/test_discovery.py index bde2e68..685b304 100644 --- a/tests/python_discovery/test_discovery.py +++ b/tests/python_discovery/test_discovery.py @@ -948,30 +948,13 @@ def test_discovery_uv_store_install_dir_overrides_the_platform_default( def test_discovery_uv_store_relative_install_dir_returns_absolute_path( - uv_home: Path, - monkeypatch: pytest.MonkeyPatch, - mocker: MockerFixture, - session_cache: DiskCache, - uv_plant: Callable[[Path], str], + uv_home: Path, monkeypatch: pytest.MonkeyPatch, uv_plant: Callable[[Path], str], uv_probed: UvProbe ) -> None: monkeypatch.setenv("UV_PYTHON_INSTALL_DIR", "store") monkeypatch.chdir(uv_home) executable = uv_plant(uv_home / "store") - def _from_exe(path: str, *_args: object, **_kwargs: object) -> PythonInfo: - info = deepcopy(PythonInfo.current_system(session_cache)) - info.executable = info.system_executable = path - return info - - mocker.patch("python_discovery._discovery.PathPythonInfo.from_exe", autospec=True, side_effect=_from_exe) - result = get_interpreter( - f"{sys.version_info.major}.{sys.version_info.minor}", - cache=session_cache, - predicate=lambda info: info.executable != sys.executable, - ) - - assert result is not None - assert result.executable == executable + assert uv_probed() == [executable] @pytest.mark.parametrize(