From 674d167a5089cba5e3d8940dda0b6f59b8ba0b70 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Fri, 24 May 2024 12:18:49 -0400 Subject: [PATCH] Fix interpreter cache collisions for relative virtualenv paths (#3823) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3784 The cache did not use an absolute path. I'm not sure this is actually a new bug, as this code wasn't touched in #3266 but perhaps there was a slight difference in the paths we were passing around. Note, just canonicalizing the path as soon as we see it doesn't work because then we jump out of the virtual environmnent into the system interpreter. ## Test plan ``` ❯ uv venv Using Python 3.12.3 interpreter at: /opt/homebrew/opt/python@3.12/bin/python3.12 Creating virtualenv at: .venv Activate with: source .venv/bin/activate ❯ uv pip install anyio Resolved 3 packages in 81ms Installed 3 packages in 4ms + anyio==4.3.0 + idna==3.7 + sniffio==1.3.1 ❯ mkdir uv-issue-3784 && cd uv-issue-3784 ❯ uv venv Using Python 3.12.3 interpreter at: /opt/homebrew/opt/python@3.12/bin/python3.12 Creating virtualenv at: .venv Activate with: source .venv/bin/activate ❯ gcm Switched to branch 'main' Your branch is up to date with 'origin/main'. ❯ cargo run -q -- pip list -v -p .venv DEBUG Checking for Python interpreter in directory `.venv` TRACE Cached interpreter info for Python 3.12.3, skipping probing: .venv/bin/python3 DEBUG Using Python 3.12.3 environment at .venv/bin/python3 Package Version ------- ------- anyio 4.3.0 idna 3.7 sniffio 1.3.1 ❯ cd uv-issue-3784 ❯ cargo run -q -- pip list -v -p .venv DEBUG Checking for Python interpreter in directory `.venv` TRACE Cached interpreter info for Python 3.12.3, skipping probing: .venv/bin/python3 DEBUG Using Python 3.12.3 environment at /Users/zb/workspace/uv/.venv/bin/python3 Package Version ------- ------- anyio 4.3.0 idna 3.7 sniffio 1.3.1 ❯ cd .. ❯ gco zb/fix-relative-venv Switched to branch 'zb/fix-relative-venv' ❯ cargo run -q -- pip list -v -p .venv DEBUG Checking for Python interpreter in directory `.venv` TRACE Cached interpreter info for Python 3.12.3, skipping probing: .venv/bin/python3 DEBUG Using Python 3.12.3 environment at .venv/bin/python3 Package Version ------- ------- anyio 4.3.0 idna 3.7 sniffio 1.3.1 ❯ cd uv-issue-3784 ❯ cargo run -q -- pip list -v -p .venv DEBUG Checking for Python interpreter in directory `.venv` TRACE Cached interpreter info for Python 3.12.3, skipping probing: .venv/bin/python3 DEBUG Using Python 3.12.3 environment at .venv/bin/python3 ``` --- crates/uv-interpreter/src/interpreter.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/uv-interpreter/src/interpreter.rs b/crates/uv-interpreter/src/interpreter.rs index f5bc4752f929..551b1721cdb2 100644 --- a/crates/uv-interpreter/src/interpreter.rs +++ b/crates/uv-interpreter/src/interpreter.rs @@ -595,9 +595,13 @@ impl InterpreterInfo { let cache_entry = cache.entry( CacheBucket::Interpreter, "", - format!("{}.msgpack", digest(&executable)), + // We use the absolute path for the cache entry to avoid cache collisions for relative paths + // but we do not want to query the executable with symbolic links resolved + format!("{}.msgpack", digest(&uv_fs::absolutize_path(executable)?)), ); + // We check the timestamp of the canonicalized executable to check if an underlying + // interpreter has been modified let modified = Timestamp::from_path(uv_fs::canonicalize_executable(executable)?)?; // Read from the cache.