Skip to content

Commit

Permalink
Relock
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 25, 2024
1 parent 1c58072 commit 811a648
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
41 changes: 39 additions & 2 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 @@ -463,7 +465,7 @@ pub(crate) async fn add(
.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 @@ -568,6 +570,41 @@ pub(crate) async fn add(
// specifier.
if modified {
fs_err::write(project.root().join("pyproject.toml"), toml.to_string())?;

// 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
4 changes: 2 additions & 2 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

0 comments on commit 811a648

Please sign in to comment.