diff --git a/docs/changelog/+relative-try-first.bugfix.rst b/docs/changelog/+relative-try-first.bugfix.rst new file mode 100644 index 0000000..1293942 --- /dev/null +++ b/docs/changelog/+relative-try-first.bugfix.rst @@ -0,0 +1,3 @@ +Discovery now finds an interpreter given as a relative path such as ``./venv/bin/python`` when ``try_first_with`` lists +the same file under another name or case, its base interpreter, or another virtual environment on that base, instead of +finding nothing. diff --git a/docs/explanation.rst b/docs/explanation.rst index 6abd000..5a0b563 100644 --- a/docs/explanation.rst +++ b/docs/explanation.rst @@ -12,10 +12,10 @@ order. It stops as soon as it finds an interpreter that matches your spec. flowchart TD Start["get_interpreter()"] --> AbsPath{"Is spec an
absolute path?"} AbsPath -->|Yes| TryAbs["Use path directly"] - AbsPath -->|No| TryFirst["try_first_with paths"] - TryFirst --> RelPath{"Does spec name a
relative directory?"} + AbsPath -->|No| RelPath{"Does spec name a
relative directory?"} RelPath -->|Yes| TryRel["Resolve relative to cwd"] - RelPath -->|No| Bare{"Bare executable name?"} + RelPath -->|No| TryFirst["try_first_with paths"] + TryFirst --> Bare{"Bare executable name?"} Bare -->|Yes| PATH Bare -->|No| Current["Current interpreter"] Current --> Win{"Windows?"} diff --git a/src/python_discovery/_discovery.py b/src/python_discovery/_discovery.py index a2667ac..c03ad85 100644 --- a/src/python_discovery/_discovery.py +++ b/src/python_discovery/_discovery.py @@ -127,15 +127,17 @@ def _propose_explicit( env: Mapping[str, str], tested_exes: set[str], ) -> Generator[tuple[PythonInfo | None, bool], None, None]: + # Unparsed bare names reach spec.path; only an explicit directory should search the working directory. Probe that + # path before try_first_with: only it can satisfy the spec, and a hint on the same file or base interpreter would + # otherwise fail first and get it skipped as a duplicate. + if spec.path is not None and os.path.dirname(spec.path) and (exe_raw := _check_exe(spec.path, tested_exes)): + yield PythonInfo.from_exe(exe_raw, cache, env=env), True + for py_exe in try_first_with: if Path(py_exe).exists() and (exe_raw := _check_exe(py_exe, tested_exes)): yield PythonInfo.from_exe(exe_raw, cache, env=env), True - if spec.path is not None: - # Unparsed bare names reach spec.path; only an explicit directory should search the working directory. - if os.path.dirname(spec.path) and (exe_raw := _check_exe(spec.path, tested_exes)): - yield PythonInfo.from_exe(exe_raw, cache, env=env), True - else: + if spec.path is None: yield from _propose_current_and_windows(spec, cache, env, tested_exes) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index b03ab33..bc5d229 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -307,6 +307,36 @@ def test_discovery_absolute_path_with_try_first( assert Path(interpreter.executable) == good_exe +@pytest.mark.parametrize( + "hint", + [ + pytest.param("sibling", id="sibling-venv"), + pytest.param("base", id="base-interpreter"), + pytest.param("case", id="other-case", marks=pytest.mark.skipif(not IS_WIN, reason="case-insensitive paths")), + ], +) +def test_discovery_relative_path_with_try_first( + tmp_path: Path, session_cache: DiskCache, monkeypatch: pytest.MonkeyPatch, hint: str +) -> None: + for name in ("good", "bad"): + check_call([sys.executable, "-m", "venv", "--without-pip", str(tmp_path / name)]) + monkeypatch.chdir(tmp_path) + spec = str(Path("good", _SCRIPTS_DIR, _PYTHON_NAME)) + good_exe = tmp_path / spec + first = { + "sibling": tmp_path / "bad" / _VENV_PYTHON, + "base": PythonInfo.current_system(session_cache).system_executable, + "case": str(good_exe).upper(), + }[hint] + env = {**os.environ, "PATH": "", "UV_PYTHON_INSTALL_DIR": str(tmp_path / "missing")} + + found = get_interpreter(spec, try_first_with=[str(first)], cache=session_cache, env=env) + listed = iter_interpreters(spec, try_first_with=[str(first)], cache=session_cache, env=env) + + assert found is not None + assert (Path(found.executable), [Path(info.executable) for info in listed]) == (good_exe, [good_exe]) + + def test_discovery_venvs_on_one_base_keep_their_executable(tmp_path: Path, session_cache: DiskCache) -> None: scripts_dir = _SCRIPTS_DIR exe_name = _PYTHON_NAME