Skip to content

Commit

Permalink
Invalidate lockfile when member versions change
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 5, 2024
1 parent 5b89734 commit bf83f08
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
30 changes: 28 additions & 2 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,9 @@ impl Lock {
}
}

// Validate that the member sources have not changed (e.g., switched from packaged to
// virtual).
// Validate that the member sources have not changed.
{
// E.g., that they've switched from virtual to non-virtual or vice versa.
for (name, member) in workspace.packages() {
let expected = !member.pyproject_toml().is_package();
let actual = self
Expand All @@ -905,6 +905,30 @@ impl Lock {
return Ok(SatisfiesResult::MismatchedSources(name.clone(), expected));
}
}

// E.g., that the version has changed.
for (name, member) in workspace.packages() {
let Some(expected) = member
.pyproject_toml()
.project
.as_ref()
.map(|project| &project.version)
else {
continue;
};
let actual = self
.find_by_name(name)
.ok()
.flatten()
.map(|package| &package.id.version);
if actual.map_or(true, |actual| actual != expected) {
return Ok(SatisfiesResult::MismatchedVersion(
name.clone(),
expected.clone(),
actual.cloned(),
));
}
}
}

// Validate that the lockfile was generated with the same requirements.
Expand Down Expand Up @@ -1188,6 +1212,8 @@ pub enum SatisfiesResult<'lock> {
MismatchedMembers(BTreeSet<PackageName>, &'lock BTreeSet<PackageName>),
/// The lockfile uses a different set of sources for its workspace members.
MismatchedSources(PackageName, bool),
/// The lockfile uses a different set of version for its workspace members.
MismatchedVersion(PackageName, Version, Option<Version>),
/// The lockfile uses a different set of requirements.
MismatchedRequirements(BTreeSet<Requirement>, BTreeSet<Requirement>),
/// The lockfile uses a different set of constraints.
Expand Down
4 changes: 3 additions & 1 deletion crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;
use url::Url;

use pep440_rs::VersionSpecifiers;
use pep440_rs::{Version, VersionSpecifiers};
use pypi_types::{RequirementSource, SupportedEnvironments, VerbatimParsedUrl};
use uv_fs::relative_to;
use uv_git::GitReference;
Expand Down Expand Up @@ -87,6 +87,8 @@ impl AsRef<[u8]> for PyProjectToml {
pub struct Project {
/// The name of the project
pub name: PackageName,
/// The version of the project
pub version: Version,
/// The Python versions this project is compatible with.
pub requires_python: Option<VersionSpecifiers>,
/// The optional dependencies of the project.
Expand Down
12 changes: 12 additions & 0 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,18 @@ impl ValidatedLock {
}
Ok(Self::Preferable(lock))
}
SatisfiesResult::MismatchedVersion(name, expected, actual) => {
if let Some(actual) = actual {
debug!(
"Ignoring existing lockfile due to mismatched version: `{name}` (expected: `{expected}`, found: `{actual}`)"
);
} else {
debug!(
"Ignoring existing lockfile due to mismatched version: `{name}` (expected: `{expected}`)"
);
}
Ok(Self::Preferable(lock))
}
SatisfiesResult::MismatchedRequirements(expected, actual) => {
debug!(
"Ignoring existing lockfile due to mismatched requirements:\n Expected: {:?}\n Actual: {:?}",
Expand Down
66 changes: 66 additions & 0 deletions crates/uv/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,72 @@ fn sync_virtual_env_warning() -> Result<()> {
Ok(())
}

#[test]
fn sync_update_project() -> Result<()> {
let context = TestContext::new_with_versions(&["3.12"]);

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["iniconfig"]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#,
)?;

uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
Creating virtualenv at: .venv
Resolved 2 packages in [TIME]
Prepared 2 packages in [TIME]
Installed 2 packages in [TIME]
+ iniconfig==2.0.0
+ my-project==0.1.0 (from file://[TEMP_DIR]/)
"###);

// Bump the project version.
pyproject_toml.write_str(
r#"
[project]
name = "my-project"
version = "0.2.0"
requires-python = ">=3.12"
dependencies = ["iniconfig"]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#,
)?;

uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 2 packages in [TIME]
Prepared 1 package in [TIME]
Uninstalled 1 package in [TIME]
Installed 1 package in [TIME]
- my-project==0.1.0 (from file://[TEMP_DIR]/)
+ my-project==0.2.0 (from file://[TEMP_DIR]/)
"###);

Ok(())
}

#[test]
fn sync_environment_prompt() -> Result<()> {
let context = TestContext::new_with_versions(&["3.12"]);
Expand Down

0 comments on commit bf83f08

Please sign in to comment.