-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPubGrubDependencyResolver.swift
845 lines (734 loc) · 36.9 KB
/
PubGrubDependencyResolver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2019-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import _Concurrency
import Dispatch
import class Foundation.NSLock
import OrderedCollections
import PackageModel
import struct TSCUtility.Version
/// The solver that is able to transitively resolve a set of package constraints
/// specified by a root package.
public struct PubGrubDependencyResolver {
/// The type of the constraints the resolver operates on.
public typealias Constraint = PackageContainerConstraint
/// the mutable state that get computed
internal final class State {
/// The root package reference.
let root: DependencyResolutionNode
/// The list of packages that are overridden in the graph. A local package reference will
/// always override any other kind of package reference and branch-based reference will override
/// version-based reference.
let overriddenPackages: [PackageReference: (version: BoundVersion, products: ProductFilter)]
/// A collection of all known incompatibilities matched to the packages they
/// refer to. This means an incompatibility can occur several times.
public private(set) var incompatibilities: [DependencyResolutionNode: [Incompatibility]] = [:]
/// The current best guess for a solution satisfying all requirements.
public private(set) var solution: PartialSolution
private let lock = NSLock()
init(root: DependencyResolutionNode,
overriddenPackages: [PackageReference: (version: BoundVersion, products: ProductFilter)] = [:],
solution: PartialSolution = PartialSolution())
{
self.root = root
self.overriddenPackages = overriddenPackages
self.solution = solution
}
func addIncompatibility(_ incompatibility: Incompatibility, at location: LogLocation) {
self.lock.withLock {
// log("incompat: \(incompatibility) \(location)")
for package in incompatibility.terms.map(\.node) {
if let incompats = self.incompatibilities[package] {
if !incompats.contains(incompatibility) {
self.incompatibilities[package]!.append(incompatibility)
}
} else {
self.incompatibilities[package] = [incompatibility]
}
}
}
}
/// Find all incompatibilities containing a positive term for a given package.
func positiveIncompatibilities(for node: DependencyResolutionNode) -> [Incompatibility]? {
self.lock.withLock {
guard let all = self.incompatibilities[node] else {
return nil
}
return all.filter {
$0.terms.first { $0.node == node }!.isPositive
}
}
}
func decide(_ node: DependencyResolutionNode, at version: Version) {
let term = Term(node, .exact(version))
self.lock.withLock {
assert(term.isValidDecision(for: self.solution))
self.solution.decide(node, at: version)
}
}
func derive(_ term: Term, cause: Incompatibility) {
self.lock.withLock {
self.solution.derive(term, cause: cause)
}
}
func backtrack(toDecisionLevel: Int) {
self.lock.withLock {
self.solution.backtrack(toDecisionLevel: toDecisionLevel)
}
}
}
/// `Package.resolved` representation.
private let resolvedPackages: ResolvedPackagesStore.ResolvedPackages
/// The container provider used to load package containers.
private let provider: ContainerProvider
/// Reference to the package container provider.
private let packageContainerProvider: PackageContainerProvider
/// Should resolver prefetch the containers.
private let prefetchBasedOnResolvedFile: Bool
/// Update containers while fetching them.
private let skipDependenciesUpdates: Bool
/// Resolver delegate
private let delegate: DependencyResolverDelegate?
@available(*,
deprecated,
renamed: "init(provider:resolvedPackages:skipDependenciesUpdates:prefetchBasedOnResolvedFile:observabilityScope:delegate:)",
message: "Renamed for consistency with the actual name of the feature"
)
@_disfavoredOverload
public init(
provider: PackageContainerProvider,
pins: ResolvedPackagesStore.ResolvedPackages = [:],
skipDependenciesUpdates: Bool = false,
prefetchBasedOnResolvedFile: Bool = false,
observabilityScope: ObservabilityScope,
delegate: DependencyResolverDelegate? = nil
) {
self.init(
provider: provider,
resolvedPackages: pins,
skipDependenciesUpdates: skipDependenciesUpdates,
prefetchBasedOnResolvedFile: prefetchBasedOnResolvedFile,
observabilityScope: observabilityScope,
delegate: delegate
)
}
public init(
provider: PackageContainerProvider,
resolvedPackages: ResolvedPackagesStore.ResolvedPackages = [:],
skipDependenciesUpdates: Bool = false,
prefetchBasedOnResolvedFile: Bool = false,
observabilityScope: ObservabilityScope,
delegate: DependencyResolverDelegate? = nil
) {
self.packageContainerProvider = provider
self.resolvedPackages = resolvedPackages
self.skipDependenciesUpdates = skipDependenciesUpdates
self.prefetchBasedOnResolvedFile = prefetchBasedOnResolvedFile
self.provider = ContainerProvider(
provider: self.packageContainerProvider,
skipUpdate: self.skipDependenciesUpdates,
resolvedPackages: self.resolvedPackages,
observabilityScope: observabilityScope
)
self.delegate = delegate
}
/// Execute the resolution algorithm to find a valid assignment of versions.
public func solve(constraints: [Constraint]) async -> Result<[DependencyResolverBinding], Error> {
// the graph resolution root
let root: DependencyResolutionNode
if constraints.count == 1, let constraint = constraints.first, constraint.package.kind.isRoot {
// root level package, use it as our resolution root
root = .root(package: constraint.package)
} else {
// more complex setup requires a synthesized root
root = .root(package: .root(
identity: .plain("<synthesized-root>"),
path: .root
))
}
do {
// strips state
let bindings = try await self.solve(root: root, constraints: constraints).bindings
return .success(bindings)
} catch {
// If version solving failing, build the user-facing diagnostic.
if let pubGrubError = error as? PubGrubError, let rootCause = pubGrubError.rootCause, let incompatibilities = pubGrubError.incompatibilities {
do {
var builder = DiagnosticReportBuilder(
root: root,
incompatibilities: incompatibilities,
provider: self.provider
)
let diagnostic = try builder.makeErrorReport(for: rootCause)
return .failure(PubGrubError.unresolvable(diagnostic))
} catch {
// failed to construct the report, will report the original error
return .failure(error)
}
}
return .failure(error)
}
}
/// Find a set of dependencies that fit the given constraints. If dependency
/// resolution is unable to provide a result, an error is thrown.
/// - Warning: It is expected that the root package reference has been set before this is called.
internal func solve(root: DependencyResolutionNode, constraints: [Constraint]) async throws -> (bindings: [DependencyResolverBinding], state: State) {
// first process inputs
let inputs = try await self.processInputs(root: root, with: constraints)
// Prefetch the containers if prefetching is enabled.
if self.prefetchBasedOnResolvedFile {
// We avoid prefetching packages that are overridden since
// otherwise we'll end up creating a repository container
// for them.
let resolvedPackageReferences = self.resolvedPackages.values
.map(\.packageRef)
.filter { !inputs.overriddenPackages.keys.contains($0) }
self.provider.prefetch(containers: resolvedPackageReferences)
}
let state = State(root: root, overriddenPackages: inputs.overriddenPackages)
// Decide root at v1.
state.decide(state.root, at: "1.0.0")
// Add the root incompatibility.
state.addIncompatibility(Incompatibility(terms: [Term(not: root, .exact("1.0.0"))], cause: .root), at: .topLevel)
// Add inputs root incompatibilities.
for incompatibility in inputs.rootIncompatibilities {
state.addIncompatibility(incompatibility, at: .topLevel)
}
try await self.run(state: state)
let decisions = state.solution.assignments.filter(\.isDecision)
var flattenedAssignments: [PackageReference: (binding: BoundVersion, products: ProductFilter)] = [:]
for assignment in decisions {
if assignment.term.node == state.root {
continue
}
let boundVersion: BoundVersion
switch assignment.term.requirement {
case .exact(let version):
boundVersion = .version(version)
case .range, .any, .empty, .ranges:
throw InternalError("unexpected requirement value for assignment \(assignment.term)")
}
let products = assignment.term.node.productFilter
let container = try await withCheckedThrowingContinuation {
self.provider.getContainer(
for: assignment.term.node.package,
completion: $0.resume(with:)
)
}
let updatePackage = try container.underlying.loadPackageReference(at: boundVersion)
if var existing = flattenedAssignments[updatePackage] {
guard existing.binding == boundVersion else {
throw InternalError("Two products in one package resolved to different versions: \(existing.products)@\(existing.binding) vs \(products)@\(boundVersion)")
}
existing.products.formUnion(products)
flattenedAssignments[updatePackage] = existing
} else {
flattenedAssignments[updatePackage] = (binding: boundVersion, products: products)
}
}
var finalAssignments: [DependencyResolverBinding]
= flattenedAssignments.keys.sorted(by: { $0.deprecatedName < $1.deprecatedName }).map { package in
let details = flattenedAssignments[package]!
return .init(package: package, boundVersion: details.binding, products: details.products)
}
// Add overridden packages to the result.
for (package, override) in state.overriddenPackages {
let container = try await withCheckedThrowingContinuation {
self.provider.getContainer(for: package, completion: $0.resume(with:))
}
let updatePackage = try container.underlying.loadPackageReference(at: override.version)
finalAssignments.append(.init(
package: updatePackage,
boundVersion: override.version,
products: override.products
))
}
self.delegate?.solved(result: finalAssignments)
return (finalAssignments, state)
}
private func processInputs(
root: DependencyResolutionNode,
with constraints: [Constraint]
) async throws -> (
overriddenPackages: [PackageReference: (version: BoundVersion, products: ProductFilter)],
rootIncompatibilities: [Incompatibility]
) {
// The list of constraints that we'll be working with. We start with the input constraints
// and process them in two phases. The first phase finds all unversioned constraints and
// the second phase discovers all branch-based constraints.
var constraints = OrderedCollections.OrderedSet(constraints)
// The list of packages that are overridden in the graph. A local package reference will
// always override any other kind of package reference and branch-based reference will override
// version-based reference.
var overriddenPackages: [PackageReference: (version: BoundVersion, products: ProductFilter)] = [:]
// The list of version-based references reachable via local and branch-based references.
// These are added as top-level incompatibilities since they always need to be satisfied.
// Some of these might be overridden as we discover local and branch-based references.
var versionBasedDependencies = OrderedCollections.OrderedDictionary<DependencyResolutionNode, [VersionBasedConstraint]>()
// Process unversioned constraints in first phase. We go through all of the unversioned packages
// and collect them and their dependencies. This gives us the complete list of unversioned
// packages in the graph since unversioned packages can only be referred by other
// unversioned packages.
while let constraint = constraints.first(where: { $0.requirement == .unversioned }) {
constraints.remove(constraint)
// Mark the package as overridden.
if var existing = overriddenPackages[constraint.package] {
guard existing.version == .unversioned else {
throw InternalError("Overridden package is not unversioned: \(constraint.package)@\(existing.version)")
}
existing.products.formUnion(constraint.products)
overriddenPackages[constraint.package] = existing
} else {
overriddenPackages[constraint.package] = (version: .unversioned, products: constraint.products)
}
for node in constraint.nodes() {
// Process dependencies of this package.
//
// We collect all version-based dependencies in a separate structure so they can
// be process at the end. This allows us to override them when there is a non-version
// based (unversioned/branch-based) constraint present in the graph.
let container = try await withCheckedThrowingContinuation {
self.provider.getContainer(
for: node.package,
completion: $0.resume(with:)
)
}
for dependency in try container.underlying.getUnversionedDependencies(productFilter: node.productFilter) {
if let versionedBasedConstraints = VersionBasedConstraint.constraints(dependency) {
for constraint in versionedBasedConstraints {
versionBasedDependencies[node, default: []].append(constraint)
}
} else if !overriddenPackages.keys.contains(dependency.package) {
// Add the constraint if its not already present. This will ensure we don't
// end up looping infinitely due to a cycle (which are diagnosed separately).
constraints.append(dependency)
}
}
}
}
// Process revision-based constraints in the second phase. Here we do the similar processing
// as the first phase but we also ignore the constraints that are overridden due to
// presence of unversioned constraints.
while let constraint = constraints.first(where: { $0.requirement.isRevision }) {
guard case .revision(let revision) = constraint.requirement else {
throw InternalError("Expected revision requirement")
}
constraints.remove(constraint)
let package = constraint.package
// Check if there is an existing value for this package in the overridden packages.
switch overriddenPackages[package]?.version {
case .excluded?, .version?:
// These values are not possible.
throw InternalError("Unexpected value for overridden package \(package) in \(overriddenPackages)")
case .unversioned?:
// This package is overridden by an unversioned package so we can ignore this constraint.
continue
case .revision(let existingRevision, let branch)?:
// If this branch-based package was encountered before, ensure the references match.
if (branch ?? existingRevision) != revision {
throw PubGrubError.unresolvable("\(package.identity) is required using two different revision-based requirements (\(existingRevision) and \(revision)), which is not supported")
} else {
// Otherwise, continue since we've already processed this constraint. Any cycles will be diagnosed separately.
continue
}
case nil:
break
}
// Process dependencies of this package, similar to the first phase but branch-based dependencies
// are not allowed to contain local/unversioned packages.
let container = try await withCheckedThrowingContinuation {
self.provider.getContainer(for: package, completion: $0.resume(with:))
}
// If there is a pin for this revision-based dependency, get
// the dependencies at the pinned revision instead of using
// latest commit on that branch. Note that if this revision-based dependency is
// already a commit, then its pin entry doesn't matter in practice.
let revisionForDependencies: String
if case .branch(revision, let pinRevision) = self.resolvedPackages[package.identity]?.state {
revisionForDependencies = pinRevision
// Mark the package as overridden with the pinned revision and record the branch as well.
overriddenPackages[package] = (version: .revision(revisionForDependencies, branch: revision), products: constraint.products)
} else {
revisionForDependencies = revision
// Mark the package as overridden.
overriddenPackages[package] = (version: .revision(revision), products: constraint.products)
}
for node in constraint.nodes() {
var unprocessedDependencies = try container.underlying.getDependencies(
at: revisionForDependencies,
productFilter: constraint.products
)
if let sharedRevision = node.revisionLock(revision: revision) {
unprocessedDependencies.append(sharedRevision)
}
for dependency in unprocessedDependencies {
switch dependency.requirement {
case .versionSet(let req):
for node in dependency.nodes() {
let versionedBasedConstraint = VersionBasedConstraint(node: node, req: req)
versionBasedDependencies[.root(package: constraint.package), default: []].append(versionedBasedConstraint)
}
case .revision:
constraints.append(dependency)
case .unversioned:
throw DependencyResolverError.revisionDependencyContainsLocalPackage(
dependency: package.identity.description,
localPackage: dependency.package.identity.description
)
}
}
}
}
// At this point, we should be left with only version-based requirements in our constraints
// list. Add them to our version-based dependency list.
for constraint in constraints {
switch constraint.requirement {
case .versionSet(let req):
for node in constraint.nodes() {
let versionedBasedConstraint = VersionBasedConstraint(node: node, req: req)
versionBasedDependencies[root, default: []].append(versionedBasedConstraint)
}
case .revision, .unversioned:
throw InternalError("Unexpected revision/unversioned requirement in the constraints list: \(constraints)")
}
}
// Finally, compute the root incompatibilities (which will be all version-based).
// note versionBasedDependencies may point to the root package dependencies, or the dependencies of root's non-versioned dependencies
var rootIncompatibilities: [Incompatibility] = []
for (node, constraints) in versionBasedDependencies {
for constraint in constraints {
if overriddenPackages.keys.contains(constraint.node.package) { continue }
let incompat = try Incompatibility(
Term(root, .exact("1.0.0")),
Term(not: constraint.node, constraint.requirement),
root: root,
cause: .dependency(node: node)
)
rootIncompatibilities.append(incompat)
}
}
return (overriddenPackages, rootIncompatibilities)
}
/// Perform unit propagation, resolving conflicts if necessary and making
/// decisions if nothing else is left to be done.
/// After this method returns `solution` is either populated with a list of
/// final version assignments or an error is thrown.
private func run(state: State) async throws {
var next: DependencyResolutionNode? = state.root
while let nxt = next {
try self.propagate(state: state, node: nxt)
// initiate prefetch of known packages that will be used to make the decision on the next step
self.provider.prefetch(containers: state.solution.undecided.map(\.node.package))
// If decision making determines that no more decisions are to be
// made, it returns nil to signal that version solving is done.
next = try await withCheckedThrowingContinuation {
self.makeDecision(state: state, completion: $0.resume(with:))
}
}
}
/// Perform unit propagation to derive new assignments based on the current
/// partial solution.
/// If a conflict is found, the conflicting incompatibility is returned to
/// resolve the conflict on.
internal func propagate(state: State, node: DependencyResolutionNode) throws {
var changed: OrderedCollections.OrderedSet<DependencyResolutionNode> = [node]
while !changed.isEmpty {
let package = changed.removeFirst()
loop: for incompatibility in state.positiveIncompatibilities(for: package)?.reversed() ?? [] {
let result = self.propagate(state: state, incompatibility: incompatibility)
switch result {
case .conflict:
let rootCause = try self.resolve(state: state, conflict: incompatibility)
let rootCauseResult = self.propagate(state: state, incompatibility: rootCause)
guard case .almostSatisfied(let pkg) = rootCauseResult else {
throw InternalError("""
Expected root cause \(rootCause) to almost satisfy the \
current partial solution:
\(state.solution.assignments.map { " * \($0.description)" }.joined(separator: "\n"))\n
""")
}
changed.removeAll(keepingCapacity: false)
changed.append(pkg)
break loop
case .almostSatisfied(let package):
changed.append(package)
case .none:
break
}
}
}
}
private func propagate(state: State, incompatibility: Incompatibility) -> PropagationResult {
var unsatisfied: Term?
for term in incompatibility.terms {
let relation = state.solution.relation(with: term)
if relation == .disjoint {
return .none
} else if relation == .overlap {
if unsatisfied != nil {
return .none
}
unsatisfied = term
}
}
// We have a conflict if all the terms of the incompatibility were satisfied.
guard let unsatisfiedTerm = unsatisfied else {
return .conflict
}
state.derive(unsatisfiedTerm.inverse, cause: incompatibility)
self.delegate?.derived(term: unsatisfiedTerm.inverse)
return .almostSatisfied(node: unsatisfiedTerm.node)
}
// Based on:
// https://github.com/dart-lang/pub/tree/master/doc/solver.md#conflict-resolution
// https://github.com/dart-lang/pub/blob/master/lib/src/solver/version_solver.dart#L201
internal func resolve(state: State, conflict: Incompatibility) throws -> Incompatibility {
self.delegate?.conflict(conflict: conflict)
var incompatibility = conflict
var createdIncompatibility = false
// rdar://93335995
// hard protection from infinite loops
let maxIterations = 1000
var iterations = 0
while !isCompleteFailure(incompatibility, root: state.root) {
var mostRecentTerm: Term?
var mostRecentSatisfier: Assignment?
var difference: Term?
var previousSatisfierLevel = 0
for term in incompatibility.terms {
let satisfier = try state.solution.satisfier(for: term)
if let _mostRecentSatisfier = mostRecentSatisfier {
let mostRecentSatisfierIdx = state.solution.assignments.firstIndex(of: _mostRecentSatisfier)!
let satisfierIdx = state.solution.assignments.firstIndex(of: satisfier)!
if mostRecentSatisfierIdx < satisfierIdx {
previousSatisfierLevel = max(previousSatisfierLevel, _mostRecentSatisfier.decisionLevel)
mostRecentTerm = term
mostRecentSatisfier = satisfier
difference = nil
} else {
previousSatisfierLevel = max(previousSatisfierLevel, satisfier.decisionLevel)
}
} else {
mostRecentTerm = term
mostRecentSatisfier = satisfier
}
if mostRecentTerm == term {
difference = mostRecentSatisfier?.term.difference(with: term)
if let difference {
previousSatisfierLevel = max(previousSatisfierLevel, try state.solution.satisfier(for: difference.inverse).decisionLevel)
}
}
}
guard let _mostRecentSatisfier = mostRecentSatisfier else {
throw InternalError("mostRecentSatisfier not set")
}
if previousSatisfierLevel < _mostRecentSatisfier.decisionLevel || _mostRecentSatisfier.cause == nil {
state.backtrack(toDecisionLevel: previousSatisfierLevel)
if createdIncompatibility {
state.addIncompatibility(incompatibility, at: .conflictResolution)
}
return incompatibility
}
let priorCause = _mostRecentSatisfier.cause!
var newTerms = Array(incompatibility.terms.filter { $0 != mostRecentTerm })
newTerms += priorCause.terms.filter { $0.node != _mostRecentSatisfier.term.node }
if let _difference = difference {
// rdar://93335995
// do not add the exact inverse of a requirement as it can lead to endless loops
if _difference.inverse != mostRecentTerm {
newTerms.append(_difference.inverse)
}
}
incompatibility = try Incompatibility(
OrderedCollections.OrderedSet(newTerms),
root: state.root,
cause: .conflict(cause: .init(conflict: incompatibility, other: priorCause))
)
createdIncompatibility = true
if let mostRecentTerm {
if let difference {
self.delegate?.partiallySatisfied(term: mostRecentTerm, by: _mostRecentSatisfier, incompatibility: incompatibility, difference: difference)
} else {
self.delegate?.satisfied(term: mostRecentTerm, by: _mostRecentSatisfier, incompatibility: incompatibility)
}
}
// rdar://93335995
// hard protection from infinite loops
iterations = iterations + 1
if iterations >= maxIterations {
break
}
}
self.delegate?.failedToResolve(incompatibility: incompatibility)
throw PubGrubError._unresolvable(incompatibility, state.incompatibilities)
}
/// Does a given incompatibility specify that version solving has entirely
/// failed, meaning this incompatibility is either empty or only for the root
/// package.
private func isCompleteFailure(_ incompatibility: Incompatibility, root: DependencyResolutionNode) -> Bool {
incompatibility.terms.isEmpty || (incompatibility.terms.count == 1 && incompatibility.terms.first?.node == root)
}
private func computeCounts(for terms: [Term], completion: @escaping (Result<[Term: Int], Error>) -> Void) {
if terms.isEmpty {
return completion(.success([:]))
}
let sync = DispatchGroup()
let results = ThreadSafeKeyValueStore<Term, Result<Int, Error>>()
terms.forEach { term in
sync.enter()
provider.getContainer(for: term.node.package) { result in
defer { sync.leave() }
results[term] = result.flatMap { container in Result(catching: { try container.versionCount(term.requirement) }) }
}
}
sync.notify(queue: .sharedConcurrent) {
do {
completion(.success(try results.mapValues { try $0.get() }))
} catch {
completion(.failure(error))
}
}
}
internal func makeDecision(state: State, completion: @escaping (Result<DependencyResolutionNode?, Error>) -> Void) {
// If there are no more undecided terms, version solving is complete.
let undecided = state.solution.undecided
guard !undecided.isEmpty else {
return completion(.success(nil))
}
// Prefer packages with least number of versions that fit the current requirements so we
// get conflicts (if any) sooner.
self.computeCounts(for: undecided) { result in
do {
let start = DispatchTime.now()
let counts = try result.get()
// forced unwraps safe since we are testing for count and errors above
let pkgTerm = undecided.min {
// Prefer packages that don't allow pre-release versions
// to allow propagation logic to find dependencies that
// limit the range before making any decisions. This means
// that we'd always prefer release versions.
if $0.supportsPrereleases != $1.supportsPrereleases {
return !$0.supportsPrereleases
}
return counts[$0]! < counts[$1]!
}!
self.delegate?.willResolve(term: pkgTerm)
// at this point the container is cached
let container = try self.provider.getCachedContainer(for: pkgTerm.node.package)
// Get the best available version for this package.
guard let version = try container.getBestAvailableVersion(for: pkgTerm) else {
state.addIncompatibility(try Incompatibility(pkgTerm, root: state.root, cause: .noAvailableVersion), at: .decisionMaking)
return completion(.success(pkgTerm.node))
}
// Add all of this version's dependencies as incompatibilities.
let depIncompatibilities = try container.incompatibilites(
at: version,
node: pkgTerm.node,
overriddenPackages: state.overriddenPackages,
root: state.root
)
var haveConflict = false
for incompatibility in depIncompatibilities {
// Add the incompatibility to our partial solution.
state.addIncompatibility(incompatibility, at: .decisionMaking)
// Check if this incompatibility will satisfy the solution.
haveConflict = haveConflict || incompatibility.terms.allSatisfy {
// We only need to check if the terms other than this package
// are satisfied because we _know_ that the terms matching
// this package will be satisfied if we make this version
// as a decision.
$0.node == pkgTerm.node || state.solution.satisfies($0)
}
}
// Decide this version if there was no conflict with its dependencies.
if !haveConflict {
self.delegate?.didResolve(term: pkgTerm, version: version, duration: start.distance(to: .now()))
state.decide(pkgTerm.node, at: version)
}
completion(.success(pkgTerm.node))
} catch {
completion(.failure(error))
}
}
}
}
internal enum LogLocation: String {
case topLevel = "top level"
case unitPropagation = "unit propagation"
case decisionMaking = "decision making"
case conflictResolution = "conflict resolution"
}
public extension PubGrubDependencyResolver {
enum PubGrubError: Swift.Error, CustomStringConvertible {
case _unresolvable(Incompatibility, [DependencyResolutionNode: [Incompatibility]])
case unresolvable(String)
public var description: String {
switch self {
case ._unresolvable(let rootCause, _):
rootCause.description
case .unresolvable(let error):
error
}
}
var rootCause: Incompatibility? {
switch self {
case ._unresolvable(let rootCause, _):
rootCause
case .unresolvable:
nil
}
}
var incompatibilities: [DependencyResolutionNode: [Incompatibility]]? {
switch self {
case ._unresolvable(_, let incompatibilities):
incompatibilities
case .unresolvable:
nil
}
}
}
}
extension PubGrubDependencyResolver {
private struct VersionBasedConstraint {
let node: DependencyResolutionNode
let requirement: VersionSetSpecifier
init(node: DependencyResolutionNode, req: VersionSetSpecifier) {
self.node = node
self.requirement = req
}
internal static func constraints(_ constraint: Constraint) -> [VersionBasedConstraint]? {
switch constraint.requirement {
case .versionSet(let req):
constraint.nodes().map { VersionBasedConstraint(node: $0, req: req) }
case .revision:
nil
case .unversioned:
nil
}
}
}
}
private enum PropagationResult {
case conflict
case almostSatisfied(node: DependencyResolutionNode)
case none
}
private extension PackageRequirement {
var isRevision: Bool {
switch self {
case .versionSet, .unversioned:
false
case .revision:
true
}
}
}