Skip to content

Commit c61b0f0

Browse files
committed
Auto merge of #11770 - Eh2406:non-active-conflict, r=weihanglo
patch can conflict on not activated packages ### What does this PR try to resolve? In the resolver there is a data structure called a `conflicting_activations`, which records all the reasons a "better" version was not picked for the package being resolved. Normally, these are packages that are currently active that for one reason or another block one of the other versions. Several optimizations assumed that this was always true, even going so far as to use `.expect`. This was a mistake because when there's a `patch` involved there can be conflicts on a version that is not selected. So the correct behavior is to fall back to skip the optimizations and try all versions when a `conflicting_activations` are not active. ### How should we test and review this PR? This adds two automated tests based on the reproductions in #7463 and #11336. So the test suite now covers this case. It can also be tested by reconstructing the repositories originally reported in those issues. ### Additional information It could be that in the long term the correct fix is to figure out how to support patch without having a conflicting activation that is not activated. But that would be a much bigger change. And for now this assumption is only used in optimizations, so this gets people unstuck.
2 parents 0942fc7 + 2c712d5 commit c61b0f0

File tree

2 files changed

+125
-17
lines changed

2 files changed

+125
-17
lines changed

src/cargo/core/resolver/mod.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -843,10 +843,8 @@ impl RemainingCandidates {
843843
}
844844
}
845845

846-
/// Attempts to find a new conflict that allows a `find_candidate` feather then the input one.
846+
/// Attempts to find a new conflict that allows a `find_candidate` better then the input one.
847847
/// It will add the new conflict to the cache if one is found.
848-
///
849-
/// Panics if the input conflict is not all active in `cx`.
850848
fn generalize_conflicting(
851849
cx: &Context,
852850
registry: &mut RegistryQueryer<'_>,
@@ -855,15 +853,12 @@ fn generalize_conflicting(
855853
dep: &Dependency,
856854
conflicting_activations: &ConflictMap,
857855
) -> Option<ConflictMap> {
858-
if conflicting_activations.is_empty() {
859-
return None;
860-
}
861856
// We need to determine the `ContextAge` that this `conflicting_activations` will jump to, and why.
862-
let (backtrack_critical_age, backtrack_critical_id) = conflicting_activations
863-
.keys()
864-
.map(|&c| (cx.is_active(c).expect("not currently active!?"), c))
865-
.max()
866-
.unwrap();
857+
let (backtrack_critical_age, backtrack_critical_id) = shortcircuit_max(
858+
conflicting_activations
859+
.keys()
860+
.map(|&c| cx.is_active(c).map(|a| (a, c))),
861+
)?;
867862
let backtrack_critical_reason: ConflictReason =
868863
conflicting_activations[&backtrack_critical_id].clone();
869864

@@ -958,6 +953,19 @@ fn generalize_conflicting(
958953
None
959954
}
960955

956+
/// Returns Some of the largest item in the iterator.
957+
/// Returns None if any of the items are None or the iterator is empty.
958+
fn shortcircuit_max<I: Ord>(iter: impl Iterator<Item = Option<I>>) -> Option<I> {
959+
let mut out = None;
960+
for i in iter {
961+
if i.is_none() {
962+
return None;
963+
}
964+
out = std::cmp::max(out, i);
965+
}
966+
out
967+
}
968+
961969
/// Looks through the states in `backtrack_stack` for dependencies with
962970
/// remaining candidates. For each one, also checks if rolling back
963971
/// could change the outcome of the failed resolution that caused backtracking
@@ -984,12 +992,10 @@ fn find_candidate(
984992
// the cause of that backtrack, so we do not update it.
985993
let age = if !backtracked {
986994
// we don't have abnormal situations. So we can ask `cx` for how far back we need to go.
987-
let a = cx.is_conflicting(Some(parent.package_id()), conflicting_activations);
988-
// If the `conflicting_activations` does not apply to `cx`, then something went very wrong
989-
// in building it. But we will just fall back to laboriously trying all possibilities witch
990-
// will give us the correct answer so only `assert` if there is a developer to debug it.
991-
debug_assert!(a.is_some());
992-
a
995+
// If the `conflicting_activations` does not apply to `cx`,
996+
// we will just fall back to laboriously trying all possibilities witch
997+
// will give us the correct answer.
998+
cx.is_conflicting(Some(parent.package_id()), conflicting_activations)
993999
} else {
9941000
None
9951001
};

tests/testsuite/patch.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,3 +2541,105 @@ foo v0.1.0 [..]
25412541
))
25422542
.run();
25432543
}
2544+
2545+
// From https://github.com/rust-lang/cargo/issues/7463
2546+
#[cargo_test]
2547+
fn patch_eq_conflict_panic() {
2548+
Package::new("bar", "0.1.0").publish();
2549+
Package::new("bar", "0.1.1").publish();
2550+
let p = project()
2551+
.file(
2552+
"Cargo.toml",
2553+
r#"
2554+
[package]
2555+
name = "foo"
2556+
version = "0.1.0"
2557+
2558+
[dependencies]
2559+
bar = "=0.1.0"
2560+
2561+
[dev-dependencies]
2562+
bar = "=0.1.1"
2563+
2564+
[patch.crates-io]
2565+
bar = {path="bar"}
2566+
"#,
2567+
)
2568+
.file("src/lib.rs", "")
2569+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.1"))
2570+
.file("bar/src/lib.rs", "")
2571+
.build();
2572+
2573+
p.cargo("generate-lockfile")
2574+
.with_status(101)
2575+
.with_stderr(
2576+
r#"[UPDATING] `dummy-registry` index
2577+
[ERROR] failed to select a version for `bar`.
2578+
... required by package `foo v0.1.0 ([..])`
2579+
versions that meet the requirements `=0.1.1` are: 0.1.1
2580+
2581+
all possible versions conflict with previously selected packages.
2582+
2583+
previously selected package `bar v0.1.0`
2584+
... which satisfies dependency `bar = "=0.1.0"` of package `foo v0.1.0 ([..])`
2585+
2586+
failed to select a version for `bar` which could resolve this conflict
2587+
"#,
2588+
)
2589+
.run();
2590+
}
2591+
2592+
// From https://github.com/rust-lang/cargo/issues/11336
2593+
#[cargo_test]
2594+
fn mismatched_version2() {
2595+
Package::new("qux", "0.1.0-beta.1").publish();
2596+
Package::new("qux", "0.1.0-beta.2").publish();
2597+
Package::new("bar", "0.1.0")
2598+
.dep("qux", "=0.1.0-beta.1")
2599+
.publish();
2600+
let p = project()
2601+
.file(
2602+
"Cargo.toml",
2603+
r#"
2604+
[package]
2605+
name = "foo"
2606+
version = "0.1.0"
2607+
2608+
[dependencies]
2609+
bar = "0.1.0"
2610+
qux = "0.1.0-beta.2"
2611+
2612+
[patch.crates-io]
2613+
qux = { path = "qux" }
2614+
"#,
2615+
)
2616+
.file("src/lib.rs", "")
2617+
.file(
2618+
"qux/Cargo.toml",
2619+
r#"
2620+
[package]
2621+
name = "qux"
2622+
version = "0.1.0-beta.1"
2623+
"#,
2624+
)
2625+
.file("qux/src/lib.rs", "")
2626+
.build();
2627+
2628+
p.cargo("generate-lockfile")
2629+
.with_status(101)
2630+
.with_stderr(
2631+
r#"[UPDATING] `dummy-registry` index
2632+
[ERROR] failed to select a version for `qux`.
2633+
... required by package `bar v0.1.0`
2634+
... which satisfies dependency `bar = "^0.1.0"` of package `foo v0.1.0 ([..])`
2635+
versions that meet the requirements `=0.1.0-beta.1` are: 0.1.0-beta.1
2636+
2637+
all possible versions conflict with previously selected packages.
2638+
2639+
previously selected package `qux v0.1.0-beta.2`
2640+
... which satisfies dependency `qux = "^0.1.0-beta.2"` of package `foo v0.1.0 ([..])`
2641+
2642+
failed to select a version for `qux` which could resolve this conflict"#,
2643+
)
2644+
.run();
2645+
}

0 commit comments

Comments
 (0)