Skip to content

Commit aad65fe

Browse files
authored
Add default upper bound Dart SDK constraint override from 2.0.0 -> 2.0.0-dev.infinity (#1704)
Also introduces the PUB_ALLOW_PRERELEASE_SDK environment variable which can override this behavior. Valid values are `true` (default), `false`, and `quiet` (true but no log message about the override). Fixes #1692
1 parent ebc64f7 commit aad65fe

11 files changed

+178
-22
lines changed

lib/src/cached_package.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class _CachedPubspec implements Pubspec {
8787
List<PackageRange> get dependencyOverrides => _inner.dependencyOverrides;
8888
Map<String, Feature> get features => _inner.features;
8989
VersionConstraint get dartSdkConstraint => _inner.dartSdkConstraint;
90+
VersionConstraint get originalDartSdkConstraint =>
91+
_inner.originalDartSdkConstraint;
92+
bool get dartSdkWasOverridden => _inner.dartSdkWasOverridden;
9093
VersionConstraint get flutterSdkConstraint => _inner.flutterSdkConstraint;
9194
String get publishTo => _inner.publishTo;
9295
Map<String, String> get executables => _inner.executables;

lib/src/entrypoint.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'log.dart' as log;
2222
import 'package.dart';
2323
import 'package_name.dart';
2424
import 'package_graph.dart';
25+
import 'pubspec.dart';
2526
import 'sdk.dart' as sdk;
2627
import 'solver/version_solver.dart';
2728
import 'source/cached.dart';
@@ -193,6 +194,25 @@ class Entrypoint {
193194
bool packagesDir: false}) async {
194195
var result = await resolveVersions(type, cache, root,
195196
lockFile: lockFile, useLatest: useLatest);
197+
198+
// Log once about all overridden packages.
199+
if (warnAboutPreReleaseTwoDotZeroSdkOverrides && result.pubspecs != null) {
200+
var overriddenPackages = (result.pubspecs.values
201+
.where((pubspec) => pubspec.dartSdkWasOverridden)
202+
.map((pubspec) => pubspec.name)
203+
.toList()
204+
..sort())
205+
.join(', ');
206+
if (overriddenPackages.isNotEmpty) {
207+
log.message(log.yellow(
208+
'Overriding Dart SDK constraint from <2.0.0 to <2.0.0-dev.infinity'
209+
' for the following packages:\n\n${overriddenPackages}\n\n'
210+
'To disable this you can set the PUB_ALLOW_PRERELEASE_SDK system '
211+
'environment variable to `false`, or you can silence this message '
212+
'by setting it to `quiet`.'));
213+
}
214+
}
215+
196216
if (!result.succeeded) throw result.error;
197217

198218
result.showReport(type);

lib/src/pubspec.dart

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'compiler.dart';
1414
import 'exceptions.dart';
1515
import 'feature.dart';
1616
import 'io.dart';
17+
import 'log.dart';
1718
import 'package_name.dart';
1819
import 'source_registry.dart';
1920
import 'utils.dart';
@@ -29,13 +30,48 @@ final _packageName = new RegExp(
2930
/// The default SDK upper bound constraint for packages that don't declare one.
3031
///
3132
/// This provides a sane default for packages that don't have an upper bound.
32-
final _defaultUpperBoundSdkConstraint = new VersionConstraint.parse("<2.0.0");
33+
final VersionRange _defaultUpperBoundSdkConstraint =
34+
new VersionConstraint.parse("<2.0.0");
35+
36+
/// The upper bound contraint that matches the dev SDK.
37+
final _preReleaseTwoDotZeroSdkVersion = new Version.parse("2.0.0-dev.infinity");
38+
39+
/// Whether or not to allow the pre-release SDK for packages that have an
40+
/// upper bound Dart SDK constraint of <2.0.0.
41+
///
42+
/// If enabled then a Dart SDK upper bound of <2.0.0 is always converted to
43+
/// <2.0.0-dev.infinity.
44+
///
45+
/// This has a default value of `true` but can be overridden with the
46+
/// PUB_ALLOW_PRERELEASE_SDK system environment variable.
47+
bool get allowPreReleaseTwoDotZeroSdk => allowPreReleaseSdkValue != 'false';
48+
49+
/// The value of the PUB_ALLOW_PRERELEASE_SDK environment variable, defaulted
50+
/// to `true`.
51+
final String allowPreReleaseSdkValue = () {
52+
var value =
53+
Platform.environment["PUB_ALLOW_PRERELEASE_SDK"]?.toLowerCase() ?? 'true';
54+
if (!['true', 'quiet', 'false'].contains(value)) {
55+
warning(yellow('''
56+
The environment variable PUB_ALLOW_PRERELEASE_SDK is set as `$value`.
57+
The expected value is either `true`, `quiet` (true but no logging), or `false`.
58+
Using a default value of `true`.
59+
'''));
60+
value = 'true';
61+
}
62+
return value;
63+
}();
64+
65+
/// Whether or not to warn about pre-release SDK overrides.
66+
bool get warnAboutPreReleaseTwoDotZeroSdkOverrides =>
67+
allowPreReleaseSdkValue != 'quiet';
3368

3469
/// Whether or not `features` are enabled.
3570
///
3671
/// This can be overridden manually or by setting the ENABLE_PUB_FEATURES
3772
/// environment variable to "true".
38-
bool featuresEnabled = Platform.environment["ENABLE_PUB_FEATURES"] != "true";
73+
bool featuresEnabled =
74+
Platform.environment["ENABLE_PUB_FEATURES"]?.toLowerCase() != "true";
3975

4076
/// The parsed contents of a pubspec file.
4177
///
@@ -68,7 +104,12 @@ class Pubspec {
68104

69105
/// Whether or not to apply the [_defaultUpperBoundsSdkConstraint] to this
70106
/// pubspec.
71-
bool _includeDefaultSdkConstraint;
107+
final bool _includeDefaultSdkConstraint;
108+
109+
/// Whether or not the SDK version was overridden from <2.0.0 to
110+
/// <2.0.0-dev.infinity.
111+
bool get dartSdkWasOverridden => _dartSdkWasOverridden;
112+
bool _dartSdkWasOverridden = false;
72113

73114
/// The package's name.
74115
String get name {
@@ -300,13 +341,25 @@ class Pubspec {
300341

301342
/// The constraint on the Dart SDK, with [_defaultUpperBoundSdkConstraint] if
302343
/// none is specified.
344+
///
345+
/// This also includes the pre-release override if
346+
/// [allowPreReleaseTwoDotZeroSdk] is `true`.
303347
VersionConstraint get dartSdkConstraint {
304348
_ensureEnvironment();
305349
return _dartSdkConstraint;
306350
}
307351

308352
VersionConstraint _dartSdkConstraint;
309353

354+
/// The original Dart SDK constraint, if [dartSdkWasOverridden] is `true`,
355+
/// otherwise this will be identical to [dartSdkConstraint].
356+
VersionConstraint get originalDartSdkConstraint {
357+
_ensureEnvironment();
358+
return _originalDartSdkConstraint ?? dartSdkConstraint;
359+
}
360+
361+
VersionConstraint _originalDartSdkConstraint;
362+
310363
/// The constraint on the Flutter SDK, or `null` if none is specified.
311364
VersionConstraint get flutterSdkConstraint {
312365
_ensureEnvironment();
@@ -321,7 +374,23 @@ class Pubspec {
321374
if (_dartSdkConstraint != null) return;
322375

323376
var pair = _parseEnvironment(fields);
324-
_dartSdkConstraint = pair.first;
377+
var parsedDartSdkConstraint = pair.first;
378+
379+
if (allowPreReleaseTwoDotZeroSdk &&
380+
parsedDartSdkConstraint is VersionRange &&
381+
parsedDartSdkConstraint.max == _defaultUpperBoundSdkConstraint.max &&
382+
!parsedDartSdkConstraint.includeMax) {
383+
_originalDartSdkConstraint = parsedDartSdkConstraint;
384+
_dartSdkWasOverridden = true;
385+
_dartSdkConstraint = new VersionRange(
386+
min: parsedDartSdkConstraint.min,
387+
includeMin: parsedDartSdkConstraint.includeMin,
388+
max: _preReleaseTwoDotZeroSdkVersion,
389+
includeMax: false);
390+
} else {
391+
_dartSdkConstraint = parsedDartSdkConstraint;
392+
}
393+
325394
_flutterSdkConstraint = pair.last;
326395
}
327396

lib/src/validator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ abstract class Validator {
5757
void validateSdkConstraint(Version firstSdkVersion, String message) {
5858
// If the SDK constraint disallowed all versions before [firstSdkVersion],
5959
// no error is necessary.
60-
if (entrypoint.root.pubspec.dartSdkConstraint
60+
if (entrypoint.root.pubspec.originalDartSdkConstraint
6161
.intersect(new VersionRange(max: firstSdkVersion))
6262
.isEmpty) {
6363
return;
@@ -78,8 +78,8 @@ abstract class Validator {
7878
includeMin: allowedSdks.includeMin,
7979
includeMax: allowedSdks.includeMax);
8080

81-
var newSdkConstraint =
82-
entrypoint.root.pubspec.dartSdkConstraint.intersect(allowedSdks);
81+
var newSdkConstraint = entrypoint.root.pubspec.originalDartSdkConstraint
82+
.intersect(allowedSdks);
8383
if (newSdkConstraint.isEmpty) newSdkConstraint = allowedSdks;
8484

8585
errors.add("$message\n"

lib/src/validator/sdk_constraint.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SdkConstraintValidator extends Validator {
1919
SdkConstraintValidator(Entrypoint entrypoint) : super(entrypoint);
2020

2121
Future validate() async {
22-
var dartConstraint = entrypoint.root.pubspec.dartSdkConstraint;
22+
var dartConstraint = entrypoint.root.pubspec.originalDartSdkConstraint;
2323
if (dartConstraint is VersionRange) {
2424
if (dartConstraint.toString().startsWith("^")) {
2525
errors.add(

test/pubspec_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ dependencies:
444444

445445
group("environment", () {
446446
test("allows an omitted environment", () {
447-
var pubspec = new Pubspec.parse('', sources);
447+
var pubspec = new Pubspec.parse('name: testing', sources);
448448
expect(pubspec.dartSdkConstraint,
449-
equals(new VersionConstraint.parse("<2.0.0")));
449+
equals(new VersionConstraint.parse("<2.0.0-dev.infinity")));
450450
expect(pubspec.flutterSdkConstraint, isNull);
451451
});
452452

@@ -459,11 +459,12 @@ dependencies:
459459

460460
test("defaults the upper constraint for the sdk", () {
461461
var pubspec = new Pubspec.parse('''
462+
name: test
462463
environment:
463464
sdk: ">1.0.0"
464465
''', sources);
465466
expect(pubspec.dartSdkConstraint,
466-
equals(new VersionConstraint.parse(">1.0.0 <2.0.0")));
467+
equals(new VersionConstraint.parse(">1.0.0 <2.0.0-dev.infinity")));
467468
expect(pubspec.flutterSdkConstraint, isNull);
468469
});
469470

test/upgrade/report/describes_change_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ Resolving dependencies\.\.\..*
5656
. source_changed 2\.0\.0 from path \.\.[/\\]source_changed \(was 1\.0\.0\)
5757
. unchanged 1\.0\.0
5858
. version_changed 2\.0\.0 \(was 1\.0\.0\)
59-
""", multiLine: true));
59+
""", multiLine: true), environment: {'PUB_ALLOW_PRERELEASE_SDK': 'false'});
6060
});
6161
}

test/upgrade/report/does_not_show_newer_versions_for_locked_packages_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ main() {
3333
Resolving dependencies\.\.\..*
3434
not_upgraded 1\.0\.0
3535
. upgraded 2\.0\.0 \(was 1\.0\.0\) \(3\.0\.0-dev available\)
36-
""", multiLine: true));
36+
""", multiLine: true), environment: {'PUB_ALLOW_PRERELEASE_SDK': 'false'});
3737
});
3838
}

test/upgrade/report/leading_character_shows_change_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ Resolving dependencies\.\.\..*
8282
> upgraded .*
8383
These packages are no longer being depended on:
8484
- removed .*
85-
""", multiLine: true));
85+
""", multiLine: true), environment: {'PUB_ALLOW_PRERELEASE_SDK': 'false'});
8686
});
8787
}

test/upgrade/report/shows_newer_available_versions_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ Resolving dependencies\.\.\..*
4646
. no_newer 1\.0\.0
4747
. one_newer_stable 1\.0\.0 \(1\.0\.1 available\)
4848
. one_newer_unstable 1\.0\.0 \(1\.0\.1-unstable\.1 available\)
49-
""", multiLine: true));
49+
""", multiLine: true), environment: {'PUB_ALLOW_PRERELEASE_SDK': 'false'});
5050
});
5151
}

0 commit comments

Comments
 (0)