Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ pub enum ProjectCommand {
/// present, its contents will be used as preferences for the resolution.
///
/// If there are no changes to the project's dependencies, locking will have no effect unless
/// the `--upgrade` flag is provided.
/// the `--upgrade` or `--rewrite` flag is provided.
#[command(
after_help = "Use `uv help lock` for more details.",
after_long_help = ""
Expand Down Expand Up @@ -4194,6 +4194,11 @@ pub struct LockArgs {
)]
pub dry_run: bool,

/// Require a re-resolution and rewrite of the lockfile, even if the dependencies
/// or requirements have not changed.
#[arg(long, conflicts_with_all = ["check", "locked", "check_exists", "dry_run"])]
pub rewrite: bool,

/// Lock the specified Python script, rather than the current project.
///
/// If provided, uv will lock the script (based on its inline metadata table, in adherence with
Expand Down
22 changes: 22 additions & 0 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub(crate) async fn lock(
lock_check: LockCheck,
frozen: Option<FrozenSource>,
dry_run: DryRun,
rewrite: bool,
refresh: Refresh,
python: Option<String>,
install_mirrors: PythonInstallMirrors,
Expand Down Expand Up @@ -216,6 +217,7 @@ pub(crate) async fn lock(
preview,
)
.with_refresh(&refresh)
.with_rewrite(rewrite)
.execute(target),
)
.await
Expand Down Expand Up @@ -308,6 +310,7 @@ pub(crate) struct LockOperation<'env> {
workspace_cache: &'env WorkspaceCache,
printer: Printer,
preview: Preview,
rewrite: bool,
}

impl<'env> LockOperation<'env> {
Expand Down Expand Up @@ -337,6 +340,7 @@ impl<'env> LockOperation<'env> {
workspace_cache,
printer,
preview,
rewrite: false,
}
}

Expand All @@ -357,6 +361,13 @@ impl<'env> LockOperation<'env> {
self
}

/// Set the rewrite strategy for the [`LockOperation`].
#[must_use]
pub(crate) fn with_rewrite(mut self, rewrite: bool) -> Self {
self.rewrite = rewrite;
self
}

/// Perform a [`LockOperation`].
pub(crate) async fn execute(self, target: LockTarget<'_>) -> Result<LockResult, ProjectError> {
match self.mode {
Expand Down Expand Up @@ -405,6 +416,7 @@ impl<'env> LockOperation<'env> {
self.workspace_cache,
self.printer,
self.preview,
self.rewrite,
))
.await?;

Expand Down Expand Up @@ -449,6 +461,7 @@ impl<'env> LockOperation<'env> {
self.workspace_cache,
self.printer,
self.preview,
self.rewrite,
))
.await?;

Expand Down Expand Up @@ -481,6 +494,7 @@ async fn do_lock(
workspace_cache: &WorkspaceCache,
printer: Printer,
preview: Preview,
rewrite: bool,
) -> Result<LockResult, ProjectError> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -849,6 +863,7 @@ async fn do_lock(
state.index(),
&database,
printer,
rewrite,
)
.await
{
Expand Down Expand Up @@ -1064,6 +1079,7 @@ impl ValidatedLock {
index: &InMemoryIndex,
database: &DistributionDatabase<'_, Context>,
printer: Printer,
rewrite: bool,
) -> Result<Self, ProjectError> {
// Perform checks in a deliberate order, such that the most extreme conditions are tested
// first (i.e., every check that returns `Self::Unusable`, followed by every check that
Expand Down Expand Up @@ -1242,6 +1258,12 @@ impl ValidatedLock {
Some(index_locations)
};

// If the user specified `--rewrite`, then we have to re-resolve.
if rewrite {
debug!("Resolving with existing lockfile due to `--rewrite`");
return Ok(Self::Preferable(lock));
}

// Determine whether the lockfile satisfies the workspace requirements.
match lock
.satisfies(
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,7 @@ async fn run_project(
args.lock_check,
args.frozen,
args.dry_run,
args.rewrite,
args.refresh,
args.python,
args.install_mirrors,
Expand Down
13 changes: 13 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@ pub(crate) struct LockSettings {
pub(crate) lock_check: LockCheck,
pub(crate) frozen: Option<FrozenSource>,
pub(crate) dry_run: DryRun,
pub(crate) rewrite: bool,
pub(crate) script: Option<PathBuf>,
pub(crate) python: Option<String>,
pub(crate) install_mirrors: PythonInstallMirrors,
Expand All @@ -1799,6 +1800,7 @@ impl LockSettings {
locked,
check_exists,
dry_run,
rewrite,
script,
resolver,
build,
Expand All @@ -1818,6 +1820,16 @@ impl LockSettings {
// Check for conflicts between locked and frozen.
check_conflicts(locked, frozen);

// Check for conflicts between rewrite (CLI only for now) and locked, and rewrite and frozen.
let rewrite_flag = if rewrite {
Flag::from_cli("rewrite")
} else {
Flag::Disabled
};

check_conflicts(rewrite_flag, locked);
check_conflicts(rewrite_flag, frozen);

let lock_check = if check {
LockCheck::Enabled(LockCheckSource::Check)
} else {
Expand All @@ -1828,6 +1840,7 @@ impl LockSettings {
lock_check,
frozen: resolve_frozen(frozen),
dry_run: DryRun::from_args(dry_run),
rewrite,
script,
python: python.and_then(Maybe::into_option),
refresh: Refresh::from(refresh),
Expand Down
Loading
Loading