Skip to content

Commit 2758d09

Browse files
committed
Support implicit pub dependencies
1 parent 7712907 commit 2758d09

5 files changed

+71
-23
lines changed

lib/src/solver/incompatibility.dart

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ class Incompatibility {
109109

110110
return "${_terse(depender, details, allowEvery: true)} depends on "
111111
"${_terse(dependee, details)}";
112+
} else if (cause == IncompatibilityCause.pubDependency) {
113+
if (terms.length == 1) {
114+
var forbidden = terms.first;
115+
assert(forbidden.isPositive);
116+
117+
// The only one-term pub dependency is on barback, which forbids
118+
// versions outside the range pub supports.
119+
return "pub itself depends on ${_terseRef(forbidden, details)} "
120+
"${VersionConstraint.any.difference(forbidden.constraint)}";
121+
}
122+
123+
assert(terms.length == 2);
124+
assert(terms.first.isPositive);
125+
assert(terms.first.package.name == "barback");
126+
127+
var dependee = terms.last;
128+
assert(!dependee.isPositive);
129+
130+
return "when barback is in use pub itself depends on " +
131+
_terse(dependee, details);
112132
} else if (cause == IncompatibilityCause.useLatest) {
113133
assert(terms.length == 1);
114134

@@ -222,16 +242,19 @@ class Incompatibility {
222242
/// that should be associated with [this] and [other], respectively.
223243
String andToString(Incompatibility other,
224244
[Map<String, PackageDetail> details, int thisLine, int otherLine]) {
225-
var requiresBoth = _tryRequiresBoth(other, details, thisLine, otherLine);
226-
if (requiresBoth != null) return requiresBoth;
227-
228-
var requiresThrough =
229-
_tryRequiresThrough(other, details, thisLine, otherLine);
230-
if (requiresThrough != null) return requiresThrough;
231-
232-
var requiresForbidden =
233-
_tryRequiresForbidden(other, details, thisLine, otherLine);
234-
if (requiresForbidden != null) return requiresForbidden;
245+
if (this.cause != IncompatibilityCause.pubDependency &&
246+
other.cause != IncompatibilityCause.pubDependency) {
247+
var requiresBoth = _tryRequiresBoth(other, details, thisLine, otherLine);
248+
if (requiresBoth != null) return requiresBoth;
249+
250+
var requiresThrough =
251+
_tryRequiresThrough(other, details, thisLine, otherLine);
252+
if (requiresThrough != null) return requiresThrough;
253+
254+
var requiresForbidden =
255+
_tryRequiresForbidden(other, details, thisLine, otherLine);
256+
if (requiresForbidden != null) return requiresForbidden;
257+
}
235258

236259
var buffer = new StringBuffer(this.toString(details));
237260
if (thisLine != null) buffer.write(" $thisLine");

lib/src/solver/incompatibility_cause.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ abstract class IncompatibilityCause {
1919
/// The incompatibility represents a package's dependency.
2020
static const IncompatibilityCause dependency = const _Cause("dependency");
2121

22+
/// The incompatibility represents pub's own dependency, which is activated
23+
/// when barback is selected.
24+
static const IncompatibilityCause pubDependency =
25+
const _Cause("pub dependency");
26+
2227
/// The incompatibility represents the user's request that we use the latest
2328
/// version of a given package.
2429
static const IncompatibilityCause useLatest = const _Cause("use latest");

lib/src/solver/version_solver.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:math' as math;
88
import 'package:collection/collection.dart';
99
import 'package:pub_semver/pub_semver.dart';
1010

11+
import '../barback.dart';
1112
import '../exceptions.dart';
1213
import '../lock_file.dart';
1314
import '../log.dart' as log;
@@ -79,7 +80,29 @@ class VersionSolver {
7980
VersionSolver(this._type, this._systemCache, this._root, this._lockFile,
8081
Iterable<String> useLatest)
8182
: _overriddenPackages = new MapKeySet(_root.pubspec.dependencyOverrides),
82-
_useLatest = new Set.from(useLatest);
83+
_useLatest = new Set.from(useLatest) {
84+
_addImplicitIncompatibilities();
85+
}
86+
87+
/// Adds incompatibilities representing the dependencies pub itself has on
88+
/// various packages to support barback at runtime.
89+
void _addImplicitIncompatibilities() {
90+
var barbackOverride = _root.pubspec.dependencyOverrides["barback"];
91+
var barbackRef = barbackOverride == null
92+
? _systemCache.sources.hosted.refFor("barback")
93+
: barbackOverride.toRef();
94+
95+
pubConstraints.forEach((name, constraint) {
96+
if (_root.pubspec.dependencyOverrides.containsKey(name)) return;
97+
98+
_addIncompatibility(new Incompatibility([
99+
new Term(barbackRef.withConstraint(VersionConstraint.any), true),
100+
new Term(
101+
_systemCache.sources.hosted.refFor(name).withConstraint(constraint),
102+
false)
103+
], IncompatibilityCause.pubDependency));
104+
});
105+
}
83106

84107
/// Finds a set of dependencies that match the root package's constraints, or
85108
/// throws an error if no such set is available.

test/implicit_barback_dependency_test.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@Skip()
6-
75
import 'package:test/test.dart';
86

97
import 'package:pub/src/barback.dart' as barback;
@@ -150,10 +148,11 @@ main() {
150148

151149
await d.appDir({"barback": "any"}).create();
152150

153-
await pubGet(error: """
154-
Package barback has no versions that match >=$current <$max derived from:
155-
- myapp depends on version any
156-
- pub itself depends on version >=$current <$max""");
151+
await pubGet(error: equalsIgnoringWhitespace("""
152+
Because no versions of barback match >=0.15.0 <0.15.3 and pub itself
153+
depends on barback >=0.15.0 <0.15.3, barback is forbidden.
154+
So, because myapp depends on barback any, version solving failed.
155+
"""));
157156
});
158157

159158
test(
@@ -169,9 +168,9 @@ Package barback has no versions that match >=$current <$max derived from:
169168

170169
await d.appDir({"barback": previous}).create();
171170

172-
await pubGet(error: """
173-
Incompatible version constraints on barback:
174-
- myapp depends on version $previous
175-
- pub itself depends on version >=$current <$max""");
171+
await pubGet(error: equalsIgnoringWhitespace("""
172+
Because pub itself depends on barback >=0.15.0 <0.15.3 and myapp depends
173+
on barback 0.14.0, version solving failed.
174+
"""));
176175
});
177176
}

test/implicit_dependency_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@Skip()
6-
75
import 'package:test/test.dart';
86

97
import 'package:pub/src/barback.dart' as barback;

0 commit comments

Comments
 (0)