Skip to content

🐛 fix(cache): key interpreter caches on absolute paths - #296

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
JonathanLStoff:fix/relative-exe-cache
Dec 4, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
JonathanLStoff:fix/relative-exe-cache

Conversation

@JonathanLStoff

@JonathanLStoff JonathanLStoff commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

PythonInfo.from_exe keyed its in-memory and disk caches on the executable path as given. A relative path such as venv/bin/python therefore hit the entry stored from another working directory and returned that directory's interpreter. The disk cache made the same mistake whenever the two executables had the same modification time, as POSIX virtual environments symlinking one system Python do. Separately, a lookup through another spelling of a cached path, such as bin/./python, which the key treats as the same path because pathlib drops . segments, overwrote executable on the shared cached object, which changed PythonInfo.current() and every result returned earlier.

🐛 The cache key is now the path made absolute against the current working directory, without resolving symlinks or collapsing .., so distinct virtual environments keep distinct entries. When the requested spelling differs from the cached one, the result is a copy carrying the requested executable, as resolve_to_system already returns.

$ python -m venv --without-pip first/venv second/venv
$ cat run.py
import os
import sys

from python_discovery import PythonInfo

current = PythonInfo.current()
os.chdir("first")
first = PythonInfo.from_exe("venv/bin/python", resolve_to_host=False)
os.chdir("../second")
second = PythonInfo.from_exe("venv/bin/python", resolve_to_host=False)
print(first.prefix, second.prefix, sep="\n")
head, tail = os.path.split(sys.executable)
PythonInfo.from_exe(f"{head}/./{tail}", resolve_to_host=False)
print(PythonInfo.current().executable)
$ /venv/bin/python run.py
 /work/first/venv
-/work/first/venv
-/venv/bin/./python
+/work/second/venv
+/venv/bin/python

JonathanLStoff and others added 2 commits December 2, 2026 13:26
Path.absolute anchors a relative executable at the working directory
without the ".." collapsing of os.path.abspath, which can step out of a
symlinked directory, and needs no lint suppression. The tests build both
virtual environments in one venv call, compare their results in one
assert, and no longer patch the private query function.
@gaborbernat gaborbernat added the bug Something isn't working label Dec 4, 2026
@gaborbernat gaborbernat changed the title fix(cache): key interpreter caches on absolute paths without mutating shared entries 🐛 fix(cache): key interpreter caches on absolute paths Dec 4, 2026
@gaborbernat
gaborbernat merged commit a47ef3d into tox-dev:main Dec 4, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants