Skip to content

🐛 fix(discovery): let a relative path beat try_first_with - #261

Open
darrenhuai wants to merge 2 commits into
tox-dev:mainfrom
darrenhuai:fix/relative-spec-before-try-first
Open

darrenhuai wants to merge 2 commits into
tox-dev:mainfrom
darrenhuai:fix/relative-spec-before-try-first

Conversation

@darrenhuai

Copy link
Copy Markdown
Contributor

get_interpreter can't find an interpreter given as a relative path when try_first_with holds a hint for the same install. On Windows, in a folder with two uv-made 3.12 environments, venv-a and venv-b, I asked current main for venv-b\Scripts\python.exe with different hints (results printed relative to the folder):

no hint                    -> venv-b\Scripts\python.exe
venv-a python              -> None
base 3.12 python           -> None
venv-b python, upper case  -> None

It isn't Windows-only. In WSL, with two python3 -m venv environments on the system 3.12, main returns None from get_interpreter and [] from iter_interpreters for venv-b/bin/python when the hint is venv-a/bin/python, venv-b/bin/python3 or /usr/bin/python3. (On Windows, iter_interpreters still lists venv-b for the sibling and base hints.) A hint with the exact spec path works on both.

virtualenv 21.15.6 with python-discovery 1.9.5 agrees: -p venv-b\Scripts\python.exe works, and adding --try-first-with with venv-a's python fails with RuntimeError: failed to find interpreter for Builtin discover of python_spec='venv-b\\Scripts\\python.exe'. With this branch on the same virtualenv, the --try-first-with run creates the environment.

A spec with a directory part can only match abspath(spec.path), but _propose_explicit probed the hints first, and one of two dedups then hid the spec path. _check_exe skips a path that resolves to one already tested (a POSIX venv symlinked to its base, or the same file in another case on Windows). _find_interpreter skips a candidate whose system_executable was already rejected, so a failed hint on the same base drops the spec's own venv.

_propose_explicit now probes a spec path with a directory part before the hints. Nothing else can satisfy that spec, so the order change can't pick a different interpreter. Absolute paths still return early (pypa/virtualenv#2921 added that so a hint can't override an absolute path) and bare names keep their order.

The new test is a relative twin of test_discovery_absolute_path_with_try_first. It checks get_interpreter and iter_interpreters with a sibling venv, the base interpreter and, on Windows, an upper-case spelling of the path as the hint. All three fail on main and pass here on Windows with 3.14, and the sibling and base cases do the same in WSL with 3.12 and 3.14. I also updated the docs/explanation.rst flowchart to the new order.

A spec like venv/bin/python can only be satisfied by that exact file, but
it was probed after the try_first_with entries. When a hint resolved to
the same file (a symlinked venv on POSIX, a different case on Windows) the
spec path was skipped as already tested, and when a hint shared its base
interpreter (the base itself or a sibling venv) the spec's own venv was
skipped once the hint failed the path check. Discovery then returned None.

Probe a spec path that has a directory part before the hints. Absolute
paths and bare names behave as before.
A hint with the exact spec path already worked. What failed was the same
file reached another way: venv/bin/python3 next to venv/bin/python on
POSIX, or a different case on Windows.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant