Skip to content

Commit 6c8357c

Browse files
authored
change default sdk constraint to <2.0.0 (#1698)
This only applies to package dependencies, not the root package. This is to make it so the sdk constraint doesn't become a required field for all pubspecs in 2.x sdks. A future update will make it so publishing a package requires an upper bound sdk constraint.
1 parent 2694a37 commit 6c8357c

File tree

7 files changed

+138
-36
lines changed

7 files changed

+138
-36
lines changed

lib/src/entrypoint.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class Entrypoint {
144144

145145
/// Loads the entrypoint from a package at [rootDir].
146146
Entrypoint(String rootDir, SystemCache cache, {this.isGlobal: false})
147-
: root = new Package.load(null, rootDir, cache.sources),
147+
: root =
148+
new Package.load(null, rootDir, cache.sources, isRootPackage: true),
148149
cache = cache,
149150
_inMemory = false;
150151

lib/src/package.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ class Package {
133133
/// [name] is the expected name of that package (e.g. the name given in the
134134
/// dependency), or `null` if the package being loaded is the entrypoint
135135
/// package.
136-
Package.load(String name, String packageDir, SourceRegistry sources)
136+
Package.load(String name, String packageDir, SourceRegistry sources,
137+
{bool isRootPackage: false})
137138
: dir = packageDir,
138-
pubspec = new Pubspec.load(packageDir, sources, expectedName: name);
139+
pubspec = new Pubspec.load(packageDir, sources,
140+
expectedName: name, includeDefaultSdkConstraint: !isRootPackage);
139141

140142
/// Constructs a package with the given pubspec.
141143
///

lib/src/pubspec.dart

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import 'utils.dart';
2626
final _packageName = new RegExp(
2727
"^${identifierRegExp.pattern}(\\.${identifierRegExp.pattern})*\$");
2828

29-
/// The default SDK constraint for packages that don't declare one.
29+
/// The default SDK upper bound constraint for packages that don't declare one.
3030
///
31-
/// This allows 2.0.0 dev versions to make the migration proecss smoother.
32-
final _defaultSdkConstraint =
33-
new VersionConstraint.parse("<2.0.0-dev.infinity");
31+
/// This provides a sane default for packages that don't have an upper bound.
32+
final _defaultUpperBoundSdkConstraint = new VersionConstraint.parse("<2.0.0");
3433

3534
/// Whether or not `features` are enabled.
3635
///
@@ -67,6 +66,10 @@ class Pubspec {
6766
/// This includes the fields from which other properties are derived.
6867
final YamlMap fields;
6968

69+
/// Whether or not to apply the [_defaultUpperBoundsSdkConstraint] to this
70+
/// pubspec.
71+
bool _includeDefaultSdkConstraint;
72+
7073
/// The package's name.
7174
String get name {
7275
if (_name != null) return _name;
@@ -295,8 +298,8 @@ class Pubspec {
295298
});
296299
}
297300

298-
/// The constraint on the Dart SDK, or [_defaultSdkConstraint] if none is
299-
/// specified.
301+
/// The constraint on the Dart SDK, with [_defaultUpperBoundSdkConstraint] if
302+
/// none is specified.
300303
VersionConstraint get dartSdkConstraint {
301304
_ensureEnvironment();
302305
return _dartSdkConstraint;
@@ -327,7 +330,11 @@ class Pubspec {
327330
Pair<VersionConstraint, VersionConstraint> _parseEnvironment(YamlMap parent) {
328331
var yaml = parent['environment'];
329332
if (yaml == null) {
330-
return new Pair(_defaultSdkConstraint, null);
333+
return new Pair(
334+
_includeDefaultSdkConstraint
335+
? _defaultUpperBoundSdkConstraint
336+
: VersionConstraint.any,
337+
null);
331338
}
332339

333340
if (yaml is! Map) {
@@ -337,7 +344,9 @@ class Pubspec {
337344

338345
return new Pair(
339346
_parseVersionConstraint(yaml.nodes['sdk'],
340-
defaultConstraint: _defaultSdkConstraint),
347+
defaultUpperBoundConstraint: _includeDefaultSdkConstraint
348+
? _defaultUpperBoundSdkConstraint
349+
: null),
341350
yaml.containsKey('flutter')
342351
? _parseVersionConstraint(yaml.nodes['flutter'])
343352
: null);
@@ -485,7 +494,7 @@ class Pubspec {
485494
/// If [expectedName] is passed and the pubspec doesn't have a matching name
486495
/// field, this will throw a [PubspecError].
487496
factory Pubspec.load(String packageDir, SourceRegistry sources,
488-
{String expectedName}) {
497+
{String expectedName, bool includeDefaultSdkConstraint}) {
489498
var pubspecPath = path.join(packageDir, 'pubspec.yaml');
490499
var pubspecUri = path.toUri(pubspecPath);
491500
if (!fileExists(pubspecPath)) {
@@ -498,7 +507,9 @@ class Pubspec {
498507
}
499508

500509
return new Pubspec.parse(readTextFile(pubspecPath), sources,
501-
expectedName: expectedName, location: pubspecUri);
510+
expectedName: expectedName,
511+
includeDefaultSdkConstraint: includeDefaultSdkConstraint,
512+
location: pubspecUri);
502513
}
503514

504515
Pubspec(this._name,
@@ -507,6 +518,7 @@ class Pubspec {
507518
Iterable<PackageRange> devDependencies,
508519
Iterable<PackageRange> dependencyOverrides,
509520
VersionConstraint dartSdkConstraint,
521+
bool includeDefaultSdkConstraint,
510522
VersionConstraint flutterSdkConstraint,
511523
Iterable<Iterable<TransformerConfig>> transformers,
512524
Map fields,
@@ -517,8 +529,12 @@ class Pubspec {
517529
devDependencies == null ? null : devDependencies.toList(),
518530
_dependencyOverrides =
519531
dependencyOverrides == null ? null : dependencyOverrides.toList(),
520-
_dartSdkConstraint = dartSdkConstraint ?? _defaultSdkConstraint,
532+
_dartSdkConstraint =
533+
dartSdkConstraint ?? includeDefaultSdkConstraint == true
534+
? _defaultUpperBoundSdkConstraint
535+
: VersionConstraint.any,
521536
_flutterSdkConstraint = flutterSdkConstraint,
537+
_includeDefaultSdkConstraint = includeDefaultSdkConstraint,
522538
_transformers = transformers == null
523539
? []
524540
: transformers.map((phase) => phase.toSet()).toList(),
@@ -531,8 +547,9 @@ class Pubspec {
531547
_version = Version.none,
532548
_dependencies = <PackageRange>[],
533549
_devDependencies = <PackageRange>[],
534-
_dartSdkConstraint = _defaultSdkConstraint,
550+
_dartSdkConstraint = VersionConstraint.any,
535551
_flutterSdkConstraint = null,
552+
_includeDefaultSdkConstraint = false,
536553
_transformers = <Set<TransformerConfig>>[],
537554
fields = new YamlMap();
538555

@@ -544,10 +561,11 @@ class Pubspec {
544561
///
545562
/// [location] is the location from which this pubspec was loaded.
546563
Pubspec.fromMap(Map fields, this._sources,
547-
{String expectedName, Uri location})
564+
{String expectedName, bool includeDefaultSdkConstraint, Uri location})
548565
: fields = fields is YamlMap
549566
? fields
550-
: new YamlMap.wrap(fields, sourceUrl: location) {
567+
: new YamlMap.wrap(fields, sourceUrl: location),
568+
_includeDefaultSdkConstraint = includeDefaultSdkConstraint ?? true {
551569
// If [expectedName] is passed, ensure that the actual 'name' field exists
552570
// and matches the expectation.
553571
if (expectedName == null) return;
@@ -564,7 +582,7 @@ class Pubspec {
564582
/// If the pubspec doesn't define a version for itself, it defaults to
565583
/// [Version.none].
566584
factory Pubspec.parse(String contents, SourceRegistry sources,
567-
{String expectedName, Uri location}) {
585+
{String expectedName, bool includeDefaultSdkConstraint, Uri location}) {
568586
YamlNode pubspecNode;
569587
try {
570588
pubspecNode = loadYamlNode(contents, sourceUrl: location);
@@ -583,7 +601,9 @@ class Pubspec {
583601
}
584602

585603
return new Pubspec.fromMap(pubspecMap, sources,
586-
expectedName: expectedName, location: location);
604+
expectedName: expectedName,
605+
includeDefaultSdkConstraint: includeDefaultSdkConstraint,
606+
location: location);
587607
}
588608

589609
/// Returns a list of most errors in this pubspec.
@@ -702,17 +722,31 @@ class Pubspec {
702722
return dependencies;
703723
}
704724

705-
/// Parses [node] to a [VersionConstraint], or [defaultConstraint] if no
706-
/// constraint is specified..
725+
/// Parses [node] to a [VersionConstraint].
726+
///
727+
/// If or [defaultUpperBoundConstraint] is specified then it will be set as
728+
/// the max constraint if the original constraint doesn't have an upper
729+
/// bound and it is compatible with [defaultUpperBoundConstraint].
707730
VersionConstraint _parseVersionConstraint(YamlNode node,
708-
{VersionConstraint defaultConstraint}) {
709-
if (node?.value == null) return defaultConstraint;
731+
{VersionConstraint defaultUpperBoundConstraint}) {
732+
if (node?.value == null) {
733+
return defaultUpperBoundConstraint ?? VersionConstraint.any;
734+
}
710735
if (node.value is! String) {
711736
_error('A version constraint must be a string.', node.span);
712737
}
713738

714-
return _wrapFormatException('version constraint', node.span,
715-
() => new VersionConstraint.parse(node.value));
739+
return _wrapFormatException('version constraint', node.span, () {
740+
var constraint = new VersionConstraint.parse(node.value);
741+
if (defaultUpperBoundConstraint != null &&
742+
constraint is VersionRange &&
743+
constraint.max == null &&
744+
defaultUpperBoundConstraint.allowsAny(constraint)) {
745+
constraint = new VersionConstraint.intersection(
746+
[constraint, defaultUpperBoundConstraint]);
747+
}
748+
return constraint;
749+
});
716750
}
717751

718752
/// Parses [node] to a map from feature names to whether those features are

test/pubspec_test.dart

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,36 @@ dependencies:
446446
test("allows an omitted environment", () {
447447
var pubspec = new Pubspec.parse('', sources);
448448
expect(pubspec.dartSdkConstraint,
449-
equals(new VersionConstraint.parse("<2.0.0-dev.infinity")));
449+
equals(new VersionConstraint.parse("<2.0.0")));
450+
expect(pubspec.flutterSdkConstraint, isNull);
451+
});
452+
453+
test("default sdk constraint can be ommited with empty environment", () {
454+
var pubspec =
455+
new Pubspec.parse('', sources, includeDefaultSdkConstraint: false);
456+
expect(pubspec.dartSdkConstraint, equals(VersionConstraint.any));
457+
expect(pubspec.flutterSdkConstraint, isNull);
458+
});
459+
460+
test("defaults the upper constraint for the sdk", () {
461+
var pubspec = new Pubspec.parse('''
462+
environment:
463+
sdk: ">1.0.0"
464+
''', sources);
465+
expect(pubspec.dartSdkConstraint,
466+
equals(new VersionConstraint.parse(">1.0.0 <2.0.0")));
467+
expect(pubspec.flutterSdkConstraint, isNull);
468+
});
469+
470+
test(
471+
"default upper constraint for the sdk applies only if compatibile "
472+
"with the lower bound", () {
473+
var pubspec = new Pubspec.parse('''
474+
environment:
475+
sdk: ">3.0.0"
476+
''', sources);
477+
expect(pubspec.dartSdkConstraint,
478+
equals(new VersionConstraint.parse(">3.0.0")));
450479
expect(pubspec.flutterSdkConstraint, isNull);
451480
});
452481

test/validator/dependency_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ main() {
488488
d.libPubspec("integration_pkg", "1.0.0", deps: {"foo": "^1.2.3"})
489489
]).create();
490490

491-
expectDependencyValidationError(' sdk: ">=1.8.0 <2.0.0-dev.infinity"');
491+
expectDependencyValidationError(' sdk: ">=1.8.0 <2.0.0"');
492492
});
493493

494494
test("with a too-broad SDK constraint", () async {

test/validator/sdk_constraint_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ main() {
7878
]).create();
7979
expect(
8080
validatePackage(sdkConstraint),
81-
completion(pairOf(
82-
anyElement(contains('">=1.19.0 <2.0.0-dev.infinity"')),
83-
isEmpty)));
81+
completion(
82+
pairOf(anyElement(contains('">=1.19.0 <2.0.0"')), isEmpty)));
8483
});
8584
});
8685
}

test/version_solver_test.dart

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,23 +1023,60 @@ void dartSdkConstraint() {
10231023
await expectResolves(result: {'foo': '2.0.0', 'bar': '2.0.0'}, tries: 3);
10241024
});
10251025

1026-
test('allows 2.0.0-dev by default', () async {
1026+
test('root package allows 2.0.0-dev by default', () async {
10271027
await d.dir(appPath, [
10281028
await d.pubspec({'name': 'myapp'})
10291029
]).create();
10301030

10311031
await expectResolves(
1032-
environment: {'_PUB_TEST_SDK_VERSION': '2.0.0-dev.99'}, result: {});
1032+
environment: {'_PUB_TEST_SDK_VERSION': '2.0.0-dev.99'});
10331033
});
10341034

1035-
test('disallows 2.0.0 by default', () async {
1035+
test('root package allows 2.0.0 by default', () async {
10361036
await d.dir(appPath, [
10371037
await d.pubspec({'name': 'myapp'})
10381038
]).create();
10391039

1040+
await expectResolves(environment: {'_PUB_TEST_SDK_VERSION': '2.0.0'});
1041+
});
1042+
1043+
test('package deps disallow 2.0.0-dev by default', () async {
1044+
await d.dir('foo', [
1045+
await d.pubspec({'name': 'foo'})
1046+
]).create();
1047+
1048+
await d.dir(appPath, [
1049+
await d.pubspec({
1050+
'name': 'myapp',
1051+
'dependencies': {
1052+
'foo': {'path': '../foo'}
1053+
}
1054+
})
1055+
]).create();
1056+
1057+
await expectResolves(
1058+
environment: {'_PUB_TEST_SDK_VERSION': '2.0.0-dev.99'},
1059+
error: 'Package foo requires SDK version <2.0.0 but the '
1060+
'current SDK is 2.0.0-dev.99.');
1061+
});
1062+
1063+
test('package deps disallow 2.0.0 by default', () async {
1064+
await d.dir('foo', [
1065+
await d.pubspec({'name': 'foo'})
1066+
]).create();
1067+
1068+
await d.dir(appPath, [
1069+
await d.pubspec({
1070+
'name': 'myapp',
1071+
'dependencies': {
1072+
'foo': {'path': '../foo'}
1073+
}
1074+
})
1075+
]).create();
1076+
10401077
await expectResolves(
10411078
environment: {'_PUB_TEST_SDK_VERSION': '2.0.0'},
1042-
error: 'Package myapp requires SDK version <2.0.0-dev.infinity but the '
1079+
error: 'Package foo requires SDK version <2.0.0 but the '
10431080
'current SDK is 2.0.0.');
10441081
});
10451082
}
@@ -1168,8 +1205,8 @@ void flutterSdkConstraint() {
11681205

11691206
await expectResolves(
11701207
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
1171-
error: 'Package myapp requires SDK version >0.1.2+3 but the current '
1172-
'SDK is 0.1.2+3.');
1208+
error: 'Package myapp requires SDK version >0.1.2+3 but the '
1209+
'current SDK is 0.1.2+3.');
11731210
});
11741211

11751212
test('selects the latest dependency with a matching constraint', () async {

0 commit comments

Comments
 (0)