This commit is contained in:
Zanie Blue 2025-07-02 12:42:30 -05:00
parent a9c4ef293f
commit 9a66675d7f

View file

@ -44,6 +44,8 @@ impl CachedEnvironment {
printer: Printer,
preview: PreviewMode,
) -> Result<Self, ProjectError> {
// Resolve the "base" interpreter, which resolves to an underlying parent interpreter if the
// given interpreter is a virtual environment.
let base_interpreter = Self::base_interpreter(interpreter, cache)?;
// Resolve the requirements with the interpreter.
@ -73,15 +75,34 @@ impl CachedEnvironment {
hash_digest(&distributions)
};
// Concatenate the hashes of the interpreter path and the sys prefix. We don't want to
// share ephemeral environments between two different project venvs, for example.
let interpreter_hash =
cache_digest(&canonicalize_executable(base_interpreter.sys_executable())?);
let base_venv_hash = cache_digest(&interpreter.sys_prefix().canonicalize()?);
let cache_dir_name = interpreter_hash + &base_venv_hash;
// Construct a hash for the environment.
//
// Use the canonicalized base interpreter path since that's the interpreter we performed the
// resolution with and the interpreter the environment will be created with.
//
// We also include the canonicalized `sys.prefix` of the non-base interpreter, that is, the
// virtual environment's path. Originally, we shared cached environments independent of the
// environment they'd be layered on top of. However, this causes collisions as the overlay
// `.pth` file can be overridden by another instance of uv. Including this element in the key
// avoids this problem at the cost of creating separate cached environments for identical
// `--with` invocations across projects. We use `sys.prefix` rather than `sys.executable` so
// we can canonicalize it without invalidating the purpose of the element — it'd probably be
// safe to just use the absolute `sys.executable` as well.
//
// TODO(zanieb): Since we're not sharing these environmments across projects, we should move
// [`CachedEvnvironment::set_overlay`] etc. here since the values there should be constant
// now.
//
// TODO(zanieb): We should include the version of the base interpreter in the hash, so if
// the interpreter at the canonicalized path changes versions we construct a new
// environment.
let environment_hash = cache_digest(&(
&canonicalize_executable(base_interpreter.sys_executable())?,
&interpreter.sys_prefix().canonicalize()?,
));
// Search in the content-addressed cache.
let cache_entry = cache.entry(CacheBucket::Environments, cache_dir_name, resolution_hash);
let cache_entry = cache.entry(CacheBucket::Environments, environment_hash, resolution_hash);
if cache.refresh().is_none() {
if let Ok(root) = cache.resolve_link(cache_entry.path()) {