Skip to content

Commit 04c7827

Browse files
authored
expand the sdk prerelease override logic to work for any pre-release (#1705)
1 parent aad65fe commit 04c7827

File tree

4 files changed

+167
-28
lines changed

4 files changed

+167
-28
lines changed

lib/src/entrypoint.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Entrypoint {
196196
lockFile: lockFile, useLatest: useLatest);
197197

198198
// Log once about all overridden packages.
199-
if (warnAboutPreReleaseTwoDotZeroSdkOverrides && result.pubspecs != null) {
199+
if (warnAboutPreReleaseSdkOverrides && result.pubspecs != null) {
200200
var overriddenPackages = (result.pubspecs.values
201201
.where((pubspec) => pubspec.dartSdkWasOverridden)
202202
.map((pubspec) => pubspec.name)
@@ -205,8 +205,8 @@ class Entrypoint {
205205
.join(', ');
206206
if (overriddenPackages.isNotEmpty) {
207207
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'
208+
'Overriding the upper bound Dart SDK constraint to <=${sdk.version} '
209+
'for the following packages:\n\n${overriddenPackages}\n\n'
210210
'To disable this you can set the PUB_ALLOW_PRERELEASE_SDK system '
211211
'environment variable to `false`, or you can silence this message '
212212
'by setting it to `quiet`.'));
@@ -677,7 +677,7 @@ class Entrypoint {
677677
// It's very unlikely that the lockfile is invalid here, but it's not
678678
// impossible—for example, the user may have a very old application
679679
// package with a checked-in lockfile that's newer than the pubspec, but
680-
// that contains sdk dependencies.
680+
// that contains SDK dependencies.
681681
if (lockFileId.source is UnknownSource) return false;
682682

683683
var packagesFileUri = packages[lockFileId.name];

lib/src/pubspec.dart

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'feature.dart';
1616
import 'io.dart';
1717
import 'log.dart';
1818
import 'package_name.dart';
19+
import 'sdk.dart' as sdk;
1920
import 'source_registry.dart';
2021
import 'utils.dart';
2122

@@ -33,9 +34,6 @@ final _packageName = new RegExp(
3334
final VersionRange _defaultUpperBoundSdkConstraint =
3435
new VersionConstraint.parse("<2.0.0");
3536

36-
/// The upper bound contraint that matches the dev SDK.
37-
final _preReleaseTwoDotZeroSdkVersion = new Version.parse("2.0.0-dev.infinity");
38-
3937
/// Whether or not to allow the pre-release SDK for packages that have an
4038
/// upper bound Dart SDK constraint of <2.0.0.
4139
///
@@ -44,7 +42,7 @@ final _preReleaseTwoDotZeroSdkVersion = new Version.parse("2.0.0-dev.infinity");
4442
///
4543
/// This has a default value of `true` but can be overridden with the
4644
/// PUB_ALLOW_PRERELEASE_SDK system environment variable.
47-
bool get allowPreReleaseTwoDotZeroSdk => allowPreReleaseSdkValue != 'false';
45+
bool get allowPreReleaseSdk => allowPreReleaseSdkValue != 'false';
4846

4947
/// The value of the PUB_ALLOW_PRERELEASE_SDK environment variable, defaulted
5048
/// to `true`.
@@ -63,8 +61,7 @@ Using a default value of `true`.
6361
}();
6462

6563
/// Whether or not to warn about pre-release SDK overrides.
66-
bool get warnAboutPreReleaseTwoDotZeroSdkOverrides =>
67-
allowPreReleaseSdkValue != 'quiet';
64+
bool get warnAboutPreReleaseSdkOverrides => allowPreReleaseSdkValue != 'quiet';
6865

6966
/// Whether or not `features` are enabled.
7067
///
@@ -376,24 +373,45 @@ class Pubspec {
376373
var pair = _parseEnvironment(fields);
377374
var parsedDartSdkConstraint = pair.first;
378375

379-
if (allowPreReleaseTwoDotZeroSdk &&
380-
parsedDartSdkConstraint is VersionRange &&
381-
parsedDartSdkConstraint.max == _defaultUpperBoundSdkConstraint.max &&
382-
!parsedDartSdkConstraint.includeMax) {
376+
if (parsedDartSdkConstraint is VersionRange &&
377+
_shouldEnableCurrentSdk(parsedDartSdkConstraint)) {
383378
_originalDartSdkConstraint = parsedDartSdkConstraint;
384379
_dartSdkWasOverridden = true;
385380
_dartSdkConstraint = new VersionRange(
386381
min: parsedDartSdkConstraint.min,
387382
includeMin: parsedDartSdkConstraint.includeMin,
388-
max: _preReleaseTwoDotZeroSdkVersion,
389-
includeMax: false);
383+
max: sdk.version,
384+
includeMax: true);
390385
} else {
391386
_dartSdkConstraint = parsedDartSdkConstraint;
392387
}
393388

394389
_flutterSdkConstraint = pair.last;
395390
}
396391

392+
/// Whether or not we should override [sdkConstraint] to be <= the user's
393+
/// current SDK version.
394+
///
395+
/// This is true if the following conditions are met:
396+
///
397+
/// - [allowPreReleaseSdk] is `true`
398+
/// - The user's current SDK is a pre-release version.
399+
/// - The original [sdkConstraint] max version is exclusive (`includeMax`
400+
/// is `false`).
401+
/// - The original [sdkConstraint] is not a pre-release version.
402+
/// - The original [sdkConstraint] matches the exact same major, minor, and
403+
/// patch versions as the user's current SDK.
404+
bool _shouldEnableCurrentSdk(VersionRange sdkConstraint) {
405+
if (!allowPreReleaseSdk) return false;
406+
if (!sdk.version.isPreRelease) return false;
407+
if (sdkConstraint.includeMax) return false;
408+
if (sdkConstraint.max == null) return false;
409+
if (sdkConstraint.max.isPreRelease) return false;
410+
return sdkConstraint.max.major == sdk.version.major &&
411+
sdkConstraint.max.minor == sdk.version.minor &&
412+
sdkConstraint.max.patch == sdk.version.patch;
413+
}
414+
397415
/// Parses the "environment" field in [parent] and returns the Dart and
398416
/// Flutter SDK constraints, respectively.
399417
Pair<VersionConstraint, VersionConstraint> _parseEnvironment(YamlMap parent) {

test/pubspec_test.dart

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:pub/src/compiler.dart';
66
import 'package:pub/src/package_name.dart';
77
import 'package:pub/src/pubspec.dart';
8+
import 'package:pub/src/sdk.dart' as sdk;
89
import 'package:pub/src/source.dart';
910
import 'package:pub/src/source_registry.dart';
1011
import 'package:pub/src/system_cache.dart';
@@ -443,33 +444,46 @@ dependencies:
443444
});
444445

445446
group("environment", () {
447+
/// Checking for the default SDK constraint based on the current SDK.
448+
void expectDefaultSdkConstraint(Pubspec pubspec) {
449+
var sdkVersionString = sdk.version.toString();
450+
if (sdkVersionString.startsWith('2.0.0') && sdk.version.isPreRelease) {
451+
expect(
452+
pubspec.dartSdkConstraint,
453+
new VersionConstraint.parse(
454+
'${pubspec.dartSdkConstraint} <=$sdkVersionString'));
455+
} else {
456+
expect(
457+
pubspec.dartSdkConstraint,
458+
new VersionConstraint.parse(
459+
"${pubspec.dartSdkConstraint} <2.0.0"));
460+
}
461+
}
462+
446463
test("allows an omitted environment", () {
447464
var pubspec = new Pubspec.parse('name: testing', sources);
448-
expect(pubspec.dartSdkConstraint,
449-
equals(new VersionConstraint.parse("<2.0.0-dev.infinity")));
465+
expectDefaultSdkConstraint(pubspec);
450466
expect(pubspec.flutterSdkConstraint, isNull);
451467
});
452468

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));
469+
test("default SDK constraint can be omitted with empty environment", () {
470+
var pubspec = new Pubspec.parse('', sources);
471+
expectDefaultSdkConstraint(pubspec);
457472
expect(pubspec.flutterSdkConstraint, isNull);
458473
});
459474

460-
test("defaults the upper constraint for the sdk", () {
475+
test("defaults the upper constraint for the SDK", () {
461476
var pubspec = new Pubspec.parse('''
462477
name: test
463478
environment:
464479
sdk: ">1.0.0"
465480
''', sources);
466-
expect(pubspec.dartSdkConstraint,
467-
equals(new VersionConstraint.parse(">1.0.0 <2.0.0-dev.infinity")));
481+
expectDefaultSdkConstraint(pubspec);
468482
expect(pubspec.flutterSdkConstraint, isNull);
469483
});
470484

471485
test(
472-
"default upper constraint for the sdk applies only if compatibile "
486+
"default upper constraint for the SDK applies only if compatibile "
473487
"with the lower bound", () {
474488
var pubspec = new Pubspec.parse('''
475489
environment:

test/version_solver_test.dart

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ void dartSdkConstraint() {
10681068
});
10691069

10701070
test(
1071-
"pub doesn't log about pre-release sdk overrides if "
1071+
"pub doesn't log about pre-release SDK overrides if "
10721072
"PUB_ALLOW_PRERELEASE_SDK=quiet", () async {
10731073
await d.dir('foo', [
10741074
await d.pubspec({'name': 'foo'})
@@ -1134,9 +1134,116 @@ void dartSdkConstraint() {
11341134

11351135
await expectResolves(
11361136
environment: {'_PUB_TEST_SDK_VERSION': '2.0.0'},
1137-
error: 'Package foo requires SDK version <2.0.0-dev.infinity but the '
1137+
error: 'Package foo requires SDK version <2.0.0 but the '
11381138
'current SDK is 2.0.0.');
11391139
});
1140+
1141+
test("pre-release override requires major SDK versions to match", () async {
1142+
await d.dir(appPath, [
1143+
await d.pubspec({
1144+
'name': 'myapp',
1145+
'environment': {'sdk': '<2.2.3'}
1146+
})
1147+
]).create();
1148+
1149+
await expectResolves(
1150+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3-dev.1.0'},
1151+
output: isNot(contains('PUB_ALLOW_PRERELEASE_SDK')));
1152+
});
1153+
1154+
test("pre-release override requires minor SDK versions to match", () async {
1155+
await d.dir(appPath, [
1156+
await d.pubspec({
1157+
'name': 'myapp',
1158+
'environment': {'sdk': '<1.3.3'}
1159+
})
1160+
]).create();
1161+
1162+
await expectResolves(
1163+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3-dev.1.0'},
1164+
output: isNot(contains('PUB_ALLOW_PRERELEASE_SDK')));
1165+
});
1166+
1167+
test("pre-release override requires patch SDK versions to match", () async {
1168+
await d.dir(appPath, [
1169+
await d.pubspec({
1170+
'name': 'myapp',
1171+
'environment': {'sdk': '<1.2.4'}
1172+
})
1173+
]).create();
1174+
1175+
await expectResolves(
1176+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3-dev.1.0'},
1177+
output: isNot(contains('PUB_ALLOW_PRERELEASE_SDK')));
1178+
});
1179+
1180+
test("pre-release override requires exclusive max", () async {
1181+
await d.dir(appPath, [
1182+
await d.pubspec({
1183+
'name': 'myapp',
1184+
'environment': {'sdk': '<=1.2.3'}
1185+
})
1186+
]).create();
1187+
1188+
await expectResolves(
1189+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3-dev.1.0'},
1190+
output: isNot(contains('PUB_ALLOW_PRERELEASE_SDK')));
1191+
});
1192+
1193+
test("pre-release override requires pre-release SDK", () async {
1194+
await d.dir(appPath, [
1195+
await d.pubspec({
1196+
'name': 'myapp',
1197+
'environment': {'sdk': '<1.2.3'}
1198+
})
1199+
]).create();
1200+
1201+
await expectResolves(
1202+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3'},
1203+
error: 'Package myapp requires SDK version <1.2.3 but the current '
1204+
'SDK is 1.2.3.');
1205+
});
1206+
1207+
test("pre-release override requires no existing pre-release constraints",
1208+
() async {
1209+
await d.dir(appPath, [
1210+
await d.pubspec({
1211+
'name': 'myapp',
1212+
'environment': {'sdk': '<1.2.3-dev.2.0'}
1213+
})
1214+
]).create();
1215+
1216+
await expectResolves(
1217+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3-dev.1.0'},
1218+
output: isNot(contains('PUB_ALLOW_PRERELEASE_SDK')));
1219+
});
1220+
1221+
test("pre-release override requires no build release constraints", () async {
1222+
await d.dir(appPath, [
1223+
await d.pubspec({
1224+
'name': 'myapp',
1225+
'environment': {'sdk': '<1.2.3+1'}
1226+
})
1227+
]).create();
1228+
1229+
await expectResolves(
1230+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3'},
1231+
output: isNot(contains('PUB_ALLOW_PRERELEASE_SDK')));
1232+
});
1233+
1234+
test("pre-release override works generally", () async {
1235+
await d.dir(appPath, [
1236+
await d.pubspec({
1237+
'name': 'myapp',
1238+
'environment': {'sdk': '<1.2.3'}
1239+
})
1240+
]).create();
1241+
1242+
await expectResolves(
1243+
environment: {'_PUB_TEST_SDK_VERSION': '1.2.3-dev.1.0'},
1244+
output: allOf(contains('PUB_ALLOW_PRERELEASE_SDK'),
1245+
contains('<=1.2.3-dev.1.0'), contains('myapp')));
1246+
});
11401247
}
11411248

11421249
void flutterSdkConstraint() {

0 commit comments

Comments
 (0)