OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library pub.solver.backtracking_solver; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:collection' show Queue; |
| 9 |
| 10 import '../barback.dart' as barback; |
| 11 import '../exceptions.dart'; |
| 12 import '../lock_file.dart'; |
| 13 import '../log.dart' as log; |
| 14 import '../package.dart'; |
| 15 import '../pubspec.dart'; |
| 16 import '../sdk.dart' as sdk; |
| 17 import '../source_registry.dart'; |
| 18 import '../source/unknown.dart'; |
| 19 import '../utils.dart'; |
| 20 import '../version.dart'; |
| 21 import 'dependency_queue.dart'; |
| 22 import 'version_queue.dart'; |
| 23 import 'version_solver.dart'; |
| 24 |
| 25 /// The top-level solver. |
| 26 /// |
| 27 /// Keeps track of the current potential solution, and the other possible |
| 28 /// versions for speculative package selections. Backtracks and advances to the |
| 29 /// next potential solution in the case of a failure. |
| 30 class BacktrackingSolver { |
| 31 final SolveType type; |
| 32 final SourceRegistry sources; |
| 33 final Package root; |
| 34 |
| 35 /// The lockfile that was present before solving. |
| 36 final LockFile lockFile; |
| 37 |
| 38 final PubspecCache cache; |
| 39 |
| 40 /// The set of packages that are being explicitly upgraded. |
| 41 /// |
| 42 /// The solver will only allow the very latest version for each of these |
| 43 /// packages. |
| 44 final _forceLatest = new Set<String>(); |
| 45 |
| 46 /// The set of packages whose dependecy is being overridden by the root |
| 47 /// package, keyed by the name of the package. |
| 48 /// |
| 49 /// Any dependency on a package that appears in this map will be overriden |
| 50 /// to use the one here. |
| 51 final _overrides = new Map<String, PackageDep>(); |
| 52 |
| 53 /// The package versions currently selected by the solver, along with the |
| 54 /// versions which are remaining to be tried. |
| 55 /// |
| 56 /// Every time a package is encountered when traversing the dependency graph, |
| 57 /// the solver must select a version for it, sometimes when multiple versions |
| 58 /// are valid. This keeps track of which versions have been selected so far |
| 59 /// and which remain to be tried. |
| 60 /// |
| 61 /// Each entry in the list is a [VersionQueue], which is an ordered queue of |
| 62 /// versions to try for a single package. It maintains the currently selected |
| 63 /// version for that package. When a new dependency is encountered, a queue |
| 64 /// of versions of that dependency is pushed onto the end of the list. A |
| 65 /// queue is removed from the list once it's empty, indicating that none of |
| 66 /// the versions provided a solution. |
| 67 /// |
| 68 /// The solver tries versions in depth-first order, so only the last queue in |
| 69 /// the list will have items removed from it. When a new constraint is placed |
| 70 /// on an already-selected package, and that constraint doesn't match the |
| 71 /// selected version, that will cause the current solution to fail and |
| 72 /// trigger backtracking. |
| 73 final _selected = <VersionQueue>[]; |
| 74 |
| 75 /// The number of solutions the solver has tried so far. |
| 76 int get attemptedSolutions => _attemptedSolutions; |
| 77 var _attemptedSolutions = 1; |
| 78 |
| 79 BacktrackingSolver(SolveType type, SourceRegistry sources, this.root, |
| 80 this.lockFile, List<String> useLatest) |
| 81 : type = type, |
| 82 sources = sources, |
| 83 cache = new PubspecCache(type, sources) { |
| 84 for (var package in useLatest) { |
| 85 _forceLatest.add(package); |
| 86 } |
| 87 |
| 88 for (var override in root.dependencyOverrides) { |
| 89 _overrides[override.name] = override; |
| 90 } |
| 91 |
| 92 // A deeply nested statement that's hard on the formatter. |
| 93 isTwoWay = !isEvent && bindings.isWhole && (isCustomTag || |
| 94 tag == 'input' && (name == 'value' || name =='checked') || |
| 95 tag == 'select' && (name == 'selectedindex' || name == 'value') || |
| 96 tag == 'textarea' && name == 'value'); |
| 97 |
| 98 // Even more deeply nested pathological example. |
| 99 if (javaBooleanAnd( |
| 100 javaBooleanAnd( |
| 101 javaBooleanAnd( |
| 102 javaBooleanAnd( |
| 103 javaBooleanAnd( |
| 104 javaBooleanAnd( |
| 105 javaBooleanAnd( |
| 106 javaBooleanAnd(), |
| 107 _isEqualTokens( |
| 108 node.period, |
| 109 toNode.period)), |
| 110 _isEqualNodes( |
| 111 node.name, |
| 112 toNode.name)), |
| 113 _isEqualNodes( |
| 114 node.parameters, |
| 115 toNode.parameters)), |
| 116 _isEqualTokens( |
| 117 node.separator, |
| 118 toNode.separator)), |
| 119 _isEqualNodeLists(node.initializers, toNode.initializers)), |
| 120 _isEqualNodes( |
| 121 node.redirectedConstructor, |
| 122 toNode.redirectedConstructor)), |
| 123 _isEqualNodes(node.body, toNode.body))) { |
| 124 toNode.element = node.element; |
| 125 } |
| 126 } |
| 127 |
| 128 /// Run the solver. |
| 129 /// |
| 130 /// Completes with a list of specific package versions if successful or an |
| 131 /// error if it failed to find a solution. |
| 132 Future<SolveResult> solve() { |
| 133 var stopwatch = new Stopwatch(); |
| 134 |
| 135 _logParameters(); |
| 136 |
| 137 // Sort the overrides by package name to make sure they're deterministic. |
| 138 var overrides = _overrides.values.toList(); |
| 139 overrides.sort((a, b) => a.name.compareTo(b.name)); |
| 140 |
| 141 return newFuture(() { |
| 142 stopwatch.start(); |
| 143 |
| 144 // Pre-cache the root package's known pubspec. |
| 145 cache.cache(new PackageId.root(root), root.pubspec); |
| 146 |
| 147 _validateSdkConstraint(root.pubspec); |
| 148 return _traverseSolution(); |
| 149 }).then((packages) { |
| 150 var pubspecs = new Map.fromIterable( |
| 151 packages, |
| 152 key: (id) => id.name, |
| 153 value: (id) => cache.getCachedPubspec(id)); |
| 154 |
| 155 return new SolveResult.success( |
| 156 sources, |
| 157 root, |
| 158 lockFile, |
| 159 packages, |
| 160 overrides, |
| 161 pubspecs, |
| 162 _getAvailableVersions(packages), |
| 163 attemptedSolutions); |
| 164 }).catchError((error) { |
| 165 if (error is! SolveFailure) throw error; |
| 166 |
| 167 // Wrap a failure in a result so we can attach some other data. |
| 168 return new SolveResult.failure( |
| 169 sources, |
| 170 root, |
| 171 lockFile, |
| 172 overrides, |
| 173 error, |
| 174 attemptedSolutions); |
| 175 }).whenComplete(() { |
| 176 // Gather some solving metrics. |
| 177 var buffer = new StringBuffer(); |
| 178 buffer.writeln('${runtimeType} took ${stopwatch.elapsed} seconds.'); |
| 179 buffer.writeln(cache.describeResults()); |
| 180 log.solver(buffer); |
| 181 }); |
| 182 } |
| 183 |
| 184 /// Generates a map containing all of the known available versions for each |
| 185 /// package in [packages]. |
| 186 /// |
| 187 /// The version list may not always be complete. The the package is the root |
| 188 /// root package, or its a package that we didn't unlock while solving |
| 189 /// because we weren't trying to upgrade it, we will just know the current |
| 190 /// version. |
| 191 Map<String, List<Version>> _getAvailableVersions(List<PackageId> packages) { |
| 192 var availableVersions = new Map<String, List<Version>>(); |
| 193 for (var package in packages) { |
| 194 var cached = cache.getCachedVersions(package.toRef()); |
| 195 var versions; |
| 196 if (cached != null) { |
| 197 versions = cached.map((id) => id.version).toList(); |
| 198 } else { |
| 199 // If the version list was never requested, just use the one known |
| 200 // version. |
| 201 versions = [package.version]; |
| 202 } |
| 203 |
| 204 availableVersions[package.name] = versions; |
| 205 } |
| 206 |
| 207 return availableVersions; |
| 208 } |
| 209 |
| 210 /// Adds [versions], which is the list of all allowed versions of a given |
| 211 /// package, to the set of versions to consider for solutions. |
| 212 /// |
| 213 /// The first item in the list will be the currently selected version of that |
| 214 /// package. Subsequent items will be tried if it the current selection fails. |
| 215 /// Returns the first selected version. |
| 216 PackageId select(VersionQueue versions) { |
| 217 _selected.add(versions); |
| 218 logSolve(); |
| 219 return versions.current; |
| 220 } |
| 221 |
| 222 /// Returns the the currently selected id for the package [name] or `null` if |
| 223 /// no concrete version has been selected for that package yet. |
| 224 PackageId getSelected(String name) { |
| 225 // Always prefer the root package. |
| 226 if (root.name == name) return new PackageId.root(root); |
| 227 |
| 228 // Look through the current selections. |
| 229 for (var i = _selected.length - 1; i >= 0; i--) { |
| 230 if (_selected[i].current.name == name) return _selected[i].current; |
| 231 } |
| 232 |
| 233 return null; |
| 234 } |
| 235 |
| 236 /// Gets the version of [package] currently locked in the lock file. |
| 237 /// |
| 238 /// Returns `null` if it isn't in the lockfile (or has been unlocked). |
| 239 PackageId getLocked(String package) { |
| 240 if (type == SolveType.GET) return lockFile.packages[package]; |
| 241 |
| 242 // When downgrading, we don't want to force the latest versions of |
| 243 // non-hosted packages, since they don't support multiple versions and thus |
| 244 // can't be downgraded. |
| 245 if (type == SolveType.DOWNGRADE) { |
| 246 var locked = lockFile.packages[package]; |
| 247 if (locked != null && !sources[locked.source].hasMultipleVersions) { |
| 248 return locked; |
| 249 } |
| 250 } |
| 251 |
| 252 if (_forceLatest.isEmpty || _forceLatest.contains(package)) return null; |
| 253 return lockFile.packages[package]; |
| 254 } |
| 255 |
| 256 /// Traverses the root package's dependency graph using the current potential |
| 257 /// solution. |
| 258 /// |
| 259 /// If successful, completes to the solution. If not, backtracks to the most |
| 260 /// recently selected version of a package and tries the next version of it. |
| 261 /// If there are no more versions, continues to backtrack to previous |
| 262 /// selections, and so on. If there is nothing left to backtrack to, |
| 263 /// completes to the last failure that occurred. |
| 264 Future<List<PackageId>> _traverseSolution() => resetStack(() { |
| 265 return new Traverser(this).traverse().catchError((error) { |
| 266 if (error is! SolveFailure) throw error; |
| 267 |
| 268 return _backtrack(error).then((canTry) { |
| 269 if (canTry) { |
| 270 _attemptedSolutions++; |
| 271 return _traverseSolution(); |
| 272 } |
| 273 |
| 274 // All out of solutions, so fail. |
| 275 throw error; |
| 276 }); |
| 277 }); |
| 278 }); |
| 279 |
| 280 /// Backtracks from the current failed solution and determines the next |
| 281 /// solution to try. |
| 282 /// |
| 283 /// If possible, it will backjump based on the cause of the [failure] to |
| 284 /// minize backtracking. Otherwise, it will simply backtrack to the next |
| 285 /// possible solution. |
| 286 /// |
| 287 /// Returns `true` if there is a new solution to try. |
| 288 Future<bool> _backtrack(SolveFailure failure) { |
| 289 // Bail if there is nothing to backtrack to. |
| 290 if (_selected.isEmpty) return new Future.value(false); |
| 291 |
| 292 // Mark any packages that may have led to this failure so that we know to |
| 293 // consider them when backtracking. |
| 294 var dependers = _getTransitiveDependers(failure.package); |
| 295 |
| 296 for (var selected in _selected) { |
| 297 if (dependers.contains(selected.current.name)) { |
| 298 selected.fail(); |
| 299 } |
| 300 } |
| 301 |
| 302 // Advance past the current version of the leaf-most package. |
| 303 advanceVersion() { |
| 304 _backjump(failure); |
| 305 var previous = _selected.last.current; |
| 306 return _selected.last.advance().then((success) { |
| 307 if (success) { |
| 308 logSolve(); |
| 309 return true; |
| 310 } |
| 311 |
| 312 logSolve('$previous is last version, backtracking'); |
| 313 |
| 314 // That package has no more versions, so pop it and try the next one. |
| 315 _selected.removeLast(); |
| 316 if (_selected.isEmpty) return false; |
| 317 |
| 318 // If we got here, the leafmost package was discarded so we need to |
| 319 // advance the next one. |
| 320 return advanceVersion(); |
| 321 }); |
| 322 } |
| 323 |
| 324 return advanceVersion(); |
| 325 } |
| 326 |
| 327 /// Walks the selected packages from most to least recent to determine which |
| 328 /// ones can be ignored and jumped over by the backtracker. |
| 329 /// |
| 330 /// The only packages we need to backtrack to are ones that led (possibly |
| 331 /// indirectly) to the failure. Everything else can be skipped. |
| 332 void _backjump(SolveFailure failure) { |
| 333 for (var i = _selected.length - 1; i >= 0; i--) { |
| 334 // Each queue will never be empty since it gets discarded by _backtrack() |
| 335 // when that happens. |
| 336 var selected = _selected[i].current; |
| 337 |
| 338 // If the failure is a disjoint version range, then no possible versions |
| 339 // for that package can match and there's no reason to try them. Instead, |
| 340 // just backjump past it. |
| 341 if (failure is DisjointConstraintException && |
| 342 selected.name == failure.package) { |
| 343 logSolve("skipping past disjoint selected ${selected.name}"); |
| 344 continue; |
| 345 } |
| 346 |
| 347 if (_selected[i].hasFailed) { |
| 348 logSolve('backjump to ${selected.name}'); |
| 349 _selected.removeRange(i + 1, _selected.length); |
| 350 return; |
| 351 } |
| 352 } |
| 353 |
| 354 // If we got here, we walked the entire list without finding a package that |
| 355 // could lead to another solution, so discard everything. This will happen |
| 356 // if every package that led to the failure has no other versions that it |
| 357 // can try to select. |
| 358 _selected.removeRange(1, _selected.length); |
| 359 } |
| 360 |
| 361 /// Gets the set of currently selected packages that depend on [dependency] |
| 362 /// either directly or indirectly. |
| 363 /// |
| 364 /// When backtracking, it's only useful to consider changing the version of |
| 365 /// packages who have a dependency on the failed package that triggered |
| 366 /// backtracking. This is used to determine those packages. |
| 367 /// |
| 368 /// We calculate the full set up front before backtracking because during |
| 369 /// backtracking, we will unselect packages and start to lose this |
| 370 /// information in the middle of the process. |
| 371 /// |
| 372 /// For example, consider dependencies A -> B -> C. We've selected A and B |
| 373 /// then encounter a problem with C. We start backtracking. B has no more |
| 374 /// versions so we discard it and keep backtracking to A. When we get there, |
| 375 /// since we've unselected B, we no longer realize that A had a transitive |
| 376 /// dependency on C. We would end up backjumping over A and failing. |
| 377 /// |
| 378 /// Calculating the dependency set up front before we start backtracking |
| 379 /// solves that. |
| 380 Set<String> _getTransitiveDependers(String dependency) { |
| 381 // Generate a reverse dependency graph. For each package, create edges to |
| 382 // each package that depends on it. |
| 383 var dependers = new Map<String, Set<String>>(); |
| 384 |
| 385 addDependencies(name, deps) { |
| 386 dependers.putIfAbsent(name, () => new Set<String>()); |
| 387 for (var dep in deps) { |
| 388 dependers.putIfAbsent(dep.name, () => new Set<String>()).add(name); |
| 389 } |
| 390 } |
| 391 |
| 392 for (var i = 0; i < _selected.length; i++) { |
| 393 var id = _selected[i].current; |
| 394 var pubspec = cache.getCachedPubspec(id); |
| 395 if (pubspec != null) addDependencies(id.name, pubspec.dependencies); |
| 396 } |
| 397 |
| 398 // Include the root package's dependencies. |
| 399 addDependencies(root.name, root.immediateDependencies); |
| 400 |
| 401 // Now walk the depending graph to see which packages transitively depend |
| 402 // on [dependency]. |
| 403 var visited = new Set<String>(); |
| 404 walk(String package) { |
| 405 // Don't get stuck in cycles. |
| 406 if (visited.contains(package)) return; |
| 407 visited.add(package); |
| 408 var depender = dependers[package].forEach(walk); |
| 409 } |
| 410 |
| 411 walk(dependency); |
| 412 return visited; |
| 413 } |
| 414 |
| 415 /// Logs the initial parameters to the solver. |
| 416 void _logParameters() { |
| 417 var buffer = new StringBuffer(); |
| 418 buffer.writeln("Solving dependencies:"); |
| 419 for (var package in root.dependencies) { |
| 420 buffer.write("- $package"); |
| 421 var locked = getLocked(package.name); |
| 422 if (_forceLatest.contains(package.name)) { |
| 423 buffer.write(" (use latest)"); |
| 424 } else if (locked != null) { |
| 425 var version = locked.version; |
| 426 buffer.write(" (locked to $version)"); |
| 427 } |
| 428 buffer.writeln(); |
| 429 } |
| 430 log.solver(buffer.toString().trim()); |
| 431 } |
| 432 |
| 433 /// Logs [message] in the context of the current selected packages. |
| 434 /// |
| 435 /// If [message] is omitted, just logs a description of leaf-most selection. |
| 436 void logSolve([String message]) { |
| 437 if (message == null) { |
| 438 if (_selected.isEmpty) { |
| 439 message = "* start at root"; |
| 440 } else { |
| 441 message = "* select ${_selected.last.current}"; |
| 442 } |
| 443 } else { |
| 444 // Otherwise, indent it under the current selected package. |
| 445 message = prefixLines(message); |
| 446 } |
| 447 |
| 448 // Indent for the previous selections. |
| 449 var prefix = _selected.skip(1).map((_) => '| ').join(); |
| 450 log.solver(prefixLines(message, prefix: prefix)); |
| 451 } |
| 452 } |
| 453 |
| 454 /// Given the solver's current set of selected package versions, this tries to |
| 455 /// traverse the dependency graph and see if a complete set of valid versions |
| 456 /// has been chosen. |
| 457 /// |
| 458 /// If it reaches a conflict, it fails and stops traversing. If it reaches a |
| 459 /// package that isn't selected, it refines the solution by adding that |
| 460 /// package's set of allowed versions to the solver and then select the best |
| 461 /// one and continuing. |
| 462 class Traverser { |
| 463 final BacktrackingSolver _solver; |
| 464 |
| 465 /// The queue of packages left to traverse. |
| 466 /// |
| 467 /// We do a breadth-first traversal using an explicit queue just to avoid the |
| 468 /// code complexity of a recursive asynchronous traversal. |
| 469 final _packages = new Queue<PackageId>(); |
| 470 |
| 471 /// The packages we have already traversed. |
| 472 /// |
| 473 /// Used to avoid traversing the same package multiple times, and to build |
| 474 /// the complete solution results. |
| 475 final _visited = new Set<PackageId>(); |
| 476 |
| 477 /// The dependencies visited so far in the traversal. |
| 478 /// |
| 479 /// For each package name (the map key) we track the list of dependencies |
| 480 /// that other packages have placed on it so that we can calculate the |
| 481 /// complete constraint for shared dependencies. |
| 482 final _dependencies = <String, List<Dependency>>{}; |
| 483 |
| 484 Traverser(this._solver); |
| 485 |
| 486 /// Walks the dependency graph starting at the root package and validates |
| 487 /// that each reached package has a valid version selected. |
| 488 Future<List<PackageId>> traverse() { |
| 489 // Start at the root. |
| 490 _packages.add(new PackageId.root(_solver.root)); |
| 491 return _traversePackage(); |
| 492 } |
| 493 |
| 494 /// Traverses the next package in the queue. |
| 495 /// |
| 496 /// Completes to a list of package IDs if the traversal completed |
| 497 /// successfully and found a solution. Completes to an error if the traversal |
| 498 /// failed. Otherwise, recurses to the next package in the queue, etc. |
| 499 Future<List<PackageId>> _traversePackage() { |
| 500 if (_packages.isEmpty) { |
| 501 // We traversed the whole graph. If we got here, we successfully found |
| 502 // a solution. |
| 503 return new Future<List<PackageId>>.value(_visited.toList()); |
| 504 } |
| 505 |
| 506 var id = _packages.removeFirst(); |
| 507 |
| 508 // Don't visit the same package twice. |
| 509 if (_visited.contains(id)) { |
| 510 return _traversePackage(); |
| 511 } |
| 512 _visited.add(id); |
| 513 |
| 514 return _solver.cache.getPubspec(id).then((pubspec) { |
| 515 _validateSdkConstraint(pubspec); |
| 516 |
| 517 var deps = pubspec.dependencies.toSet(); |
| 518 |
| 519 if (id.isRoot) { |
| 520 // Include dev dependencies of the root package. |
| 521 deps.addAll(pubspec.devDependencies); |
| 522 |
| 523 // Add all overrides. This ensures a dependency only present as an |
| 524 // override is still included. |
| 525 deps.addAll(_solver._overrides.values); |
| 526 } |
| 527 |
| 528 // Replace any overridden dependencies. |
| 529 deps = deps.map((dep) { |
| 530 var override = _solver._overrides[dep.name]; |
| 531 if (override != null) return override; |
| 532 |
| 533 // Not overridden. |
| 534 return dep; |
| 535 }).toSet(); |
| 536 |
| 537 // Make sure the package doesn't have any bad dependencies. |
| 538 for (var dep in deps) { |
| 539 if (!dep.isRoot && _solver.sources[dep.source] is UnknownSource) { |
| 540 throw new UnknownSourceException( |
| 541 id.name, |
| 542 [new Dependency(id.name, id.version, dep)]); |
| 543 } |
| 544 } |
| 545 |
| 546 return _traverseDeps(id, new DependencyQueue(_solver, deps)); |
| 547 }).catchError((error) { |
| 548 if (error is! PackageNotFoundException) throw error; |
| 549 |
| 550 // We can only get here if the lockfile refers to a specific package |
| 551 // version that doesn't exist (probably because it was yanked). |
| 552 throw new NoVersionException(id.name, null, id.version, []); |
| 553 }); |
| 554 } |
| 555 |
| 556 /// Traverses the references that [depender] depends on, stored in [deps]. |
| 557 /// |
| 558 /// Desctructively modifies [deps]. Completes to a list of packages if the |
| 559 /// traversal is complete. Completes it to an error if a failure occurred. |
| 560 /// Otherwise, recurses. |
| 561 Future<List<PackageId>> _traverseDeps(PackageId depender, |
| 562 DependencyQueue deps) { |
| 563 // Move onto the next package if we've traversed all of these references. |
| 564 if (deps.isEmpty) return _traversePackage(); |
| 565 |
| 566 return resetStack(() { |
| 567 return deps.advance().then((dep) { |
| 568 var dependency = new Dependency(depender.name, depender.version, dep); |
| 569 return _registerDependency(dependency).then((_) { |
| 570 if (dep.name == "barback") return _addImplicitDependencies(); |
| 571 }); |
| 572 }).then((_) => _traverseDeps(depender, deps)); |
| 573 }); |
| 574 } |
| 575 |
| 576 /// Register [dependency]'s constraints on the package it depends on and |
| 577 /// enqueues the package for processing if necessary. |
| 578 Future _registerDependency(Dependency dependency) { |
| 579 return new Future.sync(() { |
| 580 _validateDependency(dependency); |
| 581 |
| 582 var dep = dependency.dep; |
| 583 var dependencies = _getDependencies(dep.name); |
| 584 dependencies.add(dependency); |
| 585 |
| 586 var constraint = _getConstraint(dep.name); |
| 587 |
| 588 // See if it's possible for a package to match that constraint. |
| 589 if (constraint.isEmpty) { |
| 590 var constraints = dependencies.map( |
| 591 (dep) => " ${dep.dep.constraint} from ${dep.depender}").join('\n'); |
| 592 _solver.logSolve('disjoint constraints on ${dep.name}:\n$constraints'); |
| 593 throw new DisjointConstraintException(dep.name, dependencies); |
| 594 } |
| 595 |
| 596 var selected = _validateSelected(dep, constraint); |
| 597 if (selected != null) { |
| 598 // The selected package version is good, so enqueue it to traverse |
| 599 // into it. |
| 600 _packages.add(selected); |
| 601 return null; |
| 602 } |
| 603 |
| 604 // We haven't selected a version. Try all of the versions that match |
| 605 // the constraints we currently have for this package. |
| 606 var locked = _getValidLocked(dep.name); |
| 607 |
| 608 return VersionQueue.create(locked, () { |
| 609 return _getAllowedVersions(dep); |
| 610 }).then((versions) => _packages.add(_solver.select(versions))); |
| 611 }); |
| 612 } |
| 613 |
| 614 /// Gets all versions of [dep] that match the current constraints placed on |
| 615 /// it. |
| 616 Future<Iterable<PackageId>> _getAllowedVersions(PackageDep dep) { |
| 617 var constraint = _getConstraint(dep.name); |
| 618 return _solver.cache.getVersions(dep.toRef()).then((versions) { |
| 619 var allowed = versions.where((id) => constraint.allows(id.version)); |
| 620 |
| 621 if (allowed.isEmpty) { |
| 622 _solver.logSolve('no versions for ${dep.name} match $constraint'); |
| 623 throw new NoVersionException( |
| 624 dep.name, |
| 625 null, |
| 626 constraint, |
| 627 _getDependencies(dep.name)); |
| 628 } |
| 629 |
| 630 // If we're doing an upgrade on this package, only allow the latest |
| 631 // version. |
| 632 if (_solver._forceLatest.contains(dep.name)) allowed = [allowed.first]; |
| 633 |
| 634 // Remove the locked version, if any, since that was already handled. |
| 635 var locked = _getValidLocked(dep.name); |
| 636 if (locked != null) { |
| 637 allowed = allowed.where((dep) => dep.version != locked.version); |
| 638 } |
| 639 |
| 640 return allowed; |
| 641 }).catchError((error, stackTrace) { |
| 642 if (error is PackageNotFoundException) { |
| 643 // Show the user why the package was being requested. |
| 644 throw new DependencyNotFoundException( |
| 645 dep.name, |
| 646 error, |
| 647 _getDependencies(dep.name)); |
| 648 } |
| 649 |
| 650 throw error; |
| 651 }); |
| 652 } |
| 653 |
| 654 /// Ensures that dependency [dep] from [depender] is consistent with the |
| 655 /// other dependencies on the same package. |
| 656 /// |
| 657 /// Throws a [SolveFailure] exception if not. Only validates sources and |
| 658 /// descriptions, not the version. |
| 659 void _validateDependency(Dependency dependency) { |
| 660 var dep = dependency.dep; |
| 661 |
| 662 // Make sure the dependencies agree on source and description. |
| 663 var required = _getRequired(dep.name); |
| 664 if (required == null) return; |
| 665 |
| 666 // Make sure all of the existing sources match the new reference. |
| 667 if (required.dep.source != dep.source) { |
| 668 _solver.logSolve( |
| 669 'source mismatch on ${dep.name}: ${required.dep.source} ' '!= ${dep.so
urce}'); |
| 670 throw new SourceMismatchException(dep.name, [required, dependency]); |
| 671 } |
| 672 |
| 673 // Make sure all of the existing descriptions match the new reference. |
| 674 var source = _solver.sources[dep.source]; |
| 675 if (!source.descriptionsEqual(dep.description, required.dep.description)) { |
| 676 _solver.logSolve( |
| 677 'description mismatch on ${dep.name}: ' |
| 678 '${required.dep.description} != ${dep.description}'); |
| 679 throw new DescriptionMismatchException(dep.name, [required, dependency]); |
| 680 } |
| 681 } |
| 682 |
| 683 /// Validates the currently selected package against the new dependency that |
| 684 /// [dep] and [constraint] place on it. |
| 685 /// |
| 686 /// Returns `null` if there is no currently selected package, throws a |
| 687 /// [SolveFailure] if the new reference it not does not allow the previously |
| 688 /// selected version, or returns the selected package if successful. |
| 689 PackageId _validateSelected(PackageDep dep, VersionConstraint constraint) { |
| 690 var selected = _solver.getSelected(dep.name); |
| 691 if (selected == null) return null; |
| 692 |
| 693 // Make sure it meets the constraint. |
| 694 if (!dep.constraint.allows(selected.version)) { |
| 695 _solver.logSolve('selection $selected does not match $constraint'); |
| 696 throw new NoVersionException( |
| 697 dep.name, |
| 698 selected.version, |
| 699 constraint, |
| 700 _getDependencies(dep.name)); |
| 701 } |
| 702 |
| 703 return selected; |
| 704 } |
| 705 |
| 706 /// Register pub's implicit dependencies. |
| 707 /// |
| 708 /// Pub has an implicit version constraint on barback and various other |
| 709 /// packages used in barback's plugin isolate. |
| 710 Future _addImplicitDependencies() { |
| 711 /// Ensure we only add the barback dependency once. |
| 712 if (_getDependencies("barback").length != 1) return new Future.value(); |
| 713 |
| 714 return Future.wait(barback.pubConstraints.keys.map((depName) { |
| 715 var constraint = barback.pubConstraints[depName]; |
| 716 _solver.logSolve( |
| 717 'add implicit $constraint pub dependency on ' '$depName'); |
| 718 |
| 719 var override = _solver._overrides[depName]; |
| 720 |
| 721 // Use the same source and description as the dependency override if one |
| 722 // exists. This is mainly used by the pkgbuild tests, which use dependency |
| 723 // overrides for all repo packages. |
| 724 var pubDep = override == null ? |
| 725 new PackageDep(depName, "hosted", constraint, depName) : |
| 726 override.withConstraint(constraint); |
| 727 return _registerDependency( |
| 728 new Dependency("pub itself", Version.none, pubDep)); |
| 729 })); |
| 730 } |
| 731 |
| 732 /// Gets the list of dependencies for package [name]. |
| 733 /// |
| 734 /// Creates an empty list if needed. |
| 735 List<Dependency> _getDependencies(String name) { |
| 736 return _dependencies.putIfAbsent(name, () => <Dependency>[]); |
| 737 } |
| 738 |
| 739 /// Gets a "required" reference to the package [name]. |
| 740 /// |
| 741 /// This is the first non-root dependency on that package. All dependencies |
| 742 /// on a package must agree on source and description, except for references |
| 743 /// to the root package. This will return a reference to that "canonical" |
| 744 /// source and description, or `null` if there is no required reference yet. |
| 745 /// |
| 746 /// This is required because you may have a circular dependency back onto the |
| 747 /// root package. That second dependency won't be a root dependency and it's |
| 748 /// *that* one that other dependencies need to agree on. In other words, you |
| 749 /// can have a bunch of dependencies back onto the root package as long as |
| 750 /// they all agree with each other. |
| 751 Dependency _getRequired(String name) { |
| 752 return _getDependencies( |
| 753 name).firstWhere((dep) => !dep.dep.isRoot, orElse: () => null); |
| 754 } |
| 755 |
| 756 /// Gets the combined [VersionConstraint] currently being placed on package |
| 757 /// [name]. |
| 758 VersionConstraint _getConstraint(String name) { |
| 759 var constraint = _getDependencies( |
| 760 name).map( |
| 761 (dep) => |
| 762 dep.dep.constraint).fold(VersionConstraint.any, (a, b) => a.inte
rsect(b)); |
| 763 |
| 764 return constraint; |
| 765 } |
| 766 |
| 767 /// Gets the package [name] that's currently contained in the lockfile if it |
| 768 /// meets [constraint] and has the same source and description as other |
| 769 /// references to that package. |
| 770 /// |
| 771 /// Returns `null` otherwise. |
| 772 PackageId _getValidLocked(String name) { |
| 773 var package = _solver.getLocked(name); |
| 774 if (package == null) return null; |
| 775 |
| 776 var constraint = _getConstraint(name); |
| 777 if (!constraint.allows(package.version)) { |
| 778 _solver.logSolve('$package is locked but does not match $constraint'); |
| 779 return null; |
| 780 } else { |
| 781 _solver.logSolve('$package is locked'); |
| 782 } |
| 783 |
| 784 var required = _getRequired(name); |
| 785 if (required != null) { |
| 786 if (package.source != required.dep.source) return null; |
| 787 |
| 788 var source = _solver.sources[package.source]; |
| 789 if (!source.descriptionsEqual( |
| 790 package.description, |
| 791 required.dep.description)) return null; |
| 792 } |
| 793 |
| 794 return package; |
| 795 } |
| 796 |
| 797 /// Run the dart2js compiler. |
| 798 Future _doCompilation(Transform transform) { |
| 799 var provider = new _BarbackCompilerProvider(_environment, transform, |
| 800 generateSourceMaps: _generateSourceMaps); |
| 801 |
| 802 // Create a "path" to the entrypoint script. The entrypoint may not actually |
| 803 // be on disk, but this gives dart2js a root to resolve relative paths |
| 804 // against. |
| 805 var id = transform.primaryInput.id; |
| 806 |
| 807 var entrypoint = _environment.graph.packages[id.package].path(id.path); |
| 808 |
| 809 // Should have more sophisticated error-handling here. Need |
| 810 // to report compile errors to the user in an easily visible way. Need to |
| 811 // make sure paths in errors are mapped to the original source path so they |
| 812 // can understand them. |
| 813 return dart.compile( |
| 814 entrypoint, provider, |
| 815 commandLineOptions: _configCommandLineOptions, |
| 816 csp: _configBool('csp'), |
| 817 checked: _configBool('checked'), |
| 818 minify: _configBool( |
| 819 'minify', defaultsTo: _settings.mode == BarbackMode.RELEASE), |
| 820 verbose: _configBool('verbose'), |
| 821 environment: _configEnvironment, |
| 822 packageRoot: _environment.rootPackage.path("packages"), |
| 823 analyzeAll: _configBool('analyzeAll'), |
| 824 suppressWarnings: _configBool('suppressWarnings'), |
| 825 suppressHints: _configBool('suppressHints'), |
| 826 suppressPackageWarnings: _configBool( |
| 827 'suppressPackageWarnings', defaultsTo: true), |
| 828 terse: _configBool('terse'), |
| 829 includeSourceMapUrls: _settings.mode != BarbackMode.RELEASE); |
| 830 } |
| 831 } |
| 832 |
| 833 /// Ensures that if [pubspec] has an SDK constraint, then it is compatible |
| 834 /// with the current SDK. |
| 835 /// |
| 836 /// Throws a [SolveFailure] if not. |
| 837 void _validateSdkConstraint(Pubspec pubspec) { |
| 838 if (pubspec.environment.sdkVersion.allows(sdk.version)) return; |
| 839 |
| 840 throw new BadSdkVersionException( |
| 841 pubspec.name, |
| 842 'Package ${pubspec.name} requires SDK version ' |
| 843 '${pubspec.environment.sdkVersion} but the current SDK is ' '${sdk.ver
sion}.'); |
| 844 } |
OLD | NEW |