Skip to content

Commit 9a04b8f

Browse files
Omit redundant runtime constraints from lockfiles (#22004)
## Summary With `resolution-inputs`, omit redundant runtime constraints from `uv.lock`. We can omit a plain version bound when every locked version of its package is stable and satisfies it. For example, changing `foo<3` to `foo<4` can keep a locked `foo==2` without rewriting the lockfile. We apply this rule to each constraint separately, and retain constraints tied to specific sources, constraints that enable pre-releases, and a few other cases. An extra-marked constraint can be omitted when its bound satisfies the rule; otherwise, keeping the declaration lets unchanged inputs reuse the lock without reconstructing the original extra context.
1 parent 89d689b commit 9a04b8f

6 files changed

Lines changed: 2211 additions & 268 deletions

File tree

‎crates/uv-dev/src/generate_preview_features_reference.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ mod tests {
156156
- <a id="pylock" href="#pylock"><code>pylock</code></a>: Allows installing from `pylock.toml` files.
157157
- <a id="python-install-default" href="#python-install-default"><code>python-install-default</code></a>: Allows [installing `python` and `python3` executables](./python-versions.md#installing-python-executables).
158158
- <a id="relocatable-envs-default" href="#relocatable-envs-default"><code>relocatable-envs-default</code></a>: Creates relocatable virtual environments by default.
159-
- <a id="resolution-inputs" href="#resolution-inputs"><code>resolution-inputs</code></a>: Records runtime configuration consultations and omits unused constraints, overrides, exclusions,
159+
- <a id="resolution-inputs" href="#resolution-inputs"><code>resolution-inputs</code></a>: Omits redundant runtime constraints and records consultations to omit unused overrides, exclusions,
160160
dependency metadata, and package-specific upload cutoffs from the lockfile.
161161
- <a id="s3-endpoint" href="#s3-endpoint"><code>s3-endpoint</code></a>: Allows signing requests to configured S3-compatible endpoints.
162162
- <a id="sbom-export" href="#sbom-export"><code>sbom-export</code></a>: Allows using `uv export --format=cyclonedx1.5`.

‎crates/uv-lock/src/lock/inputs.rs‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use std::collections::BTreeSet;
2+
use std::mem;
23

3-
use uv_configuration::{ExcludeDependency, Override};
4-
use uv_distribution_types::{Requirement, ResolutionLookups, StaticMetadata};
4+
use uv_configuration::{ExcludeDependency, NormalizedConstraints, Override};
5+
use uv_distribution_types::{Requirement, RequirementSource, ResolutionLookups, StaticMetadata};
56
use uv_normalize::PackageName;
7+
use uv_pep440::VersionSpecifier;
8+
use uv_preview::PreviewFeature;
69

10+
use super::requirements::normalize_collection;
711
use super::{Lock, Package};
812

913
impl Lock {
@@ -34,6 +38,50 @@ impl Lock {
3438
);
3539
self
3640
}
41+
42+
/// Omit redundant constraints before normalizing the remaining declarations.
43+
pub(super) fn prune_constraints(mut self) -> Self {
44+
let constraints = mem::take(&mut self.manifest.constraints);
45+
self.manifest.constraints = normalize_collection::<_, NormalizedConstraints>(
46+
constraints
47+
.into_iter()
48+
.filter(|constraint| !self.can_omit_constraint(constraint)),
49+
uv_preview::is_enabled(PreviewFeature::LockfileNormalization),
50+
);
51+
self
52+
}
53+
54+
/// Return whether a plain version bound is satisfied by every stable locked version.
55+
///
56+
/// Source declarations and prerelease specifiers can affect candidate eligibility, so their
57+
/// changes must trigger resolution. Bounds on dynamic or prerelease versions also remain
58+
/// recorded. Check every version regardless of markers: an inapplicable bound that does not
59+
/// contain a locked version must be retained to avoid resolving unchanged inputs repeatedly.
60+
pub(super) fn can_omit_constraint(&self, constraint: &Requirement) -> bool {
61+
let RequirementSource::Registry {
62+
specifier,
63+
index: None,
64+
..
65+
} = &constraint.source
66+
else {
67+
return false;
68+
};
69+
if specifier.is_empty() {
70+
return true;
71+
}
72+
if specifier.iter().any(VersionSpecifier::any_prerelease) {
73+
return false;
74+
}
75+
self.packages_for_name(&constraint.name)
76+
.iter()
77+
.all(|package| {
78+
package
79+
.id
80+
.version
81+
.as_ref()
82+
.is_some_and(|version| !version.any_prerelease() && specifier.contains(version))
83+
})
84+
}
3785
}
3886

3987
/// Names whose settings participate in lockfile retention or validation.

‎crates/uv-lock/src/lock/mod.rs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,11 @@ impl Lock {
26322632
vec![],
26332633
fork_markers,
26342634
)?;
2635+
let lock = if uv_preview::is_enabled(PreviewFeature::ResolutionInputs) {
2636+
lock.prune_constraints()
2637+
} else {
2638+
lock
2639+
};
26352640
Ok(if metadata_free {
26362641
lock.without_package_metadata()
26372642
} else {
@@ -4101,15 +4106,25 @@ impl Lock {
41014106
}
41024107

41034108
let filter = ManifestFilter::from_lock(self);
4109+
let omit_constraints = uv_preview::is_enabled(PreviewFeature::ResolutionInputs);
41044110

41054111
let normalized_constraints = {
41064112
let expected = normalizer.constraints(
41074113
constraints
41084114
.iter()
4109-
.filter(|entry| filter.includes_constraint(entry))
4115+
.filter(|entry| {
4116+
filter.includes_constraint(entry)
4117+
&& (!omit_constraints || !self.can_omit_constraint(entry))
4118+
})
4119+
.cloned(),
4120+
)?;
4121+
let actual = normalizer.constraints(
4122+
self.manifest
4123+
.constraints
4124+
.iter()
4125+
.filter(|entry| !omit_constraints || !self.can_omit_constraint(entry))
41104126
.cloned(),
41114127
)?;
4112-
let actual = normalizer.constraints(self.manifest.constraints.iter().cloned())?;
41134128
if expected != actual {
41144129
return Ok(SatisfiesResult::MismatchedConstraints(
41154130
expected.into_iter().collect(),
@@ -6074,7 +6089,11 @@ impl ResolverManifest {
60746089
requirements,
60756090
normalize,
60766091
),
6077-
constraints: normalize_collection::<_, NormalizedConstraints>(constraints, normalize),
6092+
// Prune individual constraints before combining their bounds in preview mode.
6093+
constraints: normalize_collection::<_, NormalizedConstraints>(
6094+
constraints,
6095+
normalize && !uv_preview::is_enabled(PreviewFeature::ResolutionInputs),
6096+
),
60786097
overrides: normalize_collection::<_, NormalizedOverrideEntries>(overrides, normalize),
60796098
excludes: normalize_collection::<_, NormalizedExcludes>(excludes, normalize),
60806099
build_constraints: build_constraints.into_iter().collect(),

‎crates/uv-preview/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ pub enum PreviewFeature {
340340
/// Exclude `exclude-newer-package` entries from the lockfile when not included in the
341341
/// project's resolved dependencies.
342342
MissingExcludeNewerPackageLock,
343-
/// Records runtime configuration consultations and omits unused constraints, overrides, exclusions,
343+
/// Omits redundant runtime constraints and records consultations to omit unused overrides, exclusions,
344344
/// dependency metadata, and package-specific upload cutoffs from the lockfile.
345345
ResolutionInputs,
346346
/// Allows using `uv export --batch`.

0 commit comments

Comments
 (0)