Skip to content

Commit 7502318

Browse files
committed
fix(replace): Error, rather than assert, on version mismatch
1 parent d318ed4 commit 7502318

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/cargo/core/resolver/dep_cache.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,20 @@ impl<'a> RegistryQueryer<'a> {
173173
)));
174174
}
175175

176-
// The dependency should be hard-coded to have the same name and an
177-
// exact version requirement, so both of these assertions should
178-
// never fail.
179-
assert_eq!(s.version(), summary.version());
180-
assert_eq!(s.name(), summary.name());
176+
assert_eq!(
177+
s.name(),
178+
summary.name(),
179+
"dependency should be hard coded to have the same name"
180+
);
181+
if s.version() != summary.version() {
182+
return Poll::Ready(Err(anyhow::anyhow!(
183+
"replacement specification `{}` matched {} and tried to override it with {}\n\
184+
avoid matching unrelated packages by being more specific",
185+
spec,
186+
summary.version(),
187+
s.version(),
188+
)));
189+
}
181190

182191
let replace = if s.source_id() == summary.source_id() {
183192
debug!("Preventing\n{:?}\nfrom replacing\n{:?}", summary, s);

tests/testsuite/replace.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ fn override_plus_dep() {
13001300
}
13011301

13021302
#[cargo_test]
1303-
fn override_different_metadata() {
1303+
fn override_generic_matching_other_versions() {
13041304
Package::new("bar", "0.1.0+a").publish();
13051305

13061306
let bar = git::repo(&paths::root().join("override"))
@@ -1338,11 +1338,11 @@ fn override_different_metadata() {
13381338
"\
13391339
[UPDATING] `dummy-registry` index
13401340
[UPDATING] git repository `[..]`
1341-
thread 'main' panicked at src/cargo/core/resolver/dep_cache.rs:179:13:
1342-
assertion `left == right` failed
1343-
left: Version { major: 0, minor: 1, patch: 0 }
1344-
right: Version { major: 0, minor: 1, patch: 0, build: BuildMetadata(\"a\") }
1345-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1341+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 ([..]/foo)`
1342+
1343+
Caused by:
1344+
replacement specification `https://github.com/rust-lang/crates.io-index#[email protected]` matched 0.1.0+a and tried to override it with 0.1.0
1345+
avoid matching unrelated packages by being more specific
13461346
",
13471347
)
13481348
.with_status(101)

0 commit comments

Comments
 (0)