Skip to content

Commit

Permalink
fix(spec): Ensure PackageIdSpec respects version 'build' field
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 17, 2023
1 parent ae068fb commit 0ec3274
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
13 changes: 6 additions & 7 deletions src/cargo/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ impl PackageIdSpec {
}

if let Some(ref v) = self.version {
let req = v.exact_req();
if !req.matches(package_id.version()) {
if !v.matches(package_id.version()) {
return false;
}
}
Expand Down Expand Up @@ -465,15 +464,15 @@ mod tests {
assert!(PackageIdSpec::parse("meta@1.2.3+hello")
.unwrap()
.matches(meta));
assert!(PackageIdSpec::parse("meta@1.2.3+bye")
assert!(!PackageIdSpec::parse("meta@1.2.3+bye")
.unwrap()
.matches(meta));

let pre = PackageId::new("pre", "1.2.3-alpha.0", sid).unwrap();
assert!(PackageIdSpec::parse("pre").unwrap().matches(pre));
assert!(!PackageIdSpec::parse("pre@1").unwrap().matches(pre));
assert!(!PackageIdSpec::parse("pre@1.2").unwrap().matches(pre));
assert!(!PackageIdSpec::parse("pre@1.2.3").unwrap().matches(pre));
assert!(PackageIdSpec::parse("pre@1").unwrap().matches(pre));
assert!(PackageIdSpec::parse("pre@1.2").unwrap().matches(pre));
assert!(PackageIdSpec::parse("pre@1.2.3").unwrap().matches(pre));
assert!(PackageIdSpec::parse("pre@1.2.3-alpha.0")
.unwrap()
.matches(pre));
Expand All @@ -486,7 +485,7 @@ mod tests {
assert!(!PackageIdSpec::parse("pre@1.2.3+hello")
.unwrap()
.matches(pre));
assert!(PackageIdSpec::parse("pre@1.2.3-alpha.0+hello")
assert!(!PackageIdSpec::parse("pre@1.2.3-alpha.0+hello")
.unwrap()
.matches(pre));
}
Expand Down
24 changes: 14 additions & 10 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,20 @@ impl PartialVersion {
}
}

pub fn exact_req(&self) -> VersionReq {
VersionReq {
comparators: vec![Comparator {
op: semver::Op::Exact,
major: self.major,
minor: self.minor,
patch: self.patch,
pre: self.pre.as_ref().cloned().unwrap_or_default(),
}],
}
/// Check if this matches a version, including build metadata
///
/// Build metadata does not affect version precedence but may be necessary for uniquely
/// identifying a package.
pub fn matches(&self, version: &Version) -> bool {
self.major == version.major
&& self.minor.map(|f| f == version.minor).unwrap_or(true)
&& self.patch.map(|f| f == version.patch).unwrap_or(true)
&& self.pre.as_ref().map(|f| f == &version.pre).unwrap_or(true)
&& self
.build
.as_ref()
.map(|f| f == &version.build)
.unwrap_or(true)
}
}

Expand Down
20 changes: 15 additions & 5 deletions tests/testsuite/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
}

#[cargo_test]
fn override_different_metadata_2() {
fn override_respects_spec_metadata() {
Package::new("bar", "0.1.0+a").publish();

let bar = git::repo(&paths::root().join("override"))
Expand Down Expand Up @@ -1387,12 +1387,22 @@ fn override_different_metadata_2() {
.with_stderr(
"\
[UPDATING] `dummy-registry` index
[UPDATING] git repository `[..]`
[CHECKING] bar v0.1.0+a (file://[..])
[CHECKING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[WARNING] package replacement is not used: https://github.com/rust-lang/crates.io-index#bar@0.1.0+notTheBuild
[DOWNLOADING] crates ...
[DOWNLOADED] bar v0.1.0+a (registry `dummy-registry`)
[CHECKING] bar v0.1.0+a
[CHECKING] foo v0.0.1 ([..]/foo)
[..]
[..]
[..]
[..]
[..]
[..]
[..]
error: could not compile `foo` (lib) due to previous error
",
)
.with_status(101)
.run();
}

Expand Down

0 comments on commit 0ec3274

Please sign in to comment.