Avoid creating .venv in uv add --frozen and uv add --no-sync` (#8980)
Some checks are pending
CI / cargo clippy | windows (push) Blocked by required conditions
CI / cargo dev generate-all (push) Blocked by required conditions
CI / cargo shear (push) Waiting to run
CI / check windows trampoline | i686 (push) Blocked by required conditions
CI / check windows trampoline | x86_64 (push) Blocked by required conditions
CI / test windows trampoline | i686 (push) Blocked by required conditions
CI / test windows trampoline | x86_64 (push) Blocked by required conditions
CI / typos (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / build binary | linux (push) Blocked by required conditions
CI / build binary | macos aarch64 (push) Blocked by required conditions
CI / build binary | macos x86_64 (push) Blocked by required conditions
CI / check system | conda3.8 on linux (push) Blocked by required conditions
CI / cargo clippy | ubuntu (push) Blocked by required conditions
CI / check system | conda3.11 on macos (push) Blocked by required conditions
CI / integration test | conda on ubuntu (push) Blocked by required conditions
CI / integration test | free-threaded on linux (push) Blocked by required conditions
CI / check system | conda3.8 on macos (push) Blocked by required conditions
CI / integration test | github actions (push) Blocked by required conditions
CI / integration test | determine publish changes (push) Blocked by required conditions
CI / check system | alpine (push) Blocked by required conditions
CI / check system | python on macos aarch64 (push) Blocked by required conditions
CI / check system | homebrew python on macos aarch64 (push) Blocked by required conditions
CI / check system | python on macos x86_64 (push) Blocked by required conditions
CI / check system | conda3.11 on windows (push) Blocked by required conditions
CI / check system | conda3.8 on windows (push) Blocked by required conditions
CI / check system | python3.10 on windows (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / lint (push) Waiting to run
CI / cargo test | ubuntu (push) Blocked by required conditions
CI / cargo test | macos (push) Blocked by required conditions
CI / cargo test | windows (push) Blocked by required conditions
CI / check windows trampoline | aarch64 (push) Blocked by required conditions
CI / check system | python3.10 on windows x86 (push) Blocked by required conditions
CI / build binary | windows (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / build binary | freebsd (push) Blocked by required conditions
CI / ecosystem test | prefecthq/prefect (push) Blocked by required conditions
CI / ecosystem test | pallets/flask (push) Blocked by required conditions
CI / integration test | free-threaded on windows (push) Blocked by required conditions
CI / integration test | pypy on ubuntu (push) Blocked by required conditions
CI / integration test | pypy on windows (push) Blocked by required conditions
CI / integration test | graalpy on ubuntu (push) Blocked by required conditions
CI / integration test | graalpy on windows (push) Blocked by required conditions
CI / integration test | uv publish (push) Blocked by required conditions
CI / check cache | ubuntu (push) Blocked by required conditions
CI / check cache | macos aarch64 (push) Blocked by required conditions
CI / check system | python on debian (push) Blocked by required conditions
CI / check system | python on fedora (push) Blocked by required conditions
CI / check system | python on ubuntu (push) Blocked by required conditions
CI / check system | python on opensuse (push) Blocked by required conditions
CI / check system | python on rocky linux 8 (push) Blocked by required conditions
CI / check system | python on rocky linux 9 (push) Blocked by required conditions
CI / check system | pypy on ubuntu (push) Blocked by required conditions
CI / check system | pyston (push) Blocked by required conditions
CI / check system | python3.13 on windows (push) Blocked by required conditions
CI / check system | python3.12 via chocolatey (push) Blocked by required conditions
CI / check system | python3.9 via pyenv (push) Blocked by required conditions
CI / check system | python3.13 (push) Blocked by required conditions
CI / check system | conda3.11 on linux (push) Blocked by required conditions
CI / check system | amazonlinux (push) Blocked by required conditions
CI / check system | embedded python3.10 on windows (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

## Summary

Closes https://github.com/astral-sh/uv/issues/8977.
This commit is contained in:
Charlie Marsh 2024-11-09 21:21:12 -05:00 committed by GitHub
parent 73ad9f9a07
commit a2e90b74bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 77 additions and 27 deletions

View file

@ -44,7 +44,8 @@ use crate::commands::pip::loggers::{
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::project::lock::LockMode; use crate::commands::project::lock::LockMode;
use crate::commands::project::{ use crate::commands::project::{
init_script_python_requirement, validate_script_requires_python, ProjectError, ScriptPython, init_script_python_requirement, validate_script_requires_python, ProjectError,
ProjectInterpreter, ScriptPython,
}; };
use crate::commands::reporters::{PythonDownloadReporter, ResolverReporter}; use crate::commands::reporters::{PythonDownloadReporter, ResolverReporter};
use crate::commands::{diagnostics, pip, project, ExitStatus, SharedState}; use crate::commands::{diagnostics, pip, project, ExitStatus, SharedState};
@ -214,22 +215,43 @@ pub(crate) async fn add(
} }
} }
// Discover or create the virtual environment. if frozen || no_sync {
let venv = project::get_or_init_environment( // Discover the interpreter.
project.workspace(), let interpreter = ProjectInterpreter::discover(
python.as_deref().map(PythonRequest::parse), project.workspace(),
python_preference, project_dir,
python_downloads, python.as_deref().map(PythonRequest::parse),
connectivity, python_preference,
native_tls, python_downloads,
allow_insecure_host, connectivity,
no_config, native_tls,
cache, allow_insecure_host,
printer, no_config,
) cache,
.await?; printer,
)
.await?
.into_interpreter();
Target::Project(project, venv) Target::Project(project, Box::new(PythonTarget::Interpreter(interpreter)))
} else {
// Discover or create the virtual environment.
let venv = project::get_or_init_environment(
project.workspace(),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
connectivity,
native_tls,
allow_insecure_host,
no_config,
cache,
printer,
)
.await?;
Target::Project(project, Box::new(PythonTarget::Environment(venv)))
}
}; };
let client_builder = BaseClientBuilder::new() let client_builder = BaseClientBuilder::new()
@ -576,8 +598,8 @@ pub(crate) async fn add(
} }
}; };
let (project, venv) = match target { let (project, environment) = match target {
Target::Project(project, venv) => (project, venv), Target::Project(project, environment) => (project, environment),
// If `--script`, exit early. There's no reason to lock and sync. // If `--script`, exit early. There's no reason to lock and sync.
Target::Script(script, _) => { Target::Script(script, _) => {
writeln!( writeln!(
@ -627,10 +649,9 @@ pub(crate) async fn add(
project, project,
&mut toml, &mut toml,
&edits, &edits,
&venv, &environment,
state, state,
locked, locked,
no_sync,
&dependency_type, &dependency_type,
raw_sources, raw_sources,
settings.as_ref(), settings.as_ref(),
@ -688,10 +709,9 @@ async fn lock_and_sync(
mut project: VirtualProject, mut project: VirtualProject,
toml: &mut PyProjectTomlMut, toml: &mut PyProjectTomlMut,
edits: &[DependencyEdit], edits: &[DependencyEdit],
venv: &PythonEnvironment, environment: &PythonTarget,
state: SharedState, state: SharedState,
locked: bool, locked: bool,
no_sync: bool,
dependency_type: &DependencyType, dependency_type: &DependencyType,
raw_sources: bool, raw_sources: bool,
settings: ResolverInstallerSettingsRef<'_>, settings: ResolverInstallerSettingsRef<'_>,
@ -704,9 +724,9 @@ async fn lock_and_sync(
printer: Printer, printer: Printer,
) -> Result<(), ProjectError> { ) -> Result<(), ProjectError> {
let mode = if locked { let mode = if locked {
LockMode::Locked(venv.interpreter()) LockMode::Locked(environment.interpreter())
} else { } else {
LockMode::Write(venv.interpreter()) LockMode::Write(environment.interpreter())
}; };
let mut lock = project::lock::do_safe_lock( let mut lock = project::lock::do_safe_lock(
@ -846,9 +866,10 @@ async fn lock_and_sync(
} }
} }
if no_sync { let PythonTarget::Environment(venv) = environment else {
// If we're not syncing, exit early.
return Ok(()); return Ok(());
} };
// Sync the environment. // Sync the environment.
let (extras, dev) = match dependency_type { let (extras, dev) = match dependency_type {
@ -1024,8 +1045,9 @@ fn resolve_requirement(
enum Target { enum Target {
/// A PEP 723 script, with inline metadata. /// A PEP 723 script, with inline metadata.
Script(Pep723Script, Box<Interpreter>), Script(Pep723Script, Box<Interpreter>),
/// A project with a `pyproject.toml`. /// A project with a `pyproject.toml`.
Project(VirtualProject, PythonEnvironment), Project(VirtualProject, Box<PythonTarget>),
} }
impl Target { impl Target {
@ -1038,6 +1060,24 @@ impl Target {
} }
} }
/// A Python [`Interpreter`] or [`PythonEnvironment`] for a project.
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum PythonTarget {
Interpreter(Interpreter),
Environment(PythonEnvironment),
}
impl PythonTarget {
/// Return the [`Interpreter`] for the project.
fn interpreter(&self) -> &Interpreter {
match self {
Self::Interpreter(interpreter) => interpreter,
Self::Environment(venv) => venv.interpreter(),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct DependencyEdit { struct DependencyEdit {
dependency_type: DependencyType, dependency_type: DependencyType,

View file

@ -3675,6 +3675,9 @@ fn add_puts_default_indentation_in_pyproject_toml_if_not_observed() -> Result<()
fn add_frozen() -> Result<()> { fn add_frozen() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
// Remove the virtual environment.
fs_err::remove_dir_all(&context.venv)?;
let pyproject_toml = context.temp_dir.child("pyproject.toml"); let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#" pyproject_toml.write_str(indoc! {r#"
[project] [project]
@ -3694,6 +3697,7 @@ fn add_frozen() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
"###); "###);
let pyproject_toml = context.read("pyproject.toml"); let pyproject_toml = context.read("pyproject.toml");
@ -3719,6 +3723,7 @@ fn add_frozen() -> Result<()> {
}); });
assert!(!context.temp_dir.join("uv.lock").exists()); assert!(!context.temp_dir.join("uv.lock").exists());
assert!(!context.venv.exists());
Ok(()) Ok(())
} }
@ -3728,6 +3733,9 @@ fn add_frozen() -> Result<()> {
fn add_no_sync() -> Result<()> { fn add_no_sync() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
// Remove the virtual environment.
fs_err::remove_dir_all(&context.venv)?;
let pyproject_toml = context.temp_dir.child("pyproject.toml"); let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#" pyproject_toml.write_str(indoc! {r#"
[project] [project]
@ -3747,6 +3755,7 @@ fn add_no_sync() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
Resolved 4 packages in [TIME] Resolved 4 packages in [TIME]
"###); "###);
@ -3773,6 +3782,7 @@ fn add_no_sync() -> Result<()> {
}); });
assert!(context.temp_dir.join("uv.lock").exists()); assert!(context.temp_dir.join("uv.lock").exists());
assert!(!context.venv.exists());
Ok(()) Ok(())
} }