Skip to content

Commit f3ece63

Browse files
authored
Allow building projects lacking Runner.xcworkspace (flutter#186239)
<!-- 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 --> Related: flutter#185924 I have ensured that Flutter commands do not throw errors even in projects without `Runner.xcworkspace`, such as those that have migrated to SwiftPM. Some migration processes checked for the existence of a workspace, but these do not read or write to the workspace directory and function correctly even if it is absent. While the exact intent is unclear, it is likely that they were checking whether Cocoapods was included in the project. Also, it was causing an error in `buildXcodeProject` by not passing scheme and BUILD_DIR as arguments when the workspace did not exist . ## 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. - [ ] 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 0900317 commit f3ece63

9 files changed

Lines changed: 337 additions & 92 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,17 @@ Future<XcodeBuildResult> buildXcodeProject({
411411

412412
final Directory? workspacePath = app.project.xcodeWorkspace;
413413
if (workspacePath != null) {
414-
buildCommands.addAll(<String>[
415-
'-workspace',
416-
workspacePath.basename,
417-
'-scheme',
418-
scheme,
419-
if (buildAction !=
420-
XcodeBuildAction.archive) // dSYM files aren't copied to the archive if BUILD_DIR is set.
421-
'BUILD_DIR=${globals.fs.path.absolute(buildDirectoryPath)}',
422-
]);
414+
buildCommands.addAll(<String>['-workspace', workspacePath.basename]);
415+
} else {
416+
buildCommands.addAll(<String>['-project', app.project.xcodeProject.basename]);
423417
}
418+
buildCommands.addAll(<String>[
419+
'-scheme',
420+
scheme,
421+
if (buildAction !=
422+
XcodeBuildAction.archive) // dSYM files aren't copied to the archive if BUILD_DIR is set.
423+
'BUILD_DIR=${globals.fs.path.absolute(buildDirectoryPath)}',
424+
]);
424425

425426
// Check if the project contains a watchOS companion app.
426427
final bool hasWatchCompanion = await app.project.containsWatchCompanion(

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,19 @@ Future<void> buildMacOS({
8686
SizeAnalyzer? sizeAnalyzer,
8787
bool usingCISystem = false,
8888
}) async {
89-
final Directory? xcodeWorkspace = flutterProject.macos.xcodeWorkspace;
90-
if (xcodeWorkspace == null) {
89+
final Directory xcodeProject = flutterProject.macos.xcodeProject;
90+
if (!xcodeProject.existsSync()) {
9191
throwToolExit(
9292
'No macOS desktop project configured. '
9393
'See https://flutter.dev/to/add-desktop-support '
9494
'to learn about adding macOS support to a project.',
9595
);
9696
}
97+
98+
// The .xcworkspace may not exist (e.g. a project using Swift Package Manager
99+
// without CocoaPods). When absent, xcodebuild builds the .xcodeproj directly.
100+
final Directory? xcodeWorkspace = flutterProject.macos.xcodeWorkspace;
101+
97102
const FlutterDarwinPlatform darwinPlatform = .macos;
98103
final migrators = <ProjectMigrator>[
99104
RemoveMacOSFrameworkLinkAndEmbeddingMigration(
@@ -140,8 +145,6 @@ Future<void> buildMacOS({
140145
flutterBuildDir.createSync(recursive: true);
141146
}
142147

143-
final Directory xcodeProject = flutterProject.macos.xcodeProject;
144-
145148
// If the standard project exists, specify it to getInfo to handle the case where there are
146149
// other Xcode projects in the macos/ directory. Otherwise pass no name, which will work
147150
// regardless of the project name so long as there is exactly one project.
@@ -280,8 +283,13 @@ Future<void> buildMacOS({
280283
<String>[
281284
'/usr/bin/env',
282285
...xcodebuildCommandArgs,
283-
'-workspace',
284-
xcodeWorkspace.path,
286+
if (xcodeWorkspace != null) ...<String>[
287+
'-workspace',
288+
xcodeWorkspace.path,
289+
] else ...<String>[
290+
'-project',
291+
xcodeProject.path,
292+
],
285293
'-configuration',
286294
configuration,
287295
'-scheme',

‎packages/flutter_tools/lib/src/migrations/lldb_init_migration.dart‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ class LLDBInitMigration extends ProjectMigrator {
7373
logger.printTrace('Unable to get Xcode project info.');
7474
throw _exceptionMessage();
7575
}
76-
if (_xcodeProject.xcodeWorkspace == null) {
77-
logger.printTrace('Xcode workspace not found.');
78-
throw _exceptionMessage();
79-
}
8076
final String? scheme = projectInfo.schemeFor(_buildInfo);
8177
if (scheme == null) {
8278
projectInfo.reportFlavorNotFoundAndExit();

‎packages/flutter_tools/lib/src/migrations/swift_package_manager_integration_migration.dart‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,6 @@ class SwiftPackageManagerIntegrationMigration extends ProjectMigrator {
273273
if (projectInfo == null) {
274274
throw Exception('Unable to get Xcode project info.');
275275
}
276-
if (_xcodeProject.xcodeWorkspace == null) {
277-
throw Exception('Xcode workspace not found.');
278-
}
279276
final String? scheme = projectInfo.schemeFor(_buildInfo);
280277
if (scheme == null) {
281278
projectInfo.reportFlavorNotFoundAndExit();

‎packages/flutter_tools/test/commands.shard/hermetic/build_ios_test.dart‎

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ void main() {
103103
}
104104

105105
// Sets up the minimal mock project files necessary for iOS builds to succeed.
106-
void createMinimalMockProjectFiles() {
106+
void createMinimalMockProjectFiles({bool createWorkspace = true}) {
107107
fileSystem
108108
.directory(fileSystem.path.join('ios', 'Runner.xcodeproj'))
109109
.createSync(recursive: true);
110-
fileSystem
111-
.directory(fileSystem.path.join('ios', 'Runner.xcworkspace'))
112-
.createSync(recursive: true);
110+
if (createWorkspace) {
111+
fileSystem
112+
.directory(fileSystem.path.join('ios', 'Runner.xcworkspace'))
113+
.createSync(recursive: true);
114+
}
113115
fileSystem
114116
.file(fileSystem.path.join('ios', 'Runner.xcodeproj', 'project.pbxproj'))
115117
.createSync();
@@ -167,6 +169,7 @@ void main() {
167169
bool verbose = false,
168170
bool simulator = false,
169171
bool customNaming = false,
172+
bool hasWorkspace = true,
170173
bool disablePortPublication = false,
171174
String? deviceId,
172175
int exitCode = 0,
@@ -182,8 +185,13 @@ void main() {
182185
if (verbose) 'VERBOSE_SCRIPT_LOGGING=YES' else '-quiet',
183186
'-allowProvisioningUpdates',
184187
'-allowProvisioningDeviceRegistration',
185-
'-workspace',
186-
if (customNaming) 'RenamedWorkspace.xcworkspace' else 'Runner.xcworkspace',
188+
if (hasWorkspace) ...<String>[
189+
'-workspace',
190+
if (customNaming) 'RenamedWorkspace.xcworkspace' else 'Runner.xcworkspace',
191+
] else ...<String>[
192+
'-project',
193+
if (customNaming) 'RenamedProj.xcodeproj' else 'Runner.xcodeproj',
194+
],
187195
'-scheme',
188196
'Runner',
189197
'BUILD_DIR=/build/ios',
@@ -641,6 +649,55 @@ void main() {
641649
},
642650
);
643651

652+
testUsingContext(
653+
'ios build invokes xcodebuild with -project when there is no .xcworkspace',
654+
() async {
655+
final command = BuildCommand(
656+
androidSdk: FakeAndroidSdk(),
657+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
658+
fileSystem: fileSystem,
659+
logger: logger,
660+
osUtils: FakeOperatingSystemUtils(),
661+
config: FakeConfig(),
662+
platform: FakePlatform(),
663+
fileSystemUtils: FakeFileSystemUtils(),
664+
terminal: FakeTerminal(),
665+
plistParser: FakePlistParser(),
666+
processUtils: FakeProcessUtils(),
667+
processManager: FakeProcessManager.any(),
668+
templateRenderer: FakeTemplateRenderer(),
669+
xcode: FakeXcode(),
670+
artifacts: FakeArtifacts(),
671+
cache: FakeCache(),
672+
flutterVersion: FakeFlutterVersion(),
673+
);
674+
createMinimalMockProjectFiles(createWorkspace: false);
675+
676+
processManager.addCommands(<FakeCommand>[
677+
setUpFakeXcodeBuildHandler(
678+
hasWorkspace: false,
679+
onRun: (_) {
680+
fileSystem
681+
.directory('build/ios/Release-iphoneos/Runner.app')
682+
.createSync(recursive: true);
683+
},
684+
),
685+
...postBuildCommands(),
686+
]);
687+
688+
await createTestCommandRunner(command).run(const <String>['build', 'ios', '--no-pub']);
689+
expect(testLogger.statusText, contains('build/ios/iphoneos/Runner.app'));
690+
},
691+
overrides: <Type, Generator>{
692+
FileSystem: () => fileSystem,
693+
ProcessManager: () => processManager,
694+
Pub: ThrowingPub.new,
695+
Platform: () => macosPlatform,
696+
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(),
697+
Artifacts: () => Artifacts.test(),
698+
},
699+
);
700+
644701
testUsingContext(
645702
'ios build invokes xcode build with device ID',
646703
() async {

‎packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart‎

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ void main() {
131131
}
132132

133133
// Sets up the minimal mock project files necessary for macOS builds to succeed.
134-
void createMinimalMockProjectFiles() {
134+
void createMinimalMockProjectFiles({bool createWorkspace = true}) {
135135
fileSystem
136136
.directory(fileSystem.path.join('macos', 'Runner.xcodeproj'))
137137
.createSync(recursive: true);
138-
fileSystem
139-
.directory(fileSystem.path.join('macos', 'Runner.xcworkspace'))
140-
.createSync(recursive: true);
138+
if (createWorkspace) {
139+
fileSystem
140+
.directory(fileSystem.path.join('macos', 'Runner.xcworkspace'))
141+
.createSync(recursive: true);
142+
}
141143
createCoreMockProjectFiles();
142144
}
143145

@@ -146,6 +148,7 @@ void main() {
146148
FakeCommand setUpFakeXcodeBuildHandler(
147149
String configuration, {
148150
bool verbose = false,
151+
bool hasWorkspace = true,
149152
void Function(List<String> command)? onRun,
150153
List<String>? additionalCommandArguments,
151154
String hostPlatformArch = 'x86_64',
@@ -160,8 +163,13 @@ void main() {
160163
'/usr/bin/env',
161164
'xcrun',
162165
'xcodebuild',
163-
'-workspace',
164-
flutterProject.macos.xcodeWorkspace!.path,
166+
if (hasWorkspace) ...<String>[
167+
'-workspace',
168+
flutterProject.macos.xcodeWorkspace!.path,
169+
] else ...<String>[
170+
'-project',
171+
flutterProject.macos.xcodeProject.path,
172+
],
165173
'-configuration',
166174
configuration,
167175
'-scheme',
@@ -305,6 +313,51 @@ STDERR STUFF
305313
},
306314
);
307315

316+
testUsingContext(
317+
'macOS build invokes xcodebuild with -project when there is no .xcworkspace',
318+
() async {
319+
final command = BuildCommand(
320+
androidSdk: FakeAndroidSdk(),
321+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
322+
fileSystem: fileSystem,
323+
logger: logger,
324+
osUtils: FakeOperatingSystemUtils(),
325+
config: FakeConfig(),
326+
platform: FakePlatform(),
327+
fileSystemUtils: FakeFileSystemUtils(),
328+
terminal: FakeTerminal(),
329+
plistParser: FakePlistParser(),
330+
processUtils: FakeProcessUtils(),
331+
processManager: FakeProcessManager.any(),
332+
templateRenderer: FakeTemplateRenderer(),
333+
xcode: FakeXcode(),
334+
artifacts: FakeArtifacts(),
335+
cache: FakeCache(),
336+
flutterVersion: FakeFlutterVersion(),
337+
);
338+
339+
createMinimalMockProjectFiles(createWorkspace: false);
340+
341+
fakeProcessManager.addCommands(<FakeCommand>[
342+
setUpFakeXcodeBuildHandler('Debug', hasWorkspace: false),
343+
]);
344+
345+
await createTestCommandRunner(
346+
command,
347+
).run(const <String>['build', 'macos', '--debug', '--no-pub']);
348+
349+
expect(fakeProcessManager, hasNoRemainingExpectations);
350+
},
351+
overrides: <Type, Generator>{
352+
FileSystem: () => fileSystem,
353+
ProcessManager: () => fakeProcessManager,
354+
Pub: ThrowingPub.new,
355+
Platform: () => macosPlatform,
356+
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
357+
OperatingSystemUtils: () => FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_x64),
358+
},
359+
);
360+
308361
testUsingContext(
309362
'macOS build fails on non-macOS platform',
310363
() async {

‎packages/flutter_tools/test/general.shard/migrations/lldb_init_migration_test.dart‎

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,6 @@ void main() {
7474
);
7575
});
7676

77-
testWithoutContext('fails if Xcode workspace not found', () async {
78-
final memoryFileSystem = MemoryFileSystem();
79-
final testLogger = BufferLogger.test();
80-
final project = FakeXcodeProject(
81-
platform: SupportedPlatform.ios.name,
82-
fileSystem: memoryFileSystem,
83-
logger: testLogger,
84-
);
85-
_createProjectFiles(project);
86-
project.xcodeWorkspace = null;
87-
88-
final migration = LLDBInitMigration(
89-
project,
90-
BuildInfo.debug,
91-
testLogger,
92-
environmentType: EnvironmentType.physical,
93-
fileSystem: memoryFileSystem,
94-
);
95-
await migration.migrate();
96-
expect(testLogger.traceText, contains('Xcode workspace not found.'));
97-
expect(
98-
testLogger.errorText,
99-
contains(
100-
'Running Flutter in debug mode on new iOS versions requires a LLDB Init File, but the '
101-
'scheme does not have it set.',
102-
),
103-
);
104-
});
105-
10677
testWithoutContext('fails if scheme not found', () async {
10778
final memoryFileSystem = MemoryFileSystem();
10879
final testLogger = BufferLogger.test();
@@ -496,6 +467,36 @@ void main() {
496467
),
497468
);
498469
});
470+
471+
testWithoutContext('succeeds with no Runner.xcworkspace', () async {
472+
final memoryFileSystem = MemoryFileSystem();
473+
final testLogger = BufferLogger.test();
474+
final project = FakeXcodeProject(
475+
platform: SupportedPlatform.ios.name,
476+
fileSystem: memoryFileSystem,
477+
logger: testLogger,
478+
);
479+
_createProjectFiles(project);
480+
project.xcodeWorkspace = null;
481+
project.xcodeProjectSchemeFile().writeAsStringSync(_validScheme());
482+
483+
final migration = LLDBInitMigration(
484+
project,
485+
BuildInfo.debug,
486+
testLogger,
487+
environmentType: EnvironmentType.physical,
488+
fileSystem: memoryFileSystem,
489+
);
490+
await migration.migrate();
491+
expect(testLogger.errorText, isEmpty);
492+
expect(
493+
project.xcodeProjectSchemeFile().readAsStringSync(),
494+
_validScheme(
495+
lldbInitFile:
496+
'\n customLLDBInitFile = "\$(SRCROOT)/Flutter/ephemeral/flutter_lldbinit"',
497+
),
498+
);
499+
});
499500
});
500501
}
501502

0 commit comments

Comments
 (0)