Skip to content

Commit

Permalink
fix(replace): Ensure PackageIdSpec respects version 'build' field
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 10, 2023
1 parent dce2e6f commit c5dfafd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
3 changes: 1 addition & 2 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
20 changes: 10 additions & 10 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,16 @@ 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(),
}],
}
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#[email protected]+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 c5dfafd

Please sign in to comment.