Skip to content

Commit fbb2656

Browse files
authored
[flutter_tools] Handle missing Xcode gracefully in getInfo and buildMacOS (flutter#191180)
Fixes flutter#191176 ### Problem In Flutter 3.47.0 telemetry, an unhandled `ProcessException` when invoking `xcodebuild` emerged as a top crash signature. When running `flutter build macos` on a machine where Xcode is not installed, incomplete, or misconfigured, `flutter_tools` crashes with an unhandled `ProcessException` instead of providing a graceful `ToolExit` error message. ### Root Cause 1. `buildMacOS` in `packages/flutter_tools/lib/src/macos/build_macos.dart` lacked pre-flight toolchain validation (`globals.xcodeProjectInterpreter?.isInstalled != true`) before querying Xcode project information. 2. In `XcodeProjectInterpreter.getInfo` (`packages/flutter_tools/lib/src/ios/xcodeproj.dart`), `xcodebuild -list` is executed with `throwOnError: true` while only allowing exit codes 66 and 74. When `xcrun` returns exit code 72 (unable to find utility "xcodebuild"), `_processUtils.run` throws an uncaught `ProcessException`. 3. `buildMacOS` improperly used non-null assertions on `projectInfo!` when resolving build schemes. ### Fix - Added pre-flight Xcode toolchain check in `buildMacOS` to exit early with `globals.userMessages.xcodeMissing`. - Wrapped `XcodeProjectInterpreter.getInfo` in a `try ... on ProcessException` block to convert unexpected process failures to user-friendly `ToolExit` messages. - Added safe null checks on `projectInfo` in `buildMacOS`.
1 parent 0c0d059 commit fbb2656

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

442446
/// 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
@@ -96,6 +96,10 @@ Future<void> buildMacOS({
9696
);
9797
}
9898

99+
if (globals.xcodeProjectInterpreter?.isInstalled != true) {
100+
throwToolExit(globals.userMessages.xcodeMissing);
101+
}
102+
99103
// The .xcworkspace may not exist (e.g. a project using Swift Package Manager
100104
// without CocoaPods). When absent, xcodebuild builds the .xcodeproj directly.
101105
final Directory? xcodeWorkspace = flutterProject.macos.xcodeWorkspace;
@@ -155,11 +159,14 @@ Future<void> buildMacOS({
155159
projectFilename: xcodeProjectName,
156160
buildDirectory: flutterBuildDir,
157161
);
158-
final String? scheme = projectInfo?.schemeFor(buildInfo);
162+
if (projectInfo == null) {
163+
throwToolExit('Unable to get Xcode project information.');
164+
}
165+
final String? scheme = projectInfo.schemeFor(buildInfo);
159166
if (scheme == null) {
160-
projectInfo!.reportFlavorNotFoundAndExit();
167+
projectInfo.reportFlavorNotFoundAndExit();
161168
}
162-
final String? configuration = projectInfo?.buildConfigurationFor(buildInfo, scheme);
169+
final String? configuration = projectInfo.buildConfigurationFor(buildInfo, scheme);
163170
if (configuration == null) {
164171
throwToolExit('Unable to find expected configuration in Xcode project.');
165172
}

‎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
@@ -258,6 +258,46 @@ STDERR STUFF
258258
},
259259
);
260260

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

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