@@ -133,7 +133,7 @@ impl<DP: DependencyProvider> State<DP> {
133
133
& mut self ,
134
134
package : Id < DP :: P > ,
135
135
) -> Result < SmallVec < ( Id < DP :: P > , IncompDpId < DP > ) > , NoSolutionError < DP > > {
136
- let mut root_causes = SmallVec :: default ( ) ;
136
+ let mut satisfier_causes = SmallVec :: default ( ) ;
137
137
self . unit_propagation_buffer . clear ( ) ;
138
138
self . unit_propagation_buffer . push ( package) ;
139
139
while let Some ( current_package) = self . unit_propagation_buffer . pop ( ) {
@@ -186,12 +186,11 @@ impl<DP: DependencyProvider> State<DP> {
186
186
}
187
187
}
188
188
if let Some ( incompat_id) = conflict_id {
189
- let ( package_almost, root_cause) =
190
- self . conflict_resolution ( incompat_id)
191
- . map_err ( |terminal_incompat_id| {
192
- self . build_derivation_tree ( terminal_incompat_id)
193
- } ) ?;
194
- root_causes. push ( ( package, root_cause) ) ;
189
+ let ( package_almost, root_cause) = self
190
+ . conflict_resolution ( incompat_id, & mut satisfier_causes)
191
+ . map_err ( |terminal_incompat_id| {
192
+ self . build_derivation_tree ( terminal_incompat_id)
193
+ } ) ?;
195
194
self . unit_propagation_buffer . clear ( ) ;
196
195
self . unit_propagation_buffer . push ( package_almost) ;
197
196
// Add to the partial solution with incompat as cause.
@@ -207,16 +206,46 @@ impl<DP: DependencyProvider> State<DP> {
207
206
}
208
207
}
209
208
// If there are no more changed packages, unit propagation is done.
210
- Ok ( root_causes )
209
+ Ok ( satisfier_causes )
211
210
}
212
211
213
212
/// Return the root cause or the terminal incompatibility.
214
213
/// CF <https://github.com/dart-lang/pub/blob/master/doc/solver.md#unit-propagation>
214
+ ///
215
+ /// Usually by the time we have a conflict `unit_propagation` has done a lot of work.
216
+ /// So the actual conflict we find is important, but not particularly actionable.
217
+ /// It says something like "the dependency on package X and the dependency on package Y are incompatible".
218
+ /// To make it actionable we want to track it back to decisions that made the dependency required.
219
+ /// "The decision on B is incompatible with the decision on C,
220
+ /// because unit propagation from just those decisions will lead to the conflict about X and Y"
221
+ /// is much more actionable, backtrack until one of those decisions can be revisited.
222
+ /// To make a practical, we really only need one of the terms to be a decision.
223
+ /// We may as well leave the other terms general. Something like
224
+ /// "the dependency on the package X is incompatible with the decision on C" tends to work out pretty well.
225
+ /// Then if A turns out to also have a dependency on X the resulting root cause is still useful.
226
+ /// Of course, this is more heuristics than science. If the output is too general, then `unit_propagation` will
227
+ /// handle the confusion by calling us again with the next most specific conflict it comes across.
228
+ /// If the output is to specific, then the outer `solver` loop will eventually end up calling us again
229
+ /// until all possibilities are enumerated.
230
+ ///
231
+ /// This function combines incompatibilities with things that make the problem inevitable to end up with a
232
+ /// more useful incompatibility. For the correctness of the PubGrub algorithm only the final output is required.
233
+ /// By banning the final output, unit propagation will prevent the intermediate steps from occurring again,
234
+ /// at least prevent the exact same way. However, the statistics collected for `prioritize`may want
235
+ /// to analyze those intermediate steps. For example we might start with "there is no version 1 of Z",
236
+ /// and `conflict_resolution` may be able to determine that "that was inevitable when we picked version 1 of X"
237
+ /// which was inevitable when picked W and ... and version 1 of B, which was depended on by version 1 of A.
238
+ /// Therefore the root cause may simplify all the way down to "we cannot pick version 1 of A".
239
+ /// This will prevent us going down this path again. However when we start looking at version 2 of A,
240
+ /// and discover that it depends on version 2 of B, we will want to prioritize the chain of intermediate steps
241
+ /// to confirm if it has a problem with the same shape.
242
+ /// The `satisfier_causes` argument keeps track of these intermediate steps so that the caller can use.
215
243
#[ allow( clippy:: type_complexity) ]
216
244
#[ cold]
217
245
fn conflict_resolution (
218
246
& mut self ,
219
247
incompatibility : IncompDpId < DP > ,
248
+ satisfier_causes : & mut SmallVec < ( Id < DP :: P > , IncompDpId < DP > ) > ,
220
249
) -> Result < ( Id < DP :: P > , IncompDpId < DP > ) , IncompDpId < DP > > {
221
250
let mut current_incompat_id = incompatibility;
222
251
let mut current_incompat_changed = false ;
@@ -240,6 +269,7 @@ impl<DP: DependencyProvider> State<DP> {
240
269
previous_satisfier_level,
241
270
) ;
242
271
log:: info!( "backtrack to {:?}" , previous_satisfier_level) ;
272
+ satisfier_causes. push ( ( package, current_incompat_id) ) ;
243
273
return Ok ( ( package, current_incompat_id) ) ;
244
274
}
245
275
SatisfierSearch :: SameDecisionLevels { satisfier_cause } => {
@@ -251,6 +281,7 @@ impl<DP: DependencyProvider> State<DP> {
251
281
) ;
252
282
log:: info!( "prior cause: {}" , prior_cause. display( & self . package_store) ) ;
253
283
current_incompat_id = self . incompatibility_store . alloc ( prior_cause) ;
284
+ satisfier_causes. push ( ( package, current_incompat_id) ) ;
254
285
current_incompat_changed = true ;
255
286
}
256
287
}
0 commit comments