Skip to content

Commit

Permalink
Require re-activation after minor sdk upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm committed Oct 8, 2024
1 parent 1efd3f5 commit dca5d01
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
12 changes: 12 additions & 0 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,19 @@ To recompile executables, first run `$topLevelProgram pub global deactivate $nam
cache,
);
}
final generatorVersion = entrypoint.packageConfig.generatorVersion;
if (generatorVersion != null &&
(generatorVersion.major != sdk.version.major ||
generatorVersion.minor != sdk.version.minor)) {
dataError('''
${log.bold(name)} was globally activated by Dart $generatorVersion.
You are using ${sdk.version}. Please reactivate.
run:
`$topLevelProgram pub global activate $name` to reactivate.
''');
}
// Check that the SDK constraints the lockFile says we have are honored.
lockFile.sdkConstraints.forEach((sdkName, constraint) {
final sdk = sdks[sdkName];
Expand Down
53 changes: 34 additions & 19 deletions test/global/run/fails_if_sdk_constraint_is_unmet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:path/path.dart' as p;
import 'package:pub/src/exit_codes.dart' as exit_codes;
import 'package:test/test.dart';
import 'package:yaml_edit/yaml_edit.dart';

import '../../descriptor.dart' as d;
import '../../test_pub.dart';
Expand Down Expand Up @@ -40,7 +36,7 @@ void main() {
server.serve(
'foo',
'1.0.0',
sdk: '^3.0.1',
sdk: '^3.1.2',
contents: [
d.dir('bin', [d.file('script.dart', "main(args) => print('123-OK');")]),
],
Expand All @@ -56,10 +52,10 @@ void main() {
await runPub(
environment: {
// Not compatible with [defaultSdkConstraint].
'_PUB_TEST_SDK_VERSION': '3.0.0',
'_PUB_TEST_SDK_VERSION': '3.1.1',
},
args: ['global', 'run', 'foo:script'],
error: contains("foo as globally activated doesn't support Dart 3.0.0."),
error: contains("foo as globally activated doesn't support Dart 3.1.1."),
exitCode: exit_codes.DATA,
);
});
Expand All @@ -85,7 +81,7 @@ void main() {
'1.0.0',
pubspec: {
'environment': {
'sdk': '^3.0.1',
'sdk': '^3.1.2',
},
},
);
Expand All @@ -98,11 +94,11 @@ void main() {
);

await runPub(
environment: {'_PUB_TEST_SDK_VERSION': '3.0.0'},
environment: {'_PUB_TEST_SDK_VERSION': '3.1.1'},
args: ['global', 'run', 'foo:script'],
error: contains(
"""
foo as globally activated doesn't support Dart 3.0.0.
foo as globally activated doesn't support Dart 3.1.1.
try:
`dart pub global activate foo` to reactivate.""",
Expand All @@ -111,7 +107,7 @@ try:
);
});

test('succeeds if SDK is upgraded from 2.19 to 3.0', () async {
test('succeed if there is a patch SDK upgrade from 3.0.0 to 3.0.1', () async {
final server = await servePackages();
server.serve(
'foo',
Expand All @@ -126,22 +122,41 @@ try:
);

await runPub(
environment: {'_PUB_TEST_SDK_VERSION': '2.19.0'},
environment: {'_PUB_TEST_SDK_VERSION': '3.0.0'},
args: ['global', 'activate', 'foo'],
);

final lockFile = File(
p.join(d.sandbox, cachePath, 'global_packages', 'foo', 'pubspec.lock'),
await runPub(
environment: {'_PUB_TEST_SDK_VERSION': '3.0.1'},
args: ['global', 'run', 'foo:script'],
output: contains('123-OK'),
);
});

test('fail if there is a minor SDK upgrade from 3.0.0 to 3.1.0', () async {
final server = await servePackages();
server.serve(
'foo',
'1.0.0',
sdk: '^2.19.0',
contents: [
d.dir(
'bin',
[d.file('script.dart', "main(args) => print('123-OK');")],
),
],
);
final editor = YamlEditor(lockFile.readAsStringSync());
// This corresponds to what an older sdk would write, before the dart 3 hack
// was introduced:
editor.update(['sdks', 'dart'], '^2.19.0');

await runPub(
environment: {'_PUB_TEST_SDK_VERSION': '3.0.0'},
args: ['global', 'activate', 'foo'],
);

await runPub(
environment: {'_PUB_TEST_SDK_VERSION': '3.1.0'},
args: ['global', 'run', 'foo:script'],
output: contains('123-OK'),
error: contains('foo was globally activated by Dart 3.0.0'),
exitCode: exit_codes.DATA,
);
});
}

0 comments on commit dca5d01

Please sign in to comment.