From 277864ca6ecf23aa63dd59ac28962007273fbbf0 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 25 Aug 2024 16:40:02 -0400 Subject: [PATCH] Relock --- crates/uv/src/commands/project/add.rs | 54 +++++++++++++++++++++++++-- crates/uv/tests/edit.rs | 6 +-- crates/uv/tests/workflow.rs | 12 +++--- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index d8bf9b4ffd52..37f5f83d8306 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -32,7 +32,9 @@ use uv_workspace::pyproject::{DependencyType, Source, SourceError}; use uv_workspace::pyproject_mut::{ArrayEdit, DependencyTarget, PyProjectTomlMut}; use uv_workspace::{DiscoveryOptions, VirtualProject, Workspace}; -use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger}; +use crate::commands::pip::loggers::{ + DefaultInstallLogger, DefaultResolveLogger, SummaryResolveLogger, +}; use crate::commands::pip::operations::Modifications; use crate::commands::pip::resolution_environment; use crate::commands::project::ProjectError; @@ -457,13 +459,13 @@ pub(crate) async fn add( let existing = project.pyproject_toml(); // Update the `pypackage.toml` in-memory. - let project = project + let mut project = project .clone() .with_pyproject_toml(toml::from_str(&content)?) .context("Failed to update `pyproject.toml`")?; // Lock and sync the environment, if necessary. - let lock = match project::lock::do_safe_lock( + let mut lock = match project::lock::do_safe_lock( locked, frozen, project.workspace(), @@ -567,7 +569,51 @@ pub(crate) async fn add( // string content, since the above loop _must_ change an empty specifier to a non-empty // specifier. if modified { - fs_err::write(project.root().join("pyproject.toml"), toml.to_string())?; + let content = toml.to_string(); + + // Write the updated `pyproject.toml` to disk. + fs_err::write(project.root().join("pyproject.toml"), &content)?; + + // Update the `pypackage.toml` in-memory. + project = project + .clone() + .with_pyproject_toml(toml::from_str(&content)?) + .context("Failed to update `pyproject.toml`")?; + + // If the file was modified, we have to lock again, though the only expected change is + // the addition of the minimum version specifiers. + lock = match project::lock::do_safe_lock( + locked, + frozen, + project.workspace(), + venv.interpreter(), + settings.as_ref().into(), + Box::new(SummaryResolveLogger), + connectivity, + concurrency, + native_tls, + cache, + printer, + ) + .await + { + Ok(result) => result.into_lock(), + Err(ProjectError::Operation(pip::operations::Error::Resolve( + uv_resolver::ResolveError::NoSolution(err), + ))) => { + let header = err.header(); + let report = miette::Report::new(WithHelp { header, cause: err, help: Some("If this is intentional, run `uv add --frozen` to skip the lock and sync steps.") }); + anstream::eprint!("{report:?}"); + + // Revert the changes to the `pyproject.toml`, if necessary. + if modified { + fs_err::write(project.root().join("pyproject.toml"), existing)?; + } + + return Ok(ExitStatus::Failure); + } + Err(err) => return Err(err.into()), + }; } } diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index 6dc2251909eb..83606f8c0cd9 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -3051,7 +3051,7 @@ fn add_lower_bound_optional() -> Result<()> { ] [package.metadata] - requires-dist = [{ name = "anyio", marker = "extra == 'io'" }] + requires-dist = [{ name = "anyio", marker = "extra == 'io'", specifier = ">=4.3.0" }] [[package]] name = "sniffio" @@ -3142,7 +3142,7 @@ fn add_lower_bound_local() -> Result<()> { ] [package.metadata] - requires-dist = [{ name = "local-simple-a" }] + requires-dist = [{ name = "local-simple-a", specifier = ">=1.2.3" }] "### ); }); @@ -3227,7 +3227,7 @@ fn add_virtual() -> Result<()> { exclude-newer = "2024-03-25T00:00:00Z" [manifest] - requirements = [{ name = "iniconfig" }] + requirements = [{ name = "iniconfig", specifier = ">=2.0.0" }] [[package]] name = "iniconfig" diff --git a/crates/uv/tests/workflow.rs b/crates/uv/tests/workflow.rs index 8c02b06f24bb..fbff72598faa 100644 --- a/crates/uv/tests/workflow.rs +++ b/crates/uv/tests/workflow.rs @@ -69,7 +69,7 @@ fn packse_add_remove_one_package() -> Result<()> { { name = "pyyaml", specifier = ">=6.0.1" }, { name = "setuptools", specifier = ">=69.1.1" }, { name = "twine", specifier = ">=4.0.2" }, - + { name = "tzdata" }, + + { name = "tzdata", specifier = ">=2024.1" }, { name = "watchfiles", marker = "extra == 'serve'", specifier = ">=0.21.0" }, ] @@ -153,7 +153,7 @@ fn packse_add_remove_one_package() -> Result<()> { { name = "pyyaml", specifier = ">=6.0.1" }, { name = "setuptools", specifier = ">=69.1.1" }, { name = "twine", specifier = ">=4.0.2" }, - - { name = "tzdata" }, + - { name = "tzdata", specifier = ">=2024.1" }, { name = "watchfiles", marker = "extra == 'serve'", specifier = ">=0.21.0" }, ] @@ -303,7 +303,7 @@ fn packse_promote_transitive_to_direct_then_remove() -> Result<()> { { name = "pypiserver", marker = "extra == 'index'", specifier = ">=2.0.1" }, { name = "pyyaml", specifier = ">=6.0.1" }, { name = "setuptools", specifier = ">=69.1.1" }, - + { name = "sniffio" }, + + { name = "sniffio", specifier = ">=1.3.1" }, { name = "twine", specifier = ">=4.0.2" }, { name = "watchfiles", marker = "extra == 'serve'", specifier = ">=0.21.0" }, ] @@ -357,7 +357,7 @@ fn packse_promote_transitive_to_direct_then_remove() -> Result<()> { { name = "pypiserver", marker = "extra == 'index'", specifier = ">=2.0.1" }, { name = "pyyaml", specifier = ">=6.0.1" }, { name = "setuptools", specifier = ">=69.1.1" }, - - { name = "sniffio" }, + - { name = "sniffio", specifier = ">=1.3.1" }, { name = "twine", specifier = ">=4.0.2" }, { name = "watchfiles", marker = "extra == 'serve'", specifier = ">=0.21.0" }, ] @@ -476,7 +476,7 @@ fn jax_instability() -> Result<()> { -requires-dist = [{ name = "jax", specifier = "==0.4.17" }] +requires-dist = [ + { name = "jax", specifier = "==0.4.17" }, - + { name = "tzdata" }, + + { name = "tzdata", specifier = ">=2024.1" }, +] [[package]] @@ -528,7 +528,7 @@ fn jax_instability() -> Result<()> { [package.metadata] -requires-dist = [ - { name = "jax", specifier = "==0.4.17" }, - - { name = "tzdata" }, + - { name = "tzdata", specifier = ">=2024.1" }, -] +requires-dist = [{ name = "jax", specifier = "==0.4.17" }]