Skip to content

Commit 115c53d

Browse files
authored
[stable] [flutter_tools] Handle missing Xcode gracefully in getInfo and buildMacOS (#191902)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: #191176 ### Impact Description: When running `flutter build macos` or `flutter build ios` on systems where Xcode is uninstalled, incomplete (e.g. Command Line Tools only), or misconfigured, `XcodeProjectInterpreter.getInfo` invokes `xcrun xcodebuild -list` which exits with code 72 (`unable to find utility "xcodebuild"`). Because code 72 was not in the allowed exit codes, `flutter_tools` crashes with an unhandled `ProcessException`. In Flutter 3.47.1 telemetry, this was a top crash with 320 crashes (15.2% share of top 10 crashes). This impacts development workflows on macOS. **Conflict Resolution Note**: Resolved a minor merge conflict in `packages/flutter_tools/lib/src/macos/build_macos.dart` by ordering the Xcode installation pre-flight check ahead of workspace validation. ### Changelog Description: [flutter/191176] When building for macOS or iOS on machines with missing or incomplete Xcode installations, flutter_tools displays an actionable error message instead of crashing with an unhandled ProcessException. ### Workaround: Install Xcode or configure the active developer directory via: `sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer` ### Risk: ### Test Coverage: ### Validation Steps: 1. On a macOS machine, switch `xcode-select` to Command Line Tools: `sudo xcode-select -s /Library/Developer/CommandLineTools`. 2. Run `flutter build macos` in a Flutter project. 3. Verify `flutter_tools` displays a clean `ToolExit` instructing the user to install or configure Xcode, rather than crashing with an unhandled `ProcessException` and stack trace. 4. Run automated test suite: `dart test test/general.shard/ios/xcodeproj_test.dart` and `dart test test/commands.shard/hermetic/build_macos_test.dart`.
1 parent ad4b29a commit 115c53d

5 files changed

Lines changed: 188 additions & 29 deletions

File tree

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -415,29 +415,33 @@ class XcodeProjectInterpreter {
415415
// The exit code returned by 'xcodebuild -list' when the project is corrupted.
416416
const corruptedProjectExitCode = 74;
417417
bool allowedFailures(int c) => c == missingProjectExitCode || c == corruptedProjectExitCode;
418-
final List<String> xcodebuildCommandArgs = await fetchDependenciesAndGenerateXcodebuildArgs(
419-
xcodeProject,
420-
buildDirectory,
421-
);
422-
final RunResult result = await _processUtils.run(
423-
<String>[
424-
...xcodebuildCommandArgs,
425-
'-list',
426-
if (projectFilename != null) ...<String>['-project', projectFilename],
427-
],
428-
throwOnError: true,
429-
allowedFailures: allowedFailures,
430-
workingDirectory: xcodeProject.hostAppRoot.path,
431-
);
432-
if (allowedFailures(result.exitCode)) {
433-
// User configuration error, tool exit instead of crashing.
434-
throwToolExit('Unable to get Xcode project information:\n ${result.stderr}');
418+
try {
419+
final List<String> xcodebuildCommandArgs = await fetchDependenciesAndGenerateXcodebuildArgs(
420+
xcodeProject,
421+
buildDirectory,
422+
);
423+
final RunResult result = await _processUtils.run(
424+
<String>[
425+
...xcodebuildCommandArgs,
426+
'-list',
427+
if (projectFilename != null) ...<String>['-project', projectFilename],
428+
],
429+
throwOnError: true,
430+
allowedFailures: allowedFailures,
431+
workingDirectory: xcodeProject.hostAppRoot.path,
432+
);
433+
if (allowedFailures(result.exitCode)) {
434+
// User configuration error, tool exit instead of crashing.
435+
throwToolExit('Unable to get Xcode project information:\n ${result.stderr}');
436+
}
437+
return XcodeProjectInfo.fromXcodeBuildOutput(
438+
result.toString(),
439+
_logger,
440+
ignoredSchemes: await _ignoredSwiftPackageSchemes(xcodeProject, buildDirectory),
441+
);
442+
} on ProcessException catch (exception) {
443+
throwToolExit('Unable to get Xcode project information:\n $exception');
435444
}
436-
return XcodeProjectInfo.fromXcodeBuildOutput(
437-
result.toString(),
438-
_logger,
439-
ignoredSchemes: await _ignoredSwiftPackageSchemes(xcodeProject, buildDirectory),
440-
);
441445
}
442446

443447
/// Returns scheme-name candidates for Swift packages that should be excluded from

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Future<void> buildMacOS({
8686
SizeAnalyzer? sizeAnalyzer,
8787
bool usingCISystem = false,
8888
}) async {
89+
if (globals.xcodeProjectInterpreter?.isInstalled != true) {
90+
throwToolExit(globals.userMessages.xcodeMissing);
91+
}
92+
8993
final Directory? xcodeWorkspace = flutterProject.macos.xcodeWorkspace;
9094
if (xcodeWorkspace == null) {
9195
throwToolExit(
@@ -151,11 +155,14 @@ Future<void> buildMacOS({
151155
projectFilename: xcodeProjectName,
152156
buildDirectory: flutterBuildDir,
153157
);
154-
final String? scheme = projectInfo?.schemeFor(buildInfo);
158+
if (projectInfo == null) {
159+
throwToolExit('Unable to get Xcode project information.');
160+
}
161+
final String? scheme = projectInfo.schemeFor(buildInfo);
155162
if (scheme == null) {
156-
projectInfo!.reportFlavorNotFoundAndExit();
163+
projectInfo.reportFlavorNotFoundAndExit();
157164
}
158-
final String? configuration = projectInfo?.buildConfigurationFor(buildInfo, scheme);
165+
final String? configuration = projectInfo.buildConfigurationFor(buildInfo, scheme);
159166
if (configuration == null) {
160167
throwToolExit('Unable to find expected configuration in Xcode project.');
161168
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,46 @@ STDERR STUFF
252252
},
253253
);
254254

255+
testUsingContext(
256+
'macOS build fails when Xcode is not installed',
257+
() async {
258+
final command = BuildCommand(
259+
androidSdk: FakeAndroidSdk(),
260+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
261+
fileSystem: fileSystem,
262+
logger: logger,
263+
osUtils: FakeOperatingSystemUtils(),
264+
config: FakeConfig(),
265+
platform: FakePlatform(),
266+
fileSystemUtils: FakeFileSystemUtils(),
267+
terminal: FakeTerminal(),
268+
plistParser: FakePlistParser(),
269+
processUtils: FakeProcessUtils(),
270+
processManager: FakeProcessManager.any(),
271+
templateRenderer: FakeTemplateRenderer(),
272+
xcode: FakeXcode(),
273+
artifacts: FakeArtifacts(),
274+
cache: FakeCache(),
275+
flutterVersion: FakeFlutterVersion(),
276+
);
277+
createMinimalMockProjectFiles();
278+
279+
expect(
280+
createTestCommandRunner(command).run(const <String>['build', 'macos', '--no-pub']),
281+
throwsToolExit(
282+
message: 'Xcode not installed; this is necessary for iOS and macOS development.',
283+
),
284+
);
285+
},
286+
overrides: <Type, Generator>{
287+
Platform: () => macosPlatform,
288+
FileSystem: () => fileSystem,
289+
ProcessManager: () => FakeProcessManager.any(),
290+
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
291+
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreter(isInstalled: false),
292+
},
293+
);
294+
255295
testUsingContext(
256296
'macOS build successfully with renamed .xcodeproj/.xcworkspace files',
257297
() async {

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,99 @@ void main() {
752752
},
753753
);
754754

755+
testWithoutContext(
756+
'xcodebuild -list getInfo throws a tool exit when xcodebuild is missing (exit code 72)',
757+
() async {
758+
const workingDirectory = '/';
759+
final Directory buildDirectory = fileSystem.directory('build/ios');
760+
const stderr =
761+
'xcrun: error: unable to find utility "xcodebuild", not a developer tool or in PATH';
762+
763+
fakeProcessManager.addCommands(const <FakeCommand>[
764+
kWhichSysctlCommand,
765+
kx64CheckCommand,
766+
kResolvePackagesCommand,
767+
FakeCommand(
768+
command: <String>[
769+
'xcrun',
770+
'xcodebuild',
771+
'-clonedSourcePackagesDirPath',
772+
'/build/ios/SourcePackages',
773+
'-skipPackagePluginValidation',
774+
'-skipPackageSignatureValidation',
775+
'-list',
776+
],
777+
exitCode: 72,
778+
stderr: stderr,
779+
),
780+
]);
781+
782+
final xcodeProjectInterpreter = XcodeProjectInterpreter(
783+
logger: logger,
784+
fileSystem: fileSystem,
785+
platform: platform,
786+
processManager: fakeProcessManager,
787+
analytics: const NoOpAnalytics(),
788+
);
789+
790+
await expectLater(
791+
() => xcodeProjectInterpreter.getInfo(
792+
FakeXcodeBasedProject(workingDirectory, fileSystem),
793+
buildDirectory: buildDirectory,
794+
),
795+
throwsToolExit(message: stderr),
796+
);
797+
expect(fakeProcessManager, hasNoRemainingExpectations);
798+
},
799+
);
800+
801+
testWithoutContext(
802+
'xcodebuild -list getInfo throws a tool exit when process execution throws ProcessException',
803+
() async {
804+
const workingDirectory = '/';
805+
final Directory buildDirectory = fileSystem.directory('build/ios');
806+
const errorMessage = 'xcrun: error: unable to find utility "xcodebuild"';
807+
const processException = ProcessException('xcrun', <String>['xcodebuild'], errorMessage, 72);
808+
809+
fakeProcessManager.addCommands(<FakeCommand>[
810+
kWhichSysctlCommand,
811+
kx64CheckCommand,
812+
kResolvePackagesCommand,
813+
FakeCommand(
814+
command: const <String>[
815+
'xcrun',
816+
'xcodebuild',
817+
'-clonedSourcePackagesDirPath',
818+
'/build/ios/SourcePackages',
819+
'-skipPackagePluginValidation',
820+
'-skipPackageSignatureValidation',
821+
'-list',
822+
],
823+
onRun: (_) {
824+
throw processException;
825+
},
826+
),
827+
]);
828+
829+
final xcodeProjectInterpreter = XcodeProjectInterpreter(
830+
logger: logger,
831+
fileSystem: fileSystem,
832+
platform: platform,
833+
processManager: fakeProcessManager,
834+
analytics: const NoOpAnalytics(),
835+
);
836+
837+
await expectLater(
838+
() => xcodeProjectInterpreter.getInfo(
839+
FakeXcodeBasedProject(workingDirectory, fileSystem),
840+
buildDirectory: buildDirectory,
841+
),
842+
throwsToolExit(message: processException.toString()),
843+
);
844+
expect(fakeProcessManager, hasNoRemainingExpectations);
845+
},
846+
);
847+
755848
testWithoutContext('Xcode project properties from default project can be parsed', () {
756849
const output = '''
757850
Information about project "Runner":

‎packages/flutter_tools/test/src/context.dart‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,32 @@ class NoopIOSSimulatorUtils implements IOSSimulatorUtils {
351351
}
352352

353353
class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
354+
FakeXcodeProjectInterpreter({
355+
bool isInstalled = true,
356+
String? versionText = 'Xcode 15',
357+
Version? version = const Version.withText(15, 0, 0, '15.0.0'),
358+
String? build = '15A240D',
359+
}) : _isInstalled = isInstalled,
360+
_versionText = versionText,
361+
_version = version,
362+
_build = build;
363+
364+
final bool _isInstalled;
365+
final String? _versionText;
366+
final Version? _version;
367+
final String? _build;
368+
354369
@override
355-
bool get isInstalled => true;
370+
bool get isInstalled => _isInstalled;
356371

357372
@override
358-
String get versionText => 'Xcode 15';
373+
String? get versionText => _versionText;
359374

360375
@override
361-
Version get version => Version(15, 0, 0);
376+
Version? get version => _version;
362377

363378
@override
364-
String get build => '15A240D';
379+
String? get build => _build;
365380

366381
@override
367382
Future<Map<String, String>> getBuildSettings(

0 commit comments

Comments
 (0)