From 951b25d2400a298f77a10e6a790905d045f62ddf Mon Sep 17 00:00:00 2001 From: Victoria Ashworth <15619084+vashworth@users.noreply.github.com> Date: Mon, 10 Nov 2025 14:46:51 -0600 Subject: [PATCH] Check for devicectl launch logs from std and file (#178167) For some reason in different editors, `devicectl` does not appear to stream its logs to `stdout`. Usually after calling `xcrun devicectl device process launch --console ...`, the `stdout` stream will receive a log of `'Waiting for the application to terminate'`. To workaround this, we use the `--log-ouput` flag with the `devicectl` command, which tells `devicectl` to write its logs to a file. We then periodically read the file looking for the `'Waiting for the application to terminate'` log. Fixes b/454953393. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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]. **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. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [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 --- .../lib/src/ios/core_devices.dart | 44 +++++++++++++-- .../general.shard/ios/core_devices_test.dart | 56 +++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/core_devices.dart b/packages/flutter_tools/lib/src/ios/core_devices.dart index 7607e8e6892..42d8b34e8af 100644 --- a/packages/flutter_tools/lib/src/ios/core_devices.dart +++ b/packages/flutter_tools/lib/src/ios/core_devices.dart @@ -335,6 +335,8 @@ class IOSCoreDeviceControl { 'Failed to execute code (error: EXC_BAD_ACCESS, debugger assist: not detected)', ]; + static const kCoreDeviceLaunchCompleteLog = 'Waiting for the application to terminate'; + /// Executes `devicectl` command to get list of devices. The command will /// likely complete before [timeout] is reached. If [timeout] is reached, /// the command will be stopped as a failure. @@ -650,13 +652,21 @@ class IOSCoreDeviceControl { /// /// If [attachToConsole] is true, attaches the application to the console and waits for the app /// to terminate. + /// + /// When [jsonOutputFile] is provided, devicectl will write a JSON file with the command results + /// after the command has completed. This will not have the results when using [attachToConsole] + /// until the process has exited. + /// + /// When [logOutputFile] is provided, devicectl will write all logging otherwise passed to + /// stdout/stderr to the file. It will also continue to stream the logs to stdout/stderr. List _launchAppCommand({ required String deviceId, required String bundleId, List launchArguments = const [], bool startStopped = false, bool attachToConsole = false, - File? outputFile, + File? jsonOutputFile, + File? logOutputFile, }) { return [ ..._xcode.xcrunCommand(), @@ -674,7 +684,8 @@ class IOSCoreDeviceControl { // See https://github.com/llvm/llvm-project/blob/19b43e1757b4fd3d0f188cf8a08e9febb0dbec2f/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp#L1227-L1233 '{"OS_ACTIVITY_DT_MODE": "enable"}', ], - if (outputFile != null) ...['--json-output', outputFile.path], + if (jsonOutputFile != null) ...['--json-output', jsonOutputFile.path], + if (logOutputFile != null) ...['--log-output', logOutputFile.path], bundleId, if (launchArguments.isNotEmpty) ...launchArguments, ]; @@ -703,7 +714,7 @@ class IOSCoreDeviceControl { deviceId: deviceId, launchArguments: launchArguments, startStopped: startStopped, - outputFile: output, + jsonOutputFile: output, ); try { @@ -754,6 +765,9 @@ class IOSCoreDeviceControl { return false; } + final Directory tempDirectory = _fileSystem.systemTempDirectory.createTempSync('core_devices.'); + final File output = tempDirectory.childFile('launch_log.txt')..createSync(); + final launchCompleter = Completer(); final List command = _launchAppCommand( bundleId: bundleId, @@ -761,8 +775,9 @@ class IOSCoreDeviceControl { launchArguments: launchArguments, startStopped: startStopped, attachToConsole: true, + logOutputFile: output, ); - + Timer? timer; try { final Process launchProcess = await _processUtils.start(command); coreDeviceLogForwarder.launchProcess = launchProcess; @@ -776,7 +791,7 @@ class IOSCoreDeviceControl { _logger.printTrace(line); } - if (line.contains('Waiting for the application to terminate')) { + if (!launchCompleter.isCompleted && line.contains(kCoreDeviceLaunchCompleteLog)) { launchCompleter.complete(true); } }); @@ -805,10 +820,27 @@ class IOSCoreDeviceControl { } }), ); - return launchCompleter.future; + + // Sometimes devicectl launch logs don't stream to stdout. + // As a workaround, we also use the log output file to check if it has finished launching. + timer = Timer.periodic(const Duration(seconds: 1), (timer) async { + if (await output.exists()) { + final String contents = await output.readAsString(); + if (!launchCompleter.isCompleted && contents.contains(kCoreDeviceLaunchCompleteLog)) { + launchCompleter.complete(true); + } + } + }); + + // Do not return the launchCompleter.future directly, otherwise, the timer will be canceled + // prematurely. + final bool status = await launchCompleter.future; + return status; } on ProcessException catch (err) { _logger.printTrace('Error executing devicectl: $err'); return false; + } finally { + timer?.cancel(); } } diff --git a/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart b/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart index 99525e9390c..78ff3c3c0e0 100644 --- a/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart @@ -1791,6 +1791,8 @@ invalid JSON '--console', '--environment-variables', '{"OS_ACTIVITY_DT_MODE": "enable"}', + '--log-output', + '/.tmp_rand0/core_devices.rand0/launch_log.txt', bundleId, ], stdout: ''' @@ -1830,6 +1832,8 @@ Waiting for the application to terminate... '--console', '--environment-variables', '{"OS_ACTIVITY_DT_MODE": "enable"}', + '--log-output', + '/.tmp_rand0/core_devices.rand0/launch_log.txt', bundleId, '--arg1', '--arg2', @@ -1872,6 +1876,8 @@ Waiting for the application to terminate... '--console', '--environment-variables', '{"OS_ACTIVITY_DT_MODE": "enable"}', + '--log-output', + '/.tmp_rand0/core_devices.rand0/launch_log.txt', bundleId, ], stdout: ''' @@ -1939,6 +1945,8 @@ Waiting for the application to terminate... '--console', '--environment-variables', '{"OS_ACTIVITY_DT_MODE": "enable"}', + '--log-output', + '/.tmp_rand0/core_devices.rand0/launch_log.txt', bundleId, ], exitCode: 1, @@ -1961,6 +1969,54 @@ ERROR: The operation couldn?t be completed. (OSStatus error -10814.) (NSOSStatus expect(logger.errorText, isEmpty); expect(result, isFalse); }); + + testWithoutContext('Successful launch with output in log file', () async { + final Completer launchCompleter = Completer(); + fakeProcessManager.addCommand( + FakeCommand( + command: const [ + 'xcrun', + 'devicectl', + 'device', + 'process', + 'launch', + '--device', + deviceId, + '--start-stopped', + '--console', + '--environment-variables', + '{"OS_ACTIVITY_DT_MODE": "enable"}', + '--log-output', + '/.tmp_rand0/core_devices.rand0/launch_log.txt', + bundleId, + ], + onRun: (command) { + fileSystem.file('/.tmp_rand0/core_devices.rand0/launch_log.txt') + ..createSync(recursive: true) + ..writeAsStringSync(''' +10:04:12 Acquired tunnel connection to device. +10:04:12 Enabling developer disk image services. +10:04:12 Acquired usage assertion. +Launched application with com.example.my_app bundle identifier. +Waiting for the application to terminate... +'''); + }, + completer: launchCompleter, + ), + ); + + final bool result = await deviceControl.launchAppAndStreamLogs( + deviceId: deviceId, + bundleId: bundleId, + coreDeviceLogForwarder: FakeIOSCoreDeviceLogForwarder(), + startStopped: true, + ); + launchCompleter.complete(); + + expect(fakeProcessManager, hasNoRemainingExpectations); + expect(logger.errorText, isEmpty); + expect(result, isTrue); + }); }); group('terminate app', () {