Error on acquireDependencies when the root pubspec has an unknown sdk in the environment. (#3271)
diff --git a/lib/src/entrypoint.dart b/lib/src/entrypoint.dart index 453ef56..b54ec5e 100644 --- a/lib/src/entrypoint.dart +++ b/lib/src/entrypoint.dart
@@ -10,6 +10,7 @@ import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; import 'package:pub_semver/pub_semver.dart'; +import 'package:yaml/yaml.dart'; import 'command_runner.dart'; import 'dart.dart' as dart; @@ -250,8 +251,7 @@ SolveResult result; try { result = await log.progress('Resolving dependencies$suffix', () async { - // We require an SDK constraint lower-bound as of Dart 2.12.0 - _checkSdkConstraintIsDefined(root.pubspec); + _checkSdkConstraint(root.pubspec); return resolveVersions( type, cache, @@ -872,7 +872,9 @@ } /// We require an SDK constraint lower-bound as of Dart 2.12.0 - void _checkSdkConstraintIsDefined(Pubspec pubspec) { + /// + /// We don't allow unknown sdks. + void _checkSdkConstraint(Pubspec pubspec) { final dartSdkConstraint = pubspec.sdkConstraints['dart']; if (dartSdkConstraint is! VersionRange || dartSdkConstraint.min == null) { // Suggest version range '>=2.10.0 <3.0.0', we avoid using: @@ -902,6 +904,24 @@ See https://dart.dev/go/sdk-constraint '''); } + for (final sdk in pubspec.sdkConstraints.keys) { + if (!sdks.containsKey(sdk)) { + final environment = pubspec.fields.nodes['environment'] as YamlMap; + final keyNode = environment.nodes.entries + .firstWhere((e) => (e.key as YamlNode).value == sdk) + .key as YamlNode; + throw PubspecException(''' +$pubspecPath refers to an unknown sdk '$sdk'. + +Did you mean to add it as a dependency? + +Either remove the constraint, or upgrade to a version of pub that supports the +given sdk. + +See https://dart.dev/go/sdk-constraint +''', keyNode.span); + } + } } }
diff --git a/test/get/unknown_sdk_test.dart b/test/get/unknown_sdk_test.dart new file mode 100644 index 0000000..d0c1de3 --- /dev/null +++ b/test/get/unknown_sdk_test.dart
@@ -0,0 +1,25 @@ +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file +// 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 'package:pub/src/exit_codes.dart' as exit_codes; +import 'package:test/test.dart'; + +import '../descriptor.dart' as d; +import '../test_pub.dart'; + +void main() { + test('pub get barks at unknown sdk', () async { + await d.dir(appPath, [ + d.pubspec({ + 'environment': {'foo': '>=1.2.4 <2.0.0'} + }) + ]).create(); + + await pubGet( + error: contains( + "Error on line 1, column 40 of pubspec.yaml: pubspec.yaml refers to an unknown sdk 'foo'."), + exitCode: exit_codes.DATA, + ); + }); +}