From 1cd6a12fcb6eb1e05a00be699e05b44c69fb58f9 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 7 Jan 2025 14:44:27 +0100 Subject: [PATCH] Don't revalidate Python cache entry with `--upgrade` We were always revalidating the Python cache entry when using `--upgrade`, since the cache timestamp is too recent. Instead, we ignore the usual caching semantics for packages and only compare the timestamp of the interpreter with the recorded timestamp. This avoids the cost for querying the Python interpreter, and the expense of being slightly inconsistent with `Cache` behaving different for python alone (which is imho acceptable since interpreter metadata is already different from package metadata). --- crates/uv-python/src/interpreter.rs | 47 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/crates/uv-python/src/interpreter.rs b/crates/uv-python/src/interpreter.rs index e725fbccd0f0..fc056b0bf553 100644 --- a/crates/uv-python/src/interpreter.rs +++ b/crates/uv-python/src/interpreter.rs @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize}; use thiserror::Error; use tracing::{trace, warn}; -use uv_cache::{Cache, CacheBucket, CachedByTimestamp, Freshness}; +use uv_cache::{Cache, CacheBucket, CachedByTimestamp}; use uv_cache_info::Timestamp; use uv_cache_key::cache_digest; use uv_fs::{write_atomic_sync, PythonExt, Simplified}; @@ -762,34 +762,31 @@ impl InterpreterInfo { })?; // Read from the cache. - if cache - .freshness(&cache_entry, None) - .is_ok_and(Freshness::is_fresh) - { - if let Ok(data) = fs::read(cache_entry.path()) { - match rmp_serde::from_slice::>(&data) { - Ok(cached) => { - if cached.timestamp == modified { - trace!( - "Cached interpreter info for Python {}, skipping probing: {}", - cached.data.markers.python_full_version(), - executable.user_display() - ); - return Ok(cached.data); - } - + // We ignore the freshness of the cache entry and use only our own logic, otherwise + // `--upgrade` always implies querying Python. + if let Ok(data) = fs::read(cache_entry.path()) { + match rmp_serde::from_slice::>(&data) { + Ok(cached) => { + if cached.timestamp == modified { trace!( - "Ignoring stale interpreter markers for: {}", + "Cached interpreter info for Python {}, skipping probing: {}", + cached.data.markers.python_full_version(), executable.user_display() ); + return Ok(cached.data); } - Err(err) => { - warn!( - "Broken interpreter cache entry at {}, removing: {err}", - cache_entry.path().user_display() - ); - let _ = fs_err::remove_file(cache_entry.path()); - } + + trace!( + "Ignoring stale interpreter markers for: {}", + executable.user_display() + ); + } + Err(err) => { + warn!( + "Broken interpreter cache entry at {}, removing: {err}", + cache_entry.path().user_display() + ); + let _ = fs_err::remove_file(cache_entry.path()); } } }