Skip to content

Commit b01f2f4

Browse files
authored
[flutter_tools] Move plugin validation out of asserts (flutter#191359)
## Description Platform plugin `fromYaml` factories in `platform_plugins.dart` (`AndroidPlugin`, `IOSPlugin`, `MacOSPlugin`, `WindowsPlugin`, and `LinuxPlugin`) previously performed validation inside `assert` statements. When Flutter tools runs in release mode (assertions disabled), calling these factories directly could bypass validation and return invalid plugin instances rather than surfacing a clean tool exit. This PR: 1. Replaces `assert(validate(yaml))` in all platform plugin `fromYaml` factories with explicit validation checks that call `throwToolExit`. 2. Includes the plugin name in the exit error message (e.g. `Invalid "android" plugin specification for plugin "$name".`). 3. Adds automated unit tests in `plugins_test.dart` verifying that `fromYaml` throws `ToolExit` when passed invalid YAML. Fixes flutter#67241 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to provide]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to provide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-provide [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://discord.gg/flutterdev [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
1 parent 9204165 commit b01f2f4

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎packages/flutter_tools/lib/src/platform_plugins.dart‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ class AndroidPlugin extends PluginPlatform implements NativeOrDartPlugin {
125125
String pluginPath,
126126
FileSystem fileSystem,
127127
) {
128-
assert(validate(yaml));
128+
if (!validate(yaml)) {
129+
throwToolExit('Invalid "android" plugin specification for plugin "$name".');
130+
}
129131

130132
final dartPluginClass = yaml[kDartPluginClass] as String?;
131133
final dartFileName = yaml[kDartFileName] as String?;
@@ -318,7 +320,9 @@ class IOSPlugin extends PluginPlatform implements NativeOrDartPlugin, DarwinPlug
318320
sharedDarwinSource = sharedDarwinSource ?? false;
319321

320322
factory IOSPlugin.fromYaml(String name, YamlMap yaml) {
321-
assert(validate(yaml)); // TODO(zanderso): https://github.com/flutter/flutter/issues/67241
323+
if (!validate(yaml)) {
324+
throwToolExit('Invalid "ios" plugin specification for plugin "$name".');
325+
}
322326

323327
final dartPluginClass = yaml[kDartPluginClass] as String?;
324328
final dartFileName = yaml[kDartFileName] as String?;
@@ -431,7 +435,9 @@ class MacOSPlugin extends PluginPlatform implements NativeOrDartPlugin, DarwinPl
431435
sharedDarwinSource = sharedDarwinSource ?? false;
432436

433437
factory MacOSPlugin.fromYaml(String name, YamlMap yaml) {
434-
assert(validate(yaml));
438+
if (!validate(yaml)) {
439+
throwToolExit('Invalid "macos" plugin specification for plugin "$name".');
440+
}
435441

436442
final dartPluginClass = yaml[kDartPluginClass] as String?;
437443
final dartFileName = yaml[kDartFileName] as String?;
@@ -541,7 +547,9 @@ class WindowsPlugin extends PluginPlatform implements NativeOrDartPlugin, Varian
541547
);
542548

543549
factory WindowsPlugin.fromYaml(String name, YamlMap yaml) {
544-
assert(validate(yaml));
550+
if (!validate(yaml)) {
551+
throwToolExit('Invalid "windows" plugin specification for plugin "$name".');
552+
}
545553
final pluginClass = yaml[kPluginClass] as String?;
546554
final variants = <PluginPlatformVariant>{};
547555
final variantList = yaml[kSupportedVariants] as YamlList?;
@@ -664,7 +672,9 @@ class LinuxPlugin extends PluginPlatform implements NativeOrDartPlugin {
664672
);
665673

666674
factory LinuxPlugin.fromYaml(String name, YamlMap yaml) {
667-
assert(validate(yaml));
675+
if (!validate(yaml)) {
676+
throwToolExit('Invalid "linux" plugin specification for plugin "$name".');
677+
}
668678

669679
final dartPluginClass = yaml[kDartPluginClass] as String?;
670680
final dartFileName = yaml[kDartFileName] as String?;

‎packages/flutter_tools/test/general.shard/plugins_test.dart‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,29 @@ iosPrefix: FLT
22272227
final YamlMap map = Plugin.createPlatformsYamlMap(<String>[], 'foo', 'bar');
22282228
expect(map.isEmpty, true);
22292229
});
2230+
2231+
testUsingContext('Platform plugin fromYaml factories perform validation', () async {
2232+
expect(
2233+
() => AndroidPlugin.fromYaml('foo', YamlMap.wrap(<String, dynamic>{}), '', globals.fs),
2234+
throwsToolExit(message: 'Invalid "android" plugin specification for plugin "foo".'),
2235+
);
2236+
expect(
2237+
() => IOSPlugin.fromYaml('foo', YamlMap.wrap(<String, dynamic>{})),
2238+
throwsToolExit(message: 'Invalid "ios" plugin specification for plugin "foo".'),
2239+
);
2240+
expect(
2241+
() => MacOSPlugin.fromYaml('foo', YamlMap.wrap(<String, dynamic>{})),
2242+
throwsToolExit(message: 'Invalid "macos" plugin specification for plugin "foo".'),
2243+
);
2244+
expect(
2245+
() => WindowsPlugin.fromYaml('foo', YamlMap.wrap(<String, dynamic>{})),
2246+
throwsToolExit(message: 'Invalid "windows" plugin specification for plugin "foo".'),
2247+
);
2248+
expect(
2249+
() => LinuxPlugin.fromYaml('foo', YamlMap.wrap(<String, dynamic>{})),
2250+
throwsToolExit(message: 'Invalid "linux" plugin specification for plugin "foo".'),
2251+
);
2252+
});
22302253
});
22312254

22322255
group('Plugin files', () {

0 commit comments

Comments
 (0)