Skip to content

Commit

Permalink
Update lockfile after setting minimum bounds in uv add (#6618)
Browse files Browse the repository at this point in the history
## Summary

If we update the project requirements, we _also_ need to update the
lockfile.

Closes #6614.
  • Loading branch information
charliermarsh authored Aug 25, 2024
1 parent 1c58072 commit 069b021
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
54 changes: 50 additions & 4 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()),
};
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }]
"###
);
});
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions crates/uv/tests/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]
Expand Down Expand Up @@ -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" },
]
Expand Down Expand Up @@ -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" },
]
Expand Down Expand Up @@ -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" },
]
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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" }]
Expand Down

0 comments on commit 069b021

Please sign in to comment.