From edc5cc478f7428314fbf34af5d4f5e5b5f73324b Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Tue, 3 Dec 2024 11:23:20 +0000 Subject: [PATCH] Forbid unknown description keys (after 3.7) --- lib/src/global_packages.dart | 2 ++ lib/src/language_version.dart | 5 +++++ lib/src/source/git.dart | 12 +++++++++++- lib/src/source/hosted.dart | 9 +++++++++ test/pubspec_test.dart | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/src/global_packages.dart b/lib/src/global_packages.dart index 16e2f1730..54f04d48a 100644 --- a/lib/src/global_packages.dart +++ b/lib/src/global_packages.dart @@ -13,6 +13,7 @@ import 'entrypoint.dart'; import 'exceptions.dart'; import 'executable.dart' as exec; import 'io.dart'; +import 'language_version.dart'; import 'lock_file.dart'; import 'log.dart' as log; import 'package.dart'; @@ -114,6 +115,7 @@ class GlobalPackages { if (ref != null) 'ref': ref, }, containingDescription: RootDescription(p.current), + languageVersion: LanguageVersion.fromVersion(sdk.version), ); } on FormatException catch (e) { throw ApplicationException(e.message); diff --git a/lib/src/language_version.dart b/lib/src/language_version.dart index 578744458..f7d2b9d3f 100644 --- a/lib/src/language_version.dart +++ b/lib/src/language_version.dart @@ -72,6 +72,9 @@ class LanguageVersion implements Comparable { bool get supportsWorkspaces => this >= firstVersionWithWorkspaces; + bool get forbidsUnknownDescriptionKeys => + this >= firstVersionForbidingUnknownDescriptionKeys; + /// Minimum language version at which short hosted syntax is supported. /// /// This allows `hosted` dependencies to be expressed as: @@ -109,6 +112,8 @@ class LanguageVersion implements Comparable { static const firstVersionWithNullSafety = LanguageVersion(2, 12); static const firstVersionWithShorterHostedSyntax = LanguageVersion(2, 15); static const firstVersionWithWorkspaces = LanguageVersion(3, 5); + static const firstVersionForbidingUnknownDescriptionKeys = + LanguageVersion(3, 7); /// Transform language version to string that can be parsed with /// [LanguageVersion.parse]. diff --git a/lib/src/source/git.dart b/lib/src/source/git.dart index 54f4dd2e1..2c753c653 100644 --- a/lib/src/source/git.dart +++ b/lib/src/source/git.dart @@ -38,7 +38,7 @@ class GitSource extends CachedSource { String name, Object? description, { Description? containingDescription, - LanguageVersion? languageVersion, + required LanguageVersion languageVersion, }) { String url; String? ref; @@ -72,6 +72,16 @@ class GitSource extends CachedSource { 'string.'); } path = descriptionPath; + + if (languageVersion.forbidsUnknownDescriptionKeys) { + for (final key in description.keys) { + if (!['url', 'ref', 'path'].contains(key)) { + throw FormatException( + 'Unknown key "$key" in description.', + ); + } + } + } } final containingDir = switch (containingDescription) { diff --git a/lib/src/source/hosted.dart b/lib/src/source/hosted.dart index c7d5a03b6..cb31c5931 100644 --- a/lib/src/source/hosted.dart +++ b/lib/src/source/hosted.dart @@ -381,6 +381,15 @@ class HostedSource extends CachedSource { } final url = u ?? defaultUrl; + if (languageVersion.forbidsUnknownDescriptionKeys) { + for (final key in description.keys) { + if (!['url', 'name'].contains(key)) { + throw FormatException( + 'Unknown key "$key" in description.', + ); + } + } + } return HostedDescription(name, url as String); } diff --git a/test/pubspec_test.dart b/test/pubspec_test.dart index c181fcc0c..f0f5a0f27 100644 --- a/test/pubspec_test.dart +++ b/test/pubspec_test.dart @@ -1133,5 +1133,39 @@ name: 'foo' ); }); }); + test( + 'Throws after language 3.7 ' + 'if using unknown keys in dependency description', () { + expectPubspecException( + ''' +environment: + sdk: 3.7.0 +dependencies: + foo: + hosted: + name: 'foo' + url: https://pub.dev/ + someOtherProperty: 'smile' +''', + (pubspec) => pubspec.dependencies, + expectedContains: 'Unknown key "someOtherProperty" in description.', + ); + + expectPubspecException( + ''' +environment: + sdk: 3.7.0 +dependencies: + test: + git: + ref: 'v1.0.0' + url: https://github.com/dart-lang/test + path: 'pkgs/test' + someOtherProperty: 'smile' +''', + (pubspec) => pubspec.dependencies, + expectedContains: 'Unknown key "someOtherProperty" in description.', + ); + }); }); }