Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid unknown description keys (after 3.7) #4458

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/language_version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class LanguageVersion implements Comparable<LanguageVersion> {

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:
Expand Down Expand Up @@ -109,6 +112,8 @@ class LanguageVersion implements Comparable<LanguageVersion> {
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].
Expand Down
12 changes: 11 additions & 1 deletion lib/src/source/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GitSource extends CachedSource {
String name,
Object? description, {
Description? containingDescription,
LanguageVersion? languageVersion,
required LanguageVersion languageVersion,
}) {
String url;
String? ref;
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions lib/src/source/hosted.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
34 changes: 34 additions & 0 deletions test/pubspec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
});
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
});
}
Loading