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..685b304 100644 --- a/tests/python_discovery/test_discovery.py +++ b/tests/python_discovery/test_discovery.py @@ -947,6 +947,16 @@ 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, 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") + + assert uv_probed() == [executable] + + @pytest.mark.parametrize( ("platform", "extra_env", "expected"), [