Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog/+uv-install-dir-absolute.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 5 additions & 4 deletions docs/explanation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion src/python_discovery/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/python_discovery/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down