Skip to content

Commit b9ecd16

Browse files
authored
Refactor SwiftPM symlink path logic (flutter#188647)
Xcode caches Swift package manifests and only re-processes them when the path or the contents of the manifest change. To ensure that the package manifest is re-processed when the Flutter plugin version changes, we use the basename as the symlink plugin directory name since the basename has the version number in it. However, the basename cannot be used in the following scenarios: - The basename does not match the plugin's name (see flutter#186881). - The plugin is a dependency of another plugin. Inter-plugin dependencies are defined using relative paths and therefore must use the plugin name and not basename. Example: ```swift .package(name: "firebase_core", path: "../firebase_core"), ``` This PR copies plugins where the basename can't be used into the build directory and inserts a comment into the Package.swift to force Xcode to re-process it. Fixes flutter#186881. Fixes flutter#188147. Fixes flutter#188646 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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 implement]. - [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] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [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://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 99399b2 commit b9ecd16

4 files changed

Lines changed: 235 additions & 95 deletions

File tree

‎packages/flutter_tools/lib/src/ios/xcodeproj.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import '../xcode_project.dart';
2626

2727
final _settingExpr = RegExp(r'(\w+)\s*=\s*(.*)$');
2828
final _varExpr = RegExp(r'\$\(([^)]*)\)');
29-
const kSwiftPackageCacheDirectoryName = 'SourcePackages';
3029

3130
/// Interpreter of Xcode projects.
3231
class XcodeProjectInterpreter {

‎packages/flutter_tools/lib/src/macos/swift_package_manager.dart‎

Lines changed: 96 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const kFlutterGeneratedPluginSwiftPackageName = 'FlutterGeneratedPluginSwiftPack
2525
/// a dependency on the Flutter/FlutterMacOS framework.
2626
const kFlutterGeneratedFrameworkSwiftPackageTargetName = 'FlutterFramework';
2727

28+
const kSwiftPackageCacheDirectoryName = 'SourcePackages';
29+
2830
const kDisableSwiftPMInstructions =
2931
'You can also disable Swift Package Manager for the project by following these instructions:\n'
3032
' https://docs.flutter.dev/packages-and-plugins/swift-package-manager/for-app-developers#how-to-turn-off-swift-package-manager\n'
@@ -166,6 +168,10 @@ class SwiftPackageManager {
166168
final targetDependencies = <SwiftPackageTargetDependency>[];
167169
final expectedBasenames = <String>{};
168170

171+
final Set<Plugin> pluginsRequiringPluginNameSymlink = _pluginsRequiringPluginNameSymlink(
172+
plugins: plugins,
173+
platform: platform,
174+
);
169175
for (final plugin in plugins) {
170176
final String? pluginSwiftPackageManifestPath = plugin.pluginSwiftPackageManifestPath(
171177
_fileSystem,
@@ -182,31 +188,31 @@ class SwiftPackageManager {
182188
continue;
183189
}
184190

185-
// Use the plugin basename as the symlink plugin directory name since the basename has the
186-
// version number in it. This will make the symlink name change when the plugin version
187-
// changes, which forces Xcode to re-process the package manifest.
188191
final String basename = _fileSystem.directory(plugin.path).basename;
189192

190-
// Check if the plugin has a dependency on another Flutter plugin.
191-
// If the plugin has a dependency on another plugin, copy the plugin to the SourcePackages
192-
// cache directory and update the manifest to use the versioned path.
193-
final String manifestContent = manifest.readAsStringSync();
194-
final List<({String original, String replacement})> pluginDependencies =
195-
_getPluginDependencies(manifestContent, plugins);
196-
if (pluginDependencies.isNotEmpty) {
193+
final Link pluginSymlink;
194+
final String linkName;
195+
if (pluginsRequiringPluginNameSymlink.contains(plugin)) {
196+
// When basename can't be used as the symlink path, copy the plugin and update the
197+
// manifest contents so Xcode will re-process the package manifest.
197198
packagePath = _copyPluginAndUpdateManifest(
198199
plugin: plugin,
199-
pluginDependencies: pluginDependencies,
200200
pathRelativeTo: pathRelativeTo,
201201
basename: basename,
202202
platform: platform,
203-
manifestContent: manifestContent,
203+
manifest: manifest,
204204
);
205+
linkName = plugin.name;
206+
} else {
207+
// Use the plugin basename as the symlink plugin directory name since the basename has the
208+
// version number in it. This will make the symlink name change when the plugin version
209+
// changes, which forces Xcode to re-process the package manifest.
210+
linkName = basename;
205211
}
212+
pluginSymlink = symlinkDirectory.childLink(linkName);
206213

207-
final Link pluginSymlink = symlinkDirectory.childLink(basename);
208214
_createPluginSymlink(pluginSymlink: pluginSymlink, packagePath: packagePath);
209-
expectedBasenames.add(basename);
215+
expectedBasenames.add(linkName);
210216

211217
final String packageRelativePath = _fileSystem.path.relative(
212218
pluginSymlink.path,
@@ -231,6 +237,56 @@ class SwiftPackageManager {
231237
return (packageDependencies, targetDependencies, expectedBasenames);
232238
}
233239

240+
/// Determine plugins that need to be symlinked by plugin name instead of basename.
241+
///
242+
/// Xcode caches Swift package manifests and only re-processes them when the path or the contents
243+
/// of the manifest change. To ensure that the package manifest is re-processed when the Flutter
244+
/// plugin version changes, we use the basename as the symlink plugin directory name since the
245+
/// basename has the version number in it. However, the basename cannot be used in the following
246+
/// scenarios:
247+
/// - The basename does not match the plugin's name (see https://github.com/flutter/flutter/issues/186881).
248+
/// - The plugin is a dependency of another plugin. Inter-plugin dependencies are defined
249+
/// using relative paths and therefore must use the plugin name and not basename. Example:
250+
/// ```swift
251+
/// .package(name: "firebase_core", path: "../firebase_core"),
252+
/// ```
253+
Set<Plugin> _pluginsRequiringPluginNameSymlink({
254+
required List<Plugin> plugins,
255+
required FlutterDarwinPlatform platform,
256+
}) {
257+
final Set<Plugin> pluginsToCopy = {};
258+
for (final plugin in plugins) {
259+
if (!plugin.supportSwiftPackageManagerForPlatform(_fileSystem, platform.name)) {
260+
continue;
261+
}
262+
263+
// The basename cannot be used when it does not match the plugin's name
264+
// (see https://github.com/flutter/flutter/issues/186881).
265+
final String basename = _fileSystem.directory(plugin.path).basename;
266+
if (basename != plugin.name && !basename.startsWith('${plugin.name}-')) {
267+
pluginsToCopy.add(plugin);
268+
}
269+
270+
// Plugins that are depended on by other plugins cannot use the basename
271+
// since their dependency paths are relative using the name of the plugin.
272+
final String? manifestPath = plugin.pluginSwiftPackageManifestPath(
273+
_fileSystem,
274+
platform.name,
275+
);
276+
if (manifestPath == null) {
277+
continue;
278+
}
279+
final File manifest = _fileSystem.file(manifestPath);
280+
final String manifestContent = manifest.readAsStringSync();
281+
final List<Plugin> pluginDependencies = _getPluginDependencies(manifestContent, plugins);
282+
if (pluginDependencies.isNotEmpty) {
283+
pluginsToCopy.addAll(pluginDependencies);
284+
}
285+
}
286+
287+
return pluginsToCopy;
288+
}
289+
234290
/// Safely creates a symlink at [pluginSymlink] pointing to [packagePath].
235291
///
236292
/// If a symlink already exists and points to the correct target, creation is skipped
@@ -282,35 +338,19 @@ class SwiftPackageManager {
282338
}
283339
}
284340

285-
/// Checks if the plugin has a dependency on another Flutter plugin and returns a list of paths
286-
/// that should be replaced in the [manifestContent].
341+
/// Returns a list of [Plugin]s that the given plugin's [manifestContent] declares a dependency on.
287342
///
288-
/// Plugins can declare a SwiftPM dependency on another plugin like this:
343+
/// Plugins can declare a SwiftPM dependency on another plugin in their Package.swift like this:
289344
/// ```swift
290345
/// dependencies: [
291346
/// .package(name: "plugin_1", path: "../plugin_1")
292347
/// ]
293348
/// ```
294349
///
295-
/// However, plugins are symlinked in the [XcodeBasedProject.relativeSwiftPackagesDirectory]
296-
/// using the plugin's basename as the symlink name. The basename for non-path dependencies
297-
/// includes the version number, e.g. "plugin_1-1.0.0". To make the relative path in the
298-
/// manifest match the symlink path, we need to replace the path in the manifest with the
299-
/// symlink path.
300-
///
301-
/// For example, the manifest would need to updated to:
302-
/// ```swift
303-
/// dependencies: [
304-
/// .package(name: "plugin_1", path: "../plugin_1-1.0.0")
305-
/// ]
306-
/// ```
307-
List<({String original, String replacement})> _getPluginDependencies(
308-
String manifestContent,
309-
List<Plugin> plugins,
310-
) {
350+
List<Plugin> _getPluginDependencies(String manifestContent, List<Plugin> plugins) {
311351
final dependencyPattern = RegExp(r'"\.\.\/([^"]+)"');
312352
final Iterable<Match> matches = dependencyPattern.allMatches(manifestContent);
313-
final List<({String original, String replacement})> pluginDependencies = [];
353+
final List<Plugin> pluginDependencies = [];
314354
if (matches.isNotEmpty) {
315355
for (final match in matches) {
316356
final String? path = match.group(0);
@@ -322,47 +362,46 @@ class SwiftPackageManager {
322362
if (pluginDependency == null) {
323363
continue;
324364
}
325-
final newPath = '"../${_fileSystem.directory(pluginDependency.path).basename}"';
326-
if (path == newPath) {
327-
continue;
328-
}
329-
pluginDependencies.add((original: path, replacement: newPath));
365+
pluginDependencies.add(pluginDependency);
330366
}
331367
}
332368
return pluginDependencies;
333369
}
334370

335-
/// Copy the [plugin] to the build directory and update the manifest to use the versioned path.
336-
/// Returns the path to the copied plugin.
371+
/// Copy the [plugin] to the build directory and update the manifest with a comment containing
372+
/// the version. Returns the path to the copied plugin.
337373
///
338374
/// Plugin must be copied first so that the original plugin in pub cache is not modified.
339375
///
340376
/// Throws a [ToolExit] if the plugin cannot be copied or the manifest cannot be updated.
341377
String _copyPluginAndUpdateManifest({
342378
required Plugin plugin,
343-
required List<({String original, String replacement})> pluginDependencies,
344379
required String pathRelativeTo,
345380
required String basename,
346381
required FlutterDarwinPlatform platform,
347-
required String manifestContent,
382+
required File manifest,
348383
}) {
349-
final String destination = _fileSystem
350-
.directory(
351-
_fileSystem.path.join(
352-
platform.buildDirectory(config: _config, fileSystem: _fileSystem),
353-
'SourcePackages',
354-
basename,
355-
),
356-
)
357-
.absolute
358-
.path;
384+
final String manifestContent = manifest.readAsStringSync();
385+
final Directory destination = _fileSystem.directory(
386+
_fileSystem.path.join(
387+
platform.buildDirectory(config: _config, fileSystem: _fileSystem),
388+
kSwiftPackageCacheDirectoryName,
389+
basename,
390+
),
391+
);
392+
destination.createSync(recursive: true);
393+
final String sourcePath = plugin.path.endsWith('/') ? plugin.path : '${plugin.path}/';
394+
final String destinationPath = destination.absolute.path;
359395
final RunResult result = _processUtils.runSync([
360396
'rsync',
361397
'-8', // Avoid mangling filenames with encodings that do not match the current locale.
362398
'-av', // Archive mode and verbose: preserve permissions, ownership, timestamps, etc.
363399
'--delete', // Delete files in the destination that are not in the source.
364-
plugin.path,
365-
destination,
400+
// Don't copy the example directory as this can cause rsync to fail or be slow due to
401+
// copying build directories and symlinks
402+
'--exclude=/example/',
403+
sourcePath,
404+
destinationPath,
366405
]);
367406
if (result.exitCode != 0) {
368407
throwToolExit('Failed to copy plugin ${plugin.name}: \n${result.stdout}\n${result.stderr}');
@@ -371,7 +410,7 @@ class SwiftPackageManager {
371410
final String? packagePath = plugin.pluginSwiftPackagePath(
372411
_fileSystem,
373412
platform.name,
374-
overridePath: destination,
413+
overridePath: destinationPath,
375414
);
376415
if (packagePath == null) {
377416
throwToolExit('Failed to find path to Package.swift for plugin ${plugin.name}');
@@ -383,13 +422,7 @@ class SwiftPackageManager {
383422
'rsync stdout: \n${result.stdout}\nrsync stderr: \n${result.stderr}',
384423
);
385424
}
386-
var newManifestContent = manifestContent;
387-
for (final dependency in pluginDependencies) {
388-
newManifestContent = newManifestContent.replaceAll(
389-
dependency.original,
390-
dependency.replacement,
391-
);
392-
}
425+
final newManifestContent = '$manifestContent\n\n// Basename of original plugin: $basename';
393426
copiedManifest.writeAsStringSync(newManifestContent);
394427
return packagePath;
395428
}

‎packages/flutter_tools/test/general.shard/ios/xcodeproj_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:flutter_tools/src/base/version.dart';
1515
import 'package:flutter_tools/src/build_info.dart';
1616
import 'package:flutter_tools/src/ios/xcode_build_settings.dart';
1717
import 'package:flutter_tools/src/ios/xcodeproj.dart';
18+
import 'package:flutter_tools/src/macos/swift_package_manager.dart';
1819
import 'package:flutter_tools/src/macos/xcode.dart';
1920
import 'package:flutter_tools/src/plugins.dart';
2021
import 'package:flutter_tools/src/project.dart';

0 commit comments

Comments
 (0)