diff --git a/packages/flutter_tools/lib/src/linux/build_linux.dart b/packages/flutter_tools/lib/src/linux/build_linux.dart index 48a9a943d7..286a332531 100644 --- a/packages/flutter_tools/lib/src/linux/build_linux.dart +++ b/packages/flutter_tools/lib/src/linux/build_linux.dart @@ -9,6 +9,7 @@ import '../base/process.dart'; import '../build_info.dart'; import '../cache.dart'; import '../globals.dart' as globals; +import '../plugins.dart'; import '../project.dart'; import '../reporting/reporting.dart'; @@ -38,6 +39,7 @@ export PROJECT_DIR=${linuxProject.project.directory.path} linuxProject.generatedMakeConfigFile ..createSync(recursive: true) ..writeAsStringSync(buffer.toString()); + createPluginSymlinks(linuxProject.project); if (!buildInfo.isDebug) { const String warning = '🚧 '; diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index 3e71a61cc0..cde8cddeb8 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -318,6 +318,12 @@ List findPlugins(FlutterProject project) { return plugins; } +// Key strings for the .flutter-plugins-dependencies file. +const String _kFlutterPluginsPluginListKey = 'plugins'; +const String _kFlutterPluginsNameKey = 'name'; +const String _kFlutterPluginsPathKey = 'path'; +const String _kFlutterPluginsDependenciesKey = 'dependencies'; + /// Filters [plugins] to those supported by [platformKey]. List> _filterPluginsByPlatform(Listplugins, String platformKey) { final Iterable platformPlugins = plugins.where((Plugin p) { @@ -328,9 +334,9 @@ List findPlugins(FlutterProject project) { final List> list = >[]; for (final Plugin plugin in platformPlugins) { list.add({ - 'name': plugin.name, - 'path': globals.fsUtils.escapePath(plugin.path), - 'dependencies': [...plugin.dependencies.where(pluginNames.contains)], + _kFlutterPluginsNameKey: plugin.name, + _kFlutterPluginsPathKey: globals.fsUtils.escapePath(plugin.path), + _kFlutterPluginsDependenciesKey: [...plugin.dependencies.where(pluginNames.contains)], }); } return list; @@ -411,7 +417,7 @@ bool _writeFlutterPluginsList(FlutterProject project, List plugins) { final Map result = {}; result['info'] = 'This is a generated file; do not edit or check into version control.'; - result['plugins'] = pluginsMap; + result[_kFlutterPluginsPluginListKey] = pluginsMap; /// The dependencyGraph object is kept for backwards compatibility, but /// should be removed once migration is complete. /// https://github.com/flutter/flutter/issues/48918 @@ -889,6 +895,72 @@ Future _writeWebPluginRegistrant(FlutterProject project, List plug } } +/// For each platform that uses them, creates symlinks within the platform +/// directory to each plugin used on that platform. +/// +/// If |force| is true, the symlinks will be recreated, otherwise they will +/// be created only if missing. +/// +/// This uses [project.flutterPluginsDependenciesFile], so it should only be +/// run after refreshPluginList has been run since the last plugin change. +void createPluginSymlinks(FlutterProject project, {bool force = false}) { + Map platformPlugins; + final String pluginFileContent = _readFileContent(project.flutterPluginsDependenciesFile); + if (pluginFileContent != null) { + final Map pluginInfo = json.decode(pluginFileContent) as Map; + platformPlugins = pluginInfo[_kFlutterPluginsPluginListKey] as Map; + } + platformPlugins ??= {}; + + if (featureFlags.isWindowsEnabled && project.windows.existsSync()) { + _createPlatformPluginSymlinks( + project.windows.pluginSymlinkDirectory, + platformPlugins[project.windows.pluginConfigKey] as List, + force: force, + ); + } + if (featureFlags.isLinuxEnabled && project.linux.existsSync()) { + _createPlatformPluginSymlinks( + project.linux.pluginSymlinkDirectory, + platformPlugins[project.linux.pluginConfigKey] as List, + force: force, + ); + } +} + +/// Creates [symlinkDirectory] containing symlinks to each plugin listed in [platformPlugins]. +/// +/// If [force] is true, the directory will be created only if missing. +void _createPlatformPluginSymlinks(Directory symlinkDirectory, List platformPlugins, {bool force = false}) { + if (force && symlinkDirectory.existsSync()) { + // Start fresh to avoid stale links. + symlinkDirectory.deleteSync(recursive: true); + } + symlinkDirectory.createSync(recursive: true); + if (platformPlugins == null) { + return; + } + for (final Map pluginInfo in platformPlugins.cast>()) { + final String name = pluginInfo[_kFlutterPluginsNameKey] as String; + final String path = pluginInfo[_kFlutterPluginsPathKey] as String; + final Link link = symlinkDirectory.childLink(name); + if (link.existsSync()) { + continue; + } + try { + link.createSync(path); + } on FileSystemException catch (e) { + if (globals.platform.isWindows && (e.osError?.errorCode ?? 0) == 1314) { + throwToolExit( + 'Building with plugins requires symlink support. ' + 'Please enable Developer Mode in your system settings.\n\n$e' + ); + } + rethrow; + } + } +} + /// Rewrites the `.flutter-plugins` file of [project] based on the plugin /// dependencies declared in `pubspec.yaml`. /// @@ -905,6 +977,7 @@ void refreshPluginsList(FlutterProject project, {bool checkProjects = false}) { final bool changed = _writeFlutterPluginsList(project, plugins); if (changed || legacyChanged) { + createPluginSymlinks(project, force: true); if (!checkProjects || project.ios.existsSync()) { cocoaPods.invalidatePodInstallOutput(project.ios); } diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index d227d8ed96..6570e91073 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -963,6 +963,9 @@ class WindowsProject extends FlutterProjectPlatform { /// Ideally this will be replaced in the future with inspection of the project. File get nameFile => ephemeralDirectory.childFile('exe_filename'); + /// The directory to write plugin symlinks. + Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks'); + Future ensureReadyForPlatformSpecificTooling() async {} } @@ -997,6 +1000,9 @@ class LinuxProject extends FlutterProjectPlatform { /// the build. File get generatedMakeConfigFile => ephemeralDirectory.childFile('generated_config.mk'); + /// The directory to write plugin symlinks. + Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks'); + Future ensureReadyForPlatformSpecificTooling() async {} } diff --git a/packages/flutter_tools/lib/src/windows/build_windows.dart b/packages/flutter_tools/lib/src/windows/build_windows.dart index d4b3c50a2d..58d2f5ffcf 100644 --- a/packages/flutter_tools/lib/src/windows/build_windows.dart +++ b/packages/flutter_tools/lib/src/windows/build_windows.dart @@ -9,6 +9,7 @@ import '../base/process.dart'; import '../build_info.dart'; import '../cache.dart'; import '../globals.dart' as globals; +import '../plugins.dart'; import '../project.dart'; import '../reporting/reporting.dart'; import 'msbuild_utils.dart'; @@ -39,6 +40,7 @@ Future buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {S environment['LOCAL_ENGINE'] = globals.fs.path.basename(engineOutPath); } writePropertySheet(windowsProject.generatedPropertySheetFile, environment); + createPluginSymlinks(windowsProject.project); final String vcvarsScript = visualStudio.vcvarsPath; if (vcvarsScript == null) { diff --git a/packages/flutter_tools/test/general.shard/plugins_test.dart b/packages/flutter_tools/test/general.shard/plugins_test.dart index 88e7b97f3b..f3b669ed5f 100644 --- a/packages/flutter_tools/test/general.shard/plugins_test.dart +++ b/packages/flutter_tools/test/general.shard/plugins_test.dart @@ -73,10 +73,12 @@ void main() { windowsProject = MockWindowsProject(); when(flutterProject.windows).thenReturn(windowsProject); when(windowsProject.pluginConfigKey).thenReturn('windows'); + when(windowsProject.pluginSymlinkDirectory).thenReturn(flutterProject.directory.childDirectory('windows').childDirectory('symlinks')); when(windowsProject.existsSync()).thenReturn(false); linuxProject = MockLinuxProject(); when(flutterProject.linux).thenReturn(linuxProject); when(linuxProject.pluginConfigKey).thenReturn('linux'); + when(linuxProject.pluginSymlinkDirectory).thenReturn(flutterProject.directory.childDirectory('linux').childDirectory('symlinks')); when(linuxProject.existsSync()).thenReturn(false); when(mockClock.now()).thenAnswer( @@ -826,6 +828,138 @@ web_plugin_with_nested:${webPluginWithNestedFile.childDirectory('lib').uri.toStr FeatureFlags: () => featureFlags, }); }); + + group('createPluginSymlinks', () { + MockFeatureFlags featureFlags; + + setUp(() { + featureFlags = MockFeatureFlags(); + when(featureFlags.isLinuxEnabled).thenReturn(true); + when(featureFlags.isWindowsEnabled).thenReturn(true); + }); + + testUsingContext('Symlinks are created for Linux plugins', () { + when(linuxProject.existsSync()).thenReturn(true); + configureDummyPackageAsPlugin(); + // refreshPluginsList should call createPluginSymlinks. + refreshPluginsList(flutterProject); + + expect(linuxProject.pluginSymlinkDirectory.childLink('apackage').existsSync(), true); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); + + testUsingContext('Symlinks are created for Windows plugins', () { + when(windowsProject.existsSync()).thenReturn(true); + configureDummyPackageAsPlugin(); + // refreshPluginsList should call createPluginSymlinks. + refreshPluginsList(flutterProject); + + expect(windowsProject.pluginSymlinkDirectory.childLink('apackage').existsSync(), true); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); + + testUsingContext('Existing symlinks are removed when no longer in use with force', () { + when(linuxProject.existsSync()).thenReturn(true); + when(windowsProject.existsSync()).thenReturn(true); + + final List dummyFiles = [ + flutterProject.linux.pluginSymlinkDirectory.childFile('dummy'), + flutterProject.windows.pluginSymlinkDirectory.childFile('dummy'), + ]; + for (final File file in dummyFiles) { + file.createSync(recursive: true); + } + + createPluginSymlinks(flutterProject, force: true); + + for (final File file in dummyFiles) { + expect(file.existsSync(), false); + } + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); + + testUsingContext('Existing symlinks are removed automatically on refresh when no longer in use', () { + when(linuxProject.existsSync()).thenReturn(true); + when(windowsProject.existsSync()).thenReturn(true); + + final List dummyFiles = [ + flutterProject.linux.pluginSymlinkDirectory.childFile('dummy'), + flutterProject.windows.pluginSymlinkDirectory.childFile('dummy'), + ]; + for (final File file in dummyFiles) { + file.createSync(recursive: true); + } + + // refreshPluginsList should remove existing links and recreate on changes. + configureDummyPackageAsPlugin(); + refreshPluginsList(flutterProject); + + for (final File file in dummyFiles) { + expect(file.existsSync(), false); + } + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); + + testUsingContext('createPluginSymlinks is a no-op without force when up to date', () { + when(linuxProject.existsSync()).thenReturn(true); + when(windowsProject.existsSync()).thenReturn(true); + + final List dummyFiles = [ + flutterProject.linux.pluginSymlinkDirectory.childFile('dummy'), + flutterProject.windows.pluginSymlinkDirectory.childFile('dummy'), + ]; + for (final File file in dummyFiles) { + file.createSync(recursive: true); + } + + // Without force, this should do nothing to existing files. + createPluginSymlinks(flutterProject); + + for (final File file in dummyFiles) { + expect(file.existsSync(), true); + } + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); + + testUsingContext('createPluginSymlinks repairs missing links', () { + when(linuxProject.existsSync()).thenReturn(true); + when(windowsProject.existsSync()).thenReturn(true); + configureDummyPackageAsPlugin(); + refreshPluginsList(flutterProject); + + final List links = [ + linuxProject.pluginSymlinkDirectory.childLink('apackage'), + windowsProject.pluginSymlinkDirectory.childLink('apackage'), + ]; + for (final Link link in links) { + link.deleteSync(); + } + createPluginSymlinks(flutterProject); + + for (final Link link in links) { + expect(link.existsSync(), true); + } + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); + }); }); }