From c5dfafdde2523c0bf2c0e63071e48824a9a34952 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 10 Oct 2023 16:39:46 -0500 Subject: [PATCH] fix(replace): Ensure PackageIdSpec respects version 'build' field --- src/cargo/core/package_id_spec.rs | 3 +-- src/cargo/util/semver_ext.rs | 20 ++++++++++---------- tests/testsuite/replace.rs | 20 +++++++++++++++----- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/cargo/core/package_id_spec.rs b/src/cargo/core/package_id_spec.rs index 37a6f5e7b3cd..efa0dee03c19 100644 --- a/src/cargo/core/package_id_spec.rs +++ b/src/cargo/core/package_id_spec.rs @@ -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; } } diff --git a/src/cargo/util/semver_ext.rs b/src/cargo/util/semver_ext.rs index 0be5bca68f21..fd86a75b632c 100644 --- a/src/cargo/util/semver_ext.rs +++ b/src/cargo/util/semver_ext.rs @@ -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) } } diff --git a/tests/testsuite/replace.rs b/tests/testsuite/replace.rs index 15df0f9feeb6..ee2314fd7972 100644 --- a/tests/testsuite/replace.rs +++ b/tests/testsuite/replace.rs @@ -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")) @@ -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(); }