Skip to content

Commit 4f2bae9

Browse files
committed
Revert change to automatically select the greatest patch match.
1 parent 5dde9cc commit 4f2bae9

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

src/cargo/core/registry.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'cfg> PackageRegistry<'cfg> {
296296
let (summary, should_unlock) =
297297
summary_for_patch(orig_patch, &locked, summaries, source).chain_err(|| {
298298
format!(
299-
"patch for `{}` in `{}` did not resolve to any crates",
299+
"patch for `{}` in `{}` failed to resolve",
300300
orig_patch.package_name(),
301301
url,
302302
)
@@ -754,21 +754,28 @@ fn summary_for_patch(
754754
if summaries.len() == 1 {
755755
return Ok((summaries.pop().unwrap(), None));
756756
}
757-
let best_summary = |summaries: &mut Vec<Summary>| -> Summary {
758-
// TODO: This could maybe honor -Zminimal-versions?
759-
summaries.sort_by(|a, b| a.version().cmp(b.version()));
760-
summaries.pop().unwrap()
761-
};
762757
if summaries.len() > 1 {
763-
let summary = best_summary(&mut summaries);
764-
if let Some((_dep, lock_id)) = locked {
765-
// I can't think of a scenario where this might happen (locked by
766-
// definition should only match at most one summary). Maybe if the
767-
// source is broken?
768-
return Ok((summary, Some(*lock_id)));
769-
} else {
770-
return Ok((summary, None));
771-
}
758+
// TODO: In the future, it might be nice to add all of these
759+
// candidates so that version selection would just pick the
760+
// appropriate one. However, as this is currently structured, if we
761+
// added these all as patches, the unselected versions would end up in
762+
// the "unused patch" listing, and trigger a warning. It might take a
763+
// fair bit of restructuring to make that work cleanly, and there
764+
// isn't any demand at this time to support that.
765+
let mut vers: Vec<_> = summaries.iter().map(|summary| summary.version()).collect();
766+
vers.sort();
767+
let versions: Vec<_> = vers.into_iter().map(|v| v.to_string()).collect();
768+
anyhow::bail!(
769+
"patch for `{}` in `{}` resolved to more than one candidate\n\
770+
Found versions: {}\n\
771+
Update the patch definition to select only one package.\n\
772+
For example, add an `=` version requirement to the patch definition, \
773+
such as `version = \"={}\"`.",
774+
orig_patch.package_name(),
775+
orig_patch.source_id(),
776+
versions.join(", "),
777+
versions.last().unwrap()
778+
);
772779
}
773780
assert!(summaries.is_empty());
774781
// No summaries found, try to help the user figure out what is wrong.

tests/testsuite/patch.rs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,15 +1567,20 @@ fn too_many_matches() {
15671567

15681568
// Picks 0.1.1, the most recent version.
15691569
p.cargo("check")
1570+
.with_status(101)
15701571
.with_stderr(
15711572
"\
15721573
[UPDATING] `[..]/alternative-registry` index
1573-
[UPDATING] `[..]/registry` index
1574-
[DOWNLOADING] crates ...
1575-
[DOWNLOADED] bar v0.1.1 (registry `[..]/alternative-registry`)
1576-
[CHECKING] bar v0.1.1 (registry `[..]/alternative-registry`)
1577-
[CHECKING] foo v0.1.0 ([..]/foo)
1578-
[FINISHED] [..]
1574+
[ERROR] failed to resolve patches for `https://github.com/rust-lang/crates.io-index`
1575+
1576+
Caused by:
1577+
patch for `bar` in `https://github.com/rust-lang/crates.io-index` failed to resolve
1578+
1579+
Caused by:
1580+
patch for `bar` in `registry `[..]/alternative-registry`` resolved to more than one candidate
1581+
Found versions: 0.1.0, 0.1.1
1582+
Update the patch definition to select only one package.
1583+
For example, add an `=` version requirement to the patch definition, such as `version = \"=0.1.1\"`.
15791584
",
15801585
)
15811586
.run();
@@ -1611,7 +1616,7 @@ fn no_matches() {
16111616
error: failed to resolve patches for `https://github.com/rust-lang/crates.io-index`
16121617
16131618
Caused by:
1614-
patch for `bar` in `https://github.com/rust-lang/crates.io-index` did not resolve to any crates
1619+
patch for `bar` in `https://github.com/rust-lang/crates.io-index` failed to resolve
16151620
16161621
Caused by:
16171622
The patch location `[..]/foo/bar` does not appear to contain any packages matching the name `bar`.
@@ -1650,7 +1655,7 @@ fn mismatched_version() {
16501655
[ERROR] failed to resolve patches for `https://github.com/rust-lang/crates.io-index`
16511656
16521657
Caused by:
1653-
patch for `bar` in `https://github.com/rust-lang/crates.io-index` did not resolve to any crates
1658+
patch for `bar` in `https://github.com/rust-lang/crates.io-index` failed to resolve
16541659
16551660
Caused by:
16561661
The patch location `[..]/foo/bar` contains a `bar` package with version `0.1.0`, \
@@ -1760,7 +1765,7 @@ fn patch_walks_backwards_restricted() {
17601765
error: failed to resolve patches for `https://github.com/rust-lang/crates.io-index`
17611766
17621767
Caused by:
1763-
patch for `bar` in `https://github.com/rust-lang/crates.io-index` did not resolve to any crates
1768+
patch for `bar` in `https://github.com/rust-lang/crates.io-index` failed to resolve
17641769
17651770
Caused by:
17661771
The patch location `[..]/foo/bar` contains a `bar` package with version `0.1.0`, but the patch definition requires `^0.1.1`.
@@ -1942,7 +1947,7 @@ fn can_update_with_alt_reg() {
19421947
bar = "0.1"
19431948
19441949
[patch.crates-io]
1945-
bar = { version = "0.1.1", registry = "alternative" }
1950+
bar = { version = "=0.1.1", registry = "alternative" }
19461951
"#,
19471952
)
19481953
.file("src/lib.rs", "")
@@ -1967,12 +1972,42 @@ fn can_update_with_alt_reg() {
19671972
// Should remain locked.
19681973
p.cargo("check").with_stderr("[FINISHED] [..]").run();
19691974

1975+
// This does nothing, due to `=` requirement.
19701976
p.cargo("update -p bar")
19711977
.with_stderr(
19721978
"\
19731979
[UPDATING] `[..]/alternative-registry` index
19741980
[UPDATING] `[..]/registry` index
1975-
[UPDATING] bar v0.1.1 (registry `[..]/alternative-registry`) -> v0.1.2
1981+
",
1982+
)
1983+
.run();
1984+
1985+
// Bump to 0.1.2.
1986+
p.change_file(
1987+
"Cargo.toml",
1988+
r#"
1989+
[package]
1990+
name = "foo"
1991+
version = "0.1.0"
1992+
1993+
[dependencies]
1994+
bar = "0.1"
1995+
1996+
[patch.crates-io]
1997+
bar = { version = "=0.1.2", registry = "alternative" }
1998+
"#,
1999+
);
2000+
2001+
p.cargo("check")
2002+
.with_stderr(
2003+
"\
2004+
[UPDATING] `[..]/alternative-registry` index
2005+
[UPDATING] `[..]/registry` index
2006+
[DOWNLOADING] crates ...
2007+
[DOWNLOADED] bar v0.1.2 (registry `[..]/alternative-registry`)
2008+
[CHECKING] bar v0.1.2 (registry `[..]/alternative-registry`)
2009+
[CHECKING] foo v0.1.0 ([..]/foo)
2010+
[FINISHED] [..]
19762011
",
19772012
)
19782013
.run();

0 commit comments

Comments
 (0)