Skip to content

Commit feafe41

Browse files
authored
Exclude local Swift package schemes from --flavor error message (flutter#191098)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> When a developer mistypes a `--flavor` (e.g. `flutter build ios --flavor Prdo`), Flutter reports the available schemes so they can pick a valid one: ``` The Xcode project defines schemes: Runner, Amplitude-Swift, Foo-Dependency... You must specify a --flavor option to select one of the available schemes. ``` `XcodeProjectInfo` already filters two kinds of non-flavor schemes out of this list: Flutter's own generated SwiftPM packages, and *remote* Swift package checkouts (packages fetched by SwiftPM into the build directory's `checkouts` folder). Xcode auto-merges each of those packages' own `.xcscheme` files into the host project's scheme list, even though they aren't declared in the host `.xcodeproj` and aren't real Flutter flavors. What wasn't handled: **local** Swift packages — ones added directly to the Xcode project (an `XCLocalSwiftPackageReference` in `project.pbxproj`, e.g. living inside `ios/`) rather than fetched as a remote dependency. Those never appear under the `checkouts` directory, so their auto-merged schemes leaked straight through into `definesCustomSchemes`/`schemes`, and ended up quoted in the error message as if they were valid `--flavor` values. This PR parses `XCLocalSwiftPackageReference` entries out of `project.pbxproj`, resolves each entry's `relativePath` against the host app root, and — for any local package that ships its own `.swiftpm/xcode/xcshareddata/xcschemes/` directory — adds those scheme names to the same ignore set used for remote checkouts. The scheme-directory scan itself was already duplicated between the (new) local-package path and the existing remote-checkout path, so it's factored into one small shared helper. Fixes flutter#190098 ## 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]. <!-- 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 619aec9 commit feafe41

2 files changed

Lines changed: 146 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\s*=\s*XCLocalSwiftPackageReference\s*;[^}]*relativePath\s*=\s*(?:"([^"]*)"|([^;]+))\s*;',
39+
dotAll: true,
40+
);
41+
3042
/// Interpreter of Xcode projects.
3143
class XcodeProjectInterpreter {
3244
factory XcodeProjectInterpreter({
@@ -459,6 +471,7 @@ class XcodeProjectInterpreter {
459471
kFlutterGeneratedPluginSwiftPackageName,
460472
kFlutterGeneratedFrameworkSwiftPackageTargetName,
461473
..._swiftPackageCheckoutSchemes(buildDirectory),
474+
..._localSwiftPackageSchemes(xcodeProject),
462475
};
463476
try {
464477
for (final Plugin plugin in await xcodeProject.getPlugins()) {
@@ -471,6 +484,60 @@ class XcodeProjectInterpreter {
471484
return ignoredSchemes;
472485
}
473486

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

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,84 @@ Information about project "Runner":
968968
expect(fakeProcessManager, hasNoRemainingExpectations);
969969
});
970970

971+
testWithoutContext('getInfo filters local Swift package schemes from project schemes', () async {
972+
const workingDirectory = '/';
973+
final Directory buildDirectory = fileSystem.directory('build/ios');
974+
final xcodeProject = FakeXcodeBasedProject(workingDirectory, fileSystem);
975+
976+
// A local Swift package added directly to the Xcode project (not fetched
977+
// as a remote dependency, so it never appears under the SwiftPM
978+
// `checkouts` directory) but which ships its own scheme.
979+
xcodeProject.hostAppRoot
980+
.childDirectory('LocalPackages')
981+
.childDirectory('LocalDependency')
982+
.childDirectory('.swiftpm')
983+
.childDirectory('xcode')
984+
.childDirectory('xcshareddata')
985+
.childDirectory('xcschemes')
986+
.childFile('LocalDependency.xcscheme')
987+
.createSync(recursive: true);
988+
989+
xcodeProject.xcodeProjectInfoFile
990+
..createSync(recursive: true)
991+
..writeAsStringSync('''
992+
/* Begin XCLocalSwiftPackageReference section */
993+
1A2B3C4D /* XCLocalSwiftPackageReference "LocalPackages/LocalDependency" */ = {
994+
isa = XCLocalSwiftPackageReference;
995+
customName = MyLocalDependency;
996+
relativePath = LocalPackages/LocalDependency;
997+
};
998+
/* End XCLocalSwiftPackageReference section */
999+
''');
1000+
1001+
fakeProcessManager.addCommands(const <FakeCommand>[
1002+
kWhichSysctlCommand,
1003+
kx64CheckCommand,
1004+
kResolvePackagesCommand,
1005+
FakeCommand(
1006+
command: <String>[
1007+
'xcrun',
1008+
'xcodebuild',
1009+
'-clonedSourcePackagesDirPath',
1010+
'/build/ios/SourcePackages',
1011+
'-skipPackagePluginValidation',
1012+
'-skipPackageSignatureValidation',
1013+
'-list',
1014+
],
1015+
stdout: '''
1016+
Information about project "Runner":
1017+
Targets:
1018+
Runner
1019+
1020+
Build Configurations:
1021+
Debug
1022+
Release
1023+
1024+
Schemes:
1025+
Runner
1026+
LocalDependency
1027+
1028+
''',
1029+
),
1030+
]);
1031+
1032+
final xcodeProjectInterpreter = XcodeProjectInterpreter(
1033+
logger: logger,
1034+
fileSystem: fileSystem,
1035+
platform: platform,
1036+
processManager: fakeProcessManager,
1037+
analytics: const NoOpAnalytics(),
1038+
);
1039+
1040+
final XcodeProjectInfo? info = await xcodeProjectInterpreter.getInfo(
1041+
xcodeProject,
1042+
buildDirectory: buildDirectory,
1043+
);
1044+
1045+
expect(info!.schemes, <String>['Runner']);
1046+
expect(fakeProcessManager, hasNoRemainingExpectations);
1047+
});
1048+
9711049
testWithoutContext('expected scheme for non-flavored build is Runner', () {
9721050
expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.debug), 'Runner');
9731051
expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.profile), 'Runner');

0 commit comments

Comments
 (0)