From b4f925e85a510d2d4436818d04987ed9d8dc23c8 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Mon, 26 Feb 2024 20:45:18 +0100 Subject: [PATCH] refactor: Differentiate pubspec and resolution errors for plugins (#142035) Part of #137040 and #80374 - Differentiate pubspec and resolution errors - Rename platform to platformKey - Add TODO for rework logic of flag [throwOnPluginPubspecError] - Swap for loop: handle by platform and then by plugin --- .../lib/src/flutter_plugins.dart | 49 ++++++++------- .../test/general.shard/dart_plugin_test.dart | 63 ++++++++++++++++++- 2 files changed, 85 insertions(+), 27 deletions(-) diff --git a/packages/flutter_tools/lib/src/flutter_plugins.dart b/packages/flutter_tools/lib/src/flutter_plugins.dart index a08b7de379..e9fcb2d7a4 100644 --- a/packages/flutter_tools/lib/src/flutter_plugins.dart +++ b/packages/flutter_tools/lib/src/flutter_plugins.dart @@ -1237,20 +1237,22 @@ bool hasPlugins(FlutterProject project) { /// /// For more details, https://flutter.dev/go/federated-plugins. // TODO(stuartmorgan): Expand implementation to apply to all implementations, -// not just Dart-only, per the federated plugin spec. +// not just Dart-only, per the federated plugin spec. +// TODO(gustl22): The flag [throwOnPluginPubspecError] is currently only used +// for testing dart plugins as the logic is not working correctly. List resolvePlatformImplementation( List plugins, { bool throwOnPluginPubspecError = true, }) { - final List platforms = [ + const Iterable platformKeys = [ AndroidPlugin.kConfigKey, IOSPlugin.kConfigKey, LinuxPlugin.kConfigKey, MacOSPlugin.kConfigKey, WindowsPlugin.kConfigKey, ]; - final Map> possibleResolutions - = >{}; + final Map> possibleResolutions = + >{}; final Map defaultImplementations = {}; // Generates a key for the maps above. String getResolutionKey({required String platform, required String packageName}) { @@ -1258,18 +1260,19 @@ List resolvePlatformImplementation( } bool hasPubspecError = false; - for (final Plugin plugin in plugins) { - for (final String platform in platforms) { - if (plugin.platforms[platform] == null && - plugin.defaultPackagePlatforms[platform] == null) { + for (final String platformKey in platformKeys) { + for (final Plugin plugin in plugins) { + if (plugin.platforms[platformKey] == null && + plugin.defaultPackagePlatforms[platformKey] == null) { // The plugin doesn't implement this platform. continue; } String? implementsPackage = plugin.implementsPackage; if (implementsPackage == null || implementsPackage.isEmpty) { - final String? defaultImplementation = plugin.defaultPackagePlatforms[platform]; + final String? defaultImplementation = + plugin.defaultPackagePlatforms[platformKey]; final bool hasInlineDartImplementation = - plugin.pluginDartClassPlatforms[platform] != null; + plugin.pluginDartClassPlatforms[platformKey] != null; if (defaultImplementation == null && !hasInlineDartImplementation) { if (throwOnPluginPubspecError) { globals.printError( @@ -1279,27 +1282,27 @@ List resolvePlatformImplementation( 'flutter:\n' ' plugin:\n' ' platforms:\n' - ' $platform:\n' + ' $platformKey:\n' ' $kDartPluginClass: \n' '\n' 'To set a default implementation, use:\n' 'flutter:\n' ' plugin:\n' ' platforms:\n' - ' $platform:\n' + ' $platformKey:\n' ' $kDefaultPackage: \n' '\n' 'To implement an interface, use:\n' 'flutter:\n' ' plugin:\n' ' implements: ' - '\n' + '\n', ); } hasPubspecError = true; continue; } - final String defaultImplementationKey = getResolutionKey(platform: platform, packageName: plugin.name); + final String defaultImplementationKey = getResolutionKey(platform: platformKey, packageName: plugin.name); if (defaultImplementation != null) { defaultImplementations[defaultImplementationKey] = defaultImplementation; continue; @@ -1313,7 +1316,7 @@ List resolvePlatformImplementation( // - the plugin requires at least Flutter 2.11 (when this opt-in logic // was added), so that existing plugins continue to work. // See https://github.com/flutter/flutter/issues/87862 for details. - final bool isDesktop = platform == 'linux' || platform == 'macos' || platform == 'windows'; + final bool isDesktop = platformKey == 'linux' || platformKey == 'macos' || platformKey == 'windows'; final semver.VersionConstraint? flutterConstraint = plugin.flutterConstraint; final semver.Version? minFlutterVersion = flutterConstraint != null && flutterConstraint is semver.VersionRange ? flutterConstraint.min : null; @@ -1330,25 +1333,23 @@ List resolvePlatformImplementation( } } // If there's no Dart implementation, there's nothing to register. - if (plugin.pluginDartClassPlatforms[platform] == null || - plugin.pluginDartClassPlatforms[platform] == 'none') { + if (plugin.pluginDartClassPlatforms[platformKey] == null || + plugin.pluginDartClassPlatforms[platformKey] == 'none') { continue; } // If it hasn't been skipped, it's a candidate for auto-registration, so // add it as a possible resolution. - final String resolutionKey = getResolutionKey(platform: platform, packageName: implementsPackage); - if (!possibleResolutions.containsKey(resolutionKey)) { - possibleResolutions[resolutionKey] = []; - } + final String resolutionKey = getResolutionKey(platform: platformKey, packageName: implementsPackage); + possibleResolutions.putIfAbsent(resolutionKey, () => []); possibleResolutions[resolutionKey]!.add(PluginInterfaceResolution( plugin: plugin, - platform: platform, + platform: platformKey, )); } } if (hasPubspecError && throwOnPluginPubspecError) { - throwToolExit('Please resolve the errors'); + throwToolExit('Please resolve the pubspec errors'); } // Now resolve all the possible resolutions to a single option for each @@ -1401,7 +1402,7 @@ List resolvePlatformImplementation( } } if (hasResolutionError) { - throwToolExit('Please resolve the errors'); + throwToolExit('Please resolve the plugin implementation selection errors'); } return finalResolution; } diff --git a/packages/flutter_tools/test/general.shard/dart_plugin_test.dart b/packages/flutter_tools/test/general.shard/dart_plugin_test.dart index 419f407358..b53f2bc1f7 100644 --- a/packages/flutter_tools/test/general.shard/dart_plugin_test.dart +++ b/packages/flutter_tools/test/general.shard/dart_plugin_test.dart @@ -95,6 +95,63 @@ void main() { ); }); + testWithoutContext( + 'selects uncontested implementation from direct dependency with additional native implementation', + () async { + final Set directDependencies = { + 'url_launcher_linux', + 'url_launcher_macos', + }; + final List resolutions = + resolvePlatformImplementation( + [ + // Following plugin is native only and is not resolved as a dart plugin: + Plugin.fromYaml( + 'url_launcher_linux', + '', + YamlMap.wrap({ + 'platforms': { + 'linux': { + 'package': 'com.example.url_launcher', + 'pluginClass': 'UrlLauncherPluginLinux', + }, + }, + }), + null, + [], + fileSystem: fs, + appDependencies: directDependencies, + ), + Plugin.fromYaml( + 'url_launcher_macos', + '', + YamlMap.wrap({ + 'implements': 'url_launcher', + 'platforms': { + 'macos': { + 'dartPluginClass': 'UrlLauncherPluginMacOS', + }, + }, + }), + null, + [], + fileSystem: fs, + appDependencies: directDependencies, + ), + ], + throwOnPluginPubspecError: false, + ); + + expect(resolutions.length, equals(1)); + expect( + resolutions[0].toMap(), + equals({ + 'pluginName': 'url_launcher_macos', + 'dartClass': 'UrlLauncherPluginMacOS', + 'platform': 'macos', + })); + }); + testWithoutContext('selects uncontested implementation from transitive dependency', () async { final Set directDependencies = { 'url_launcher_macos', @@ -538,7 +595,7 @@ void main() { }, throwsToolExit( - message: 'Please resolve the errors', + message: 'Please resolve the plugin implementation selection errors', )); expect( @@ -627,7 +684,7 @@ void main() { ]); }, throwsToolExit( - message: 'Please resolve the errors', + message: 'Please resolve the plugin implementation selection errors', )); expect( @@ -684,7 +741,7 @@ void main() { ]); }, throwsToolExit( - message: 'Please resolve the errors', + message: 'Please resolve the plugin implementation selection errors', )); expect(