Skip to content

Commit f4f4ddb

Browse files
committed
Exclude local Swift package schemes from --flavor error message
XcodeProjectInfo already filters remote Swift package checkout schemes and Flutter plugin schemes out of the scheme list shown when a developer passes an invalid --flavor, but it did not account for local Swift packages referenced directly from the host project's project.pbxproj (XCLocalSwiftPackageReference). Xcode auto-merges those packages' own schemes into the project's scheme list the same way it does for remote checkouts, so they were leaking into the 'available schemes' error message as if they were valid flavors. This parses XCLocalSwiftPackageReference entries out of project.pbxproj, resolves each relativePath against the host app root, and adds any schemes found under that package's .swiftpm/xcode/xcshareddata/xcschemes directory to the ignore set — mirroring the existing logic for remote checkouts, which is now shared via a small helper. Fixes flutter#190098
1 parent 9e5378c commit f4f4ddb

2 files changed

Lines changed: 145 additions & 13 deletions

File tree

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

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ import '../xcode_project.dart';
2727
final _settingExpr = RegExp(r'(\w+)\s*=\s*(.*)$');
2828
final _varExpr = RegExp(r'\$\(([^)]*)\)');
2929

30+
/// Matches each `XCLocalSwiftPackageReference` object body in a
31+
/// `project.pbxproj` file, capturing its `relativePath` value.
32+
///
33+
/// Local Swift packages (added directly to the Xcode project rather than
34+
/// fetched as a remote dependency) are recorded this way; unlike remote
35+
/// checkouts, they never appear under the derived data `checkouts`
36+
/// directory, so they must be located by parsing the project file directly.
37+
final _localSwiftPackageReferenceExpr = RegExp(
38+
r'isa = XCLocalSwiftPackageReference;\s*relativePath = (?:"([^"]*)"|([^;]+));',
39+
dotAll: true,
40+
);
41+
3042
/// Interpreter of Xcode projects.
3143
class XcodeProjectInterpreter {
3244
factory XcodeProjectInterpreter({
@@ -457,6 +469,7 @@ class XcodeProjectInterpreter {
457469
kFlutterGeneratedPluginSwiftPackageName,
458470
kFlutterGeneratedFrameworkSwiftPackageTargetName,
459471
..._swiftPackageCheckoutSchemes(buildDirectory),
472+
..._localSwiftPackageSchemes(xcodeProject),
460473
};
461474
try {
462475
for (final Plugin plugin in await xcodeProject.getPlugins()) {
@@ -469,6 +482,60 @@ class XcodeProjectInterpreter {
469482
return ignoredSchemes;
470483
}
471484

485+
/// Returns scheme names contributed by local Swift package references
486+
/// declared directly in the host project's `project.pbxproj`.
487+
///
488+
/// Like remote checkouts (see [_swiftPackageCheckoutSchemes]), a local
489+
/// Swift package that ships its own `.swiftpm/xcode/xcshareddata/xcschemes/`
490+
/// directory has those schemes auto-merged into the host project's scheme
491+
/// list by Xcode, despite not being declared as a scheme in the host
492+
/// `.xcodeproj` itself.
493+
Set<String> _localSwiftPackageSchemes(XcodeBasedProject xcodeProject) {
494+
final File pbxprojFile = xcodeProject.xcodeProjectInfoFile;
495+
if (!pbxprojFile.existsSync()) {
496+
return const <String>{};
497+
}
498+
final schemes = <String>{};
499+
String contents;
500+
try {
501+
contents = pbxprojFile.readAsStringSync();
502+
} on FileSystemException catch (error) {
503+
_logger.printTrace(
504+
'Failed to read ${pbxprojFile.path} while filtering Xcode schemes: $error',
505+
);
506+
return const <String>{};
507+
}
508+
for (final RegExpMatch match in _localSwiftPackageReferenceExpr.allMatches(contents)) {
509+
final String? relativePath = (match.group(1) ?? match.group(2))?.trim();
510+
if (relativePath == null || relativePath.isEmpty) {
511+
continue;
512+
}
513+
final Directory packageDirectory = xcodeProject.hostAppRoot.childDirectory(relativePath);
514+
schemes.addAll(_schemesFromSwiftPackageSchemeDirectory(packageDirectory));
515+
}
516+
return schemes;
517+
}
518+
519+
/// Returns the names of any `.xcscheme` files found in [packageDirectory]'s
520+
/// `.swiftpm/xcode/xcshareddata/xcschemes` directory, if present.
521+
Set<String> _schemesFromSwiftPackageSchemeDirectory(Directory packageDirectory) {
522+
final Directory schemeDirectory = packageDirectory
523+
.childDirectory('.swiftpm')
524+
.childDirectory('xcode')
525+
.childDirectory('xcshareddata')
526+
.childDirectory('xcschemes');
527+
if (!schemeDirectory.existsSync()) {
528+
return const <String>{};
529+
}
530+
final schemes = <String>{};
531+
for (final File schemeFile in schemeDirectory.listSync().whereType<File>()) {
532+
if (_fileSystem.path.extension(schemeFile.path) == '.xcscheme') {
533+
schemes.add(_fileSystem.path.basenameWithoutExtension(schemeFile.path));
534+
}
535+
}
536+
return schemes;
537+
}
538+
472539
/// Returns scheme names contributed by direct and transitive Swift package checkouts.
473540
///
474541
/// When a Swift package ships its own `.swiftpm/xcode/xcshareddata/xcschemes/`
@@ -486,19 +553,7 @@ class XcodeProjectInterpreter {
486553
final schemes = <String>{};
487554
for (final Directory checkoutDirectory
488555
in checkoutsDirectory.listSync().whereType<Directory>()) {
489-
final Directory schemeDirectory = checkoutDirectory
490-
.childDirectory('.swiftpm')
491-
.childDirectory('xcode')
492-
.childDirectory('xcshareddata')
493-
.childDirectory('xcschemes');
494-
if (!schemeDirectory.existsSync()) {
495-
continue;
496-
}
497-
for (final File schemeFile in schemeDirectory.listSync().whereType<File>()) {
498-
if (_fileSystem.path.extension(schemeFile.path) == '.xcscheme') {
499-
schemes.add(_fileSystem.path.basenameWithoutExtension(schemeFile.path));
500-
}
501-
}
556+
schemes.addAll(_schemesFromSwiftPackageSchemeDirectory(checkoutDirectory));
502557
}
503558
return schemes;
504559
}

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,83 @@ Information about project "Runner":
875875
expect(fakeProcessManager, hasNoRemainingExpectations);
876876
});
877877

878+
testWithoutContext('getInfo filters local Swift package schemes from project schemes', () async {
879+
const workingDirectory = '/';
880+
final Directory buildDirectory = fileSystem.directory('build/ios');
881+
final FakeXcodeBasedProject xcodeProject = FakeXcodeBasedProject(workingDirectory, fileSystem);
882+
883+
// A local Swift package added directly to the Xcode project (not fetched
884+
// as a remote dependency, so it never appears under the SwiftPM
885+
// `checkouts` directory) but which ships its own scheme.
886+
xcodeProject.hostAppRoot
887+
.childDirectory('LocalPackages')
888+
.childDirectory('LocalDependency')
889+
.childDirectory('.swiftpm')
890+
.childDirectory('xcode')
891+
.childDirectory('xcshareddata')
892+
.childDirectory('xcschemes')
893+
.childFile('LocalDependency.xcscheme')
894+
.createSync(recursive: true);
895+
896+
xcodeProject.xcodeProjectInfoFile
897+
..createSync(recursive: true)
898+
..writeAsStringSync('''
899+
/* Begin XCLocalSwiftPackageReference section */
900+
1A2B3C4D /* XCLocalSwiftPackageReference "LocalPackages/LocalDependency" */ = {
901+
isa = XCLocalSwiftPackageReference;
902+
relativePath = LocalPackages/LocalDependency;
903+
};
904+
/* End XCLocalSwiftPackageReference section */
905+
''');
906+
907+
fakeProcessManager.addCommands(const <FakeCommand>[
908+
kWhichSysctlCommand,
909+
kx64CheckCommand,
910+
kResolvePackagesCommand,
911+
FakeCommand(
912+
command: <String>[
913+
'xcrun',
914+
'xcodebuild',
915+
'-clonedSourcePackagesDirPath',
916+
'/build/ios/SourcePackages',
917+
'-skipPackagePluginValidation',
918+
'-skipPackageSignatureValidation',
919+
'-list',
920+
],
921+
stdout: '''
922+
Information about project "Runner":
923+
Targets:
924+
Runner
925+
926+
Build Configurations:
927+
Debug
928+
Release
929+
930+
Schemes:
931+
Runner
932+
LocalDependency
933+
934+
''',
935+
),
936+
]);
937+
938+
final xcodeProjectInterpreter = XcodeProjectInterpreter(
939+
logger: logger,
940+
fileSystem: fileSystem,
941+
platform: platform,
942+
processManager: fakeProcessManager,
943+
analytics: const NoOpAnalytics(),
944+
);
945+
946+
final XcodeProjectInfo? info = await xcodeProjectInterpreter.getInfo(
947+
xcodeProject,
948+
buildDirectory: buildDirectory,
949+
);
950+
951+
expect(info!.schemes, <String>['Runner']);
952+
expect(fakeProcessManager, hasNoRemainingExpectations);
953+
});
954+
878955
testWithoutContext('expected scheme for non-flavored build is Runner', () {
879956
expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.debug), 'Runner');
880957
expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.profile), 'Runner');

0 commit comments

Comments
 (0)