diff --git a/crates/uv-build/src/lib.rs b/crates/uv-build/src/lib.rs index dcda79cf6992..c162e086f919 100644 --- a/crates/uv-build/src/lib.rs +++ b/crates/uv-build/src/lib.rs @@ -437,38 +437,22 @@ impl SourceBuild { let package_name = project.clone().map(|p| p.name); // Create a virtual environment, or install into the shared environment if requested. - let venv = match build_isolation { - BuildIsolation::Isolated => uv_virtualenv::create_venv( + let venv = if let Some(venv) = build_isolation.shared_environment(package_name.as_ref()) { + venv.clone() + } else { + uv_virtualenv::create_venv( temp_dir.path(), interpreter.clone(), uv_virtualenv::Prompt::None, false, false, false, - )?, - BuildIsolation::Shared(venv) => venv.clone(), - BuildIsolation::SharedPackage(venv, packages) => { - if package_name - .as_ref() - .is_some_and(|name| packages.iter().any(|p| p == name)) - { - venv.clone() - } else { - uv_virtualenv::create_venv( - temp_dir.path(), - interpreter.clone(), - uv_virtualenv::Prompt::None, - false, - false, - false, - )? - } - } + )? }; // Setup the build environment. If build isolation is disabled, we assume the build // environment is already setup. - if build_isolation.is_isolated(&package_name) { + if build_isolation.is_isolated(package_name.as_ref()) { let resolved_requirements = Self::get_resolved_requirements( build_context, source_build_context, @@ -518,7 +502,7 @@ impl SourceBuild { // Create the PEP 517 build environment. If build isolation is disabled, we assume the build // environment is already setup. let runner = PythonRunner::new(concurrent_builds); - if build_isolation.is_isolated(&package_name) { + if build_isolation.is_isolated(package_name.as_ref()) { if let Some(pep517_backend) = &pep517_backend { create_pep517_build_environment( &runner, diff --git a/crates/uv-types/src/builds.rs b/crates/uv-types/src/builds.rs index dda14b4ee790..deca2b4a0c83 100644 --- a/crates/uv-types/src/builds.rs +++ b/crates/uv-types/src/builds.rs @@ -11,14 +11,28 @@ pub enum BuildIsolation<'a> { } impl<'a> BuildIsolation<'a> { - /// Returns `true` if build isolation is enforced. - pub fn is_isolated(&self, package_name: &Option) -> bool { + /// Returns `true` if build isolation is enforced for the given package name. + pub fn is_isolated(&self, package: Option<&PackageName>) -> bool { match self { Self::Isolated => true, Self::Shared(_) => false, - Self::SharedPackage(_, packages) => match package_name { - Some(package_name) => !packages.iter().any(|p| p == package_name), - None => true, + Self::SharedPackage(_, packages) => package.map_or(true, |package| { + !packages.iter().any(|p| p == package) + }), + } + } + + /// Returns the shared environment for a given package, if build isolation is not enforced. + pub fn shared_environment(&self, package: Option<&PackageName>) -> Option<&PythonEnvironment> { + match self { + Self::Isolated => None, + Self::Shared(env) => Some(env), + Self::SharedPackage(env, packages) => if package.is_some_and(|package| { + packages.iter().any(|p| p == package) + }) { + Some(env) + } else { + None }, } }