Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate uv.lock when virtual dev-dependencies change #6291

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
51 changes: 51 additions & 0 deletions crates/uv-resolver/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,26 @@ impl Lock {
);
}

if !self.manifest.requirements.is_empty() {
let requirements = self
.manifest
.requirements
.iter()
.map(|requirement| {
serde::Serialize::serialize(
&requirement,
toml_edit::ser::ValueSerializer::new(),
)
})
.collect::<Result<Vec<_>, _>>()?;
let requirements = match requirements.as_slice() {
[] => Array::new(),
[requirement] => Array::from_iter([requirement]),
requirements => each_element_on_its_line_array(requirements.iter()),
};
manifest_table.insert("requirements", value(requirements));
}

if !self.manifest.constraints.is_empty() {
let constraints = self
.manifest
Expand Down Expand Up @@ -657,6 +677,7 @@ impl Lock {
&self,
workspace: &Workspace,
members: &[PackageName],
requirements: &[Requirement],
constraints: &[Requirement],
overrides: &[Requirement],
indexes: Option<&IndexLocations>,
Expand All @@ -679,6 +700,29 @@ impl Lock {
}
}

// Validate that the lockfile was generated with the same requirements.
{
let expected: BTreeSet<_> = requirements
.iter()
.cloned()
.map(|requirement| normalize_requirement(requirement, workspace))
.collect::<Result<_, _>>()?;
let actual: BTreeSet<_> = self
.manifest
.requirements
.iter()
.cloned()
.map(|requirement| normalize_requirement(requirement, workspace))
.collect::<Result<_, _>>()?;
if expected != actual {
debug!(
"Mismatched requirements:\n expected: {:?}\n found: {:?}",
expected, actual
);
return Ok(SatisfiesResult::MismatchedConstraints(expected, actual));
}
}

// Validate that the lockfile was generated with the same constraints.
{
let expected: BTreeSet<_> = constraints
Expand Down Expand Up @@ -901,6 +945,8 @@ pub enum SatisfiesResult<'lock> {
Satisfied,
/// The lockfile uses a different set of workspace members.
MismatchedMembers(BTreeSet<PackageName>, &'lock BTreeSet<PackageName>),
/// The lockfile uses a different set of requirements.
MismatchedRequirements(BTreeSet<Requirement>, BTreeSet<Requirement>),
/// The lockfile uses a different set of constraints.
MismatchedConstraints(BTreeSet<Requirement>, BTreeSet<Requirement>),
/// The lockfile uses a different set of overrides.
Expand Down Expand Up @@ -947,6 +993,9 @@ pub struct ResolverManifest {
/// The workspace members included in the lockfile.
#[serde(default)]
members: BTreeSet<PackageName>,
/// The requirements provided to the resolver, exclusive of the workspace members.
#[serde(default)]
requirements: BTreeSet<Requirement>,
/// The constraints provided to the resolver.
#[serde(default)]
constraints: BTreeSet<Requirement>,
Expand All @@ -958,11 +1007,13 @@ pub struct ResolverManifest {
impl ResolverManifest {
pub fn new(
members: impl IntoIterator<Item = PackageName>,
requirements: impl IntoIterator<Item = Requirement>,
constraints: impl IntoIterator<Item = Requirement>,
overrides: impl IntoIterator<Item = Requirement>,
) -> Self {
Self {
members: members.into_iter().collect(),
requirements: requirements.into_iter().collect(),
constraints: constraints.into_iter().collect(),
overrides: overrides.into_iter().collect(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Ok(
},
manifest: ResolverManifest {
members: {},
requirements: {},
constraints: {},
overrides: {},
},
Expand Down
30 changes: 21 additions & 9 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,8 @@ async fn do_lock(
sources,
} = settings;

// When locking, include the project itself (as editable).
let requirements = workspace
.members_requirements()
.chain(workspace.root_requirements())
.collect::<Vec<_>>();
// Collect the requirements, etc.
let requirements = workspace.root_requirements().collect::<Vec<_>>();
let overrides = workspace.overrides().into_iter().collect::<Vec<_>>();
let constraints = workspace.constraints();
let dev = vec![DEV_DEPENDENCIES.clone()];
Expand Down Expand Up @@ -413,6 +410,7 @@ async fn do_lock(
existing_lock,
workspace,
&members,
&requirements,
&constraints,
&overrides,
environments,
Expand Down Expand Up @@ -486,9 +484,9 @@ async fn do_lock(

// Resolve the requirements.
let resolution = pip::operations::resolve(
requirements
.iter()
.cloned()
workspace
.members_requirements()
.chain(requirements.iter().cloned())
.map(UnresolvedRequirementSpecification::from)
.collect(),
constraints.clone(),
Expand Down Expand Up @@ -529,7 +527,12 @@ async fn do_lock(

let previous = existing_lock.map(ValidatedLock::into_lock);
let lock = Lock::from_resolution_graph(&resolution)?
.with_manifest(ResolverManifest::new(members, constraints, overrides))
.with_manifest(ResolverManifest::new(
members,
requirements,
constraints,
overrides,
))
.with_supported_environments(
environments
.cloned()
Expand Down Expand Up @@ -559,6 +562,7 @@ impl ValidatedLock {
lock: Lock,
workspace: &Workspace,
members: &[PackageName],
requirements: &[Requirement],
constraints: &[Requirement],
overrides: &[Requirement],
environments: Option<&SupportedEnvironments>,
Expand Down Expand Up @@ -679,6 +683,7 @@ impl ValidatedLock {
.satisfies(
workspace,
members,
requirements,
constraints,
overrides,
indexes,
Expand All @@ -698,6 +703,13 @@ impl ValidatedLock {
);
Ok(Self::Preferable(lock))
}
SatisfiesResult::MismatchedRequirements(expected, actual) => {
debug!(
"Ignoring existing lockfile due to mismatched requirements:\n Expected: {:?}\n Actual: {:?}",
expected, actual
);
Ok(Self::Preferable(lock))
}
SatisfiesResult::MismatchedConstraints(expected, actual) => {
debug!(
"Ignoring existing lockfile due to mismatched constraints:\n Expected: {:?}\n Actual: {:?}",
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,9 @@ fn add_virtual() -> Result<()> {
[options]
exclude-newer = "2024-03-25T00:00:00Z"

[manifest]
requirements = [{ name = "iniconfig" }]

[[package]]
name = "iniconfig"
version = "2.0.0"
Expand Down
Loading
Loading