Skip to content

Commit c260683

Browse files
vashworthIvoneDjaja
authored andcommitted
Check for devicectl launch logs from std and file (flutter#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. <!-- Links --> [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
1 parent 5708264 commit c260683

2 files changed

Lines changed: 94 additions & 6 deletions

File tree

packages/flutter_tools/lib/src/ios/core_devices.dart

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ class IOSCoreDeviceControl {
335335
'Failed to execute code (error: EXC_BAD_ACCESS, debugger assist: not detected)',
336336
];
337337

338+
static const kCoreDeviceLaunchCompleteLog = 'Waiting for the application to terminate';
339+
338340
/// Executes `devicectl` command to get list of devices. The command will
339341
/// likely complete before [timeout] is reached. If [timeout] is reached,
340342
/// the command will be stopped as a failure.
@@ -650,13 +652,21 @@ class IOSCoreDeviceControl {
650652
///
651653
/// If [attachToConsole] is true, attaches the application to the console and waits for the app
652654
/// to terminate.
655+
///
656+
/// When [jsonOutputFile] is provided, devicectl will write a JSON file with the command results
657+
/// after the command has completed. This will not have the results when using [attachToConsole]
658+
/// until the process has exited.
659+
///
660+
/// When [logOutputFile] is provided, devicectl will write all logging otherwise passed to
661+
/// stdout/stderr to the file. It will also continue to stream the logs to stdout/stderr.
653662
List<String> _launchAppCommand({
654663
required String deviceId,
655664
required String bundleId,
656665
List<String> launchArguments = const <String>[],
657666
bool startStopped = false,
658667
bool attachToConsole = false,
659-
File? outputFile,
668+
File? jsonOutputFile,
669+
File? logOutputFile,
660670
}) {
661671
return <String>[
662672
..._xcode.xcrunCommand(),
@@ -674,7 +684,8 @@ class IOSCoreDeviceControl {
674684
// See https://github.com/llvm/llvm-project/blob/19b43e1757b4fd3d0f188cf8a08e9febb0dbec2f/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp#L1227-L1233
675685
'{"OS_ACTIVITY_DT_MODE": "enable"}',
676686
],
677-
if (outputFile != null) ...<String>['--json-output', outputFile.path],
687+
if (jsonOutputFile != null) ...<String>['--json-output', jsonOutputFile.path],
688+
if (logOutputFile != null) ...<String>['--log-output', logOutputFile.path],
678689
bundleId,
679690
if (launchArguments.isNotEmpty) ...launchArguments,
680691
];
@@ -703,7 +714,7 @@ class IOSCoreDeviceControl {
703714
deviceId: deviceId,
704715
launchArguments: launchArguments,
705716
startStopped: startStopped,
706-
outputFile: output,
717+
jsonOutputFile: output,
707718
);
708719

709720
try {
@@ -754,15 +765,19 @@ class IOSCoreDeviceControl {
754765
return false;
755766
}
756767

768+
final Directory tempDirectory = _fileSystem.systemTempDirectory.createTempSync('core_devices.');
769+
final File output = tempDirectory.childFile('launch_log.txt')..createSync();
770+
757771
final launchCompleter = Completer<bool>();
758772
final List<String> command = _launchAppCommand(
759773
bundleId: bundleId,
760774
deviceId: deviceId,
761775
launchArguments: launchArguments,
762776
startStopped: startStopped,
763777
attachToConsole: true,
778+
logOutputFile: output,
764779
);
765-
780+
Timer? timer;
766781
try {
767782
final Process launchProcess = await _processUtils.start(command);
768783
coreDeviceLogForwarder.launchProcess = launchProcess;
@@ -776,7 +791,7 @@ class IOSCoreDeviceControl {
776791
_logger.printTrace(line);
777792
}
778793

779-
if (line.contains('Waiting for the application to terminate')) {
794+
if (!launchCompleter.isCompleted && line.contains(kCoreDeviceLaunchCompleteLog)) {
780795
launchCompleter.complete(true);
781796
}
782797
});
@@ -805,10 +820,27 @@ class IOSCoreDeviceControl {
805820
}
806821
}),
807822
);
808-
return launchCompleter.future;
823+
824+
// Sometimes devicectl launch logs don't stream to stdout.
825+
// As a workaround, we also use the log output file to check if it has finished launching.
826+
timer = Timer.periodic(const Duration(seconds: 1), (timer) async {
827+
if (await output.exists()) {
828+
final String contents = await output.readAsString();
829+
if (!launchCompleter.isCompleted && contents.contains(kCoreDeviceLaunchCompleteLog)) {
830+
launchCompleter.complete(true);
831+
}
832+
}
833+
});
834+
835+
// Do not return the launchCompleter.future directly, otherwise, the timer will be canceled
836+
// prematurely.
837+
final bool status = await launchCompleter.future;
838+
return status;
809839
} on ProcessException catch (err) {
810840
_logger.printTrace('Error executing devicectl: $err');
811841
return false;
842+
} finally {
843+
timer?.cancel();
812844
}
813845
}
814846

packages/flutter_tools/test/general.shard/ios/core_devices_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,8 @@ invalid JSON
17911791
'--console',
17921792
'--environment-variables',
17931793
'{"OS_ACTIVITY_DT_MODE": "enable"}',
1794+
'--log-output',
1795+
'/.tmp_rand0/core_devices.rand0/launch_log.txt',
17941796
bundleId,
17951797
],
17961798
stdout: '''
@@ -1830,6 +1832,8 @@ Waiting for the application to terminate...
18301832
'--console',
18311833
'--environment-variables',
18321834
'{"OS_ACTIVITY_DT_MODE": "enable"}',
1835+
'--log-output',
1836+
'/.tmp_rand0/core_devices.rand0/launch_log.txt',
18331837
bundleId,
18341838
'--arg1',
18351839
'--arg2',
@@ -1872,6 +1876,8 @@ Waiting for the application to terminate...
18721876
'--console',
18731877
'--environment-variables',
18741878
'{"OS_ACTIVITY_DT_MODE": "enable"}',
1879+
'--log-output',
1880+
'/.tmp_rand0/core_devices.rand0/launch_log.txt',
18751881
bundleId,
18761882
],
18771883
stdout: '''
@@ -1939,6 +1945,8 @@ Waiting for the application to terminate...
19391945
'--console',
19401946
'--environment-variables',
19411947
'{"OS_ACTIVITY_DT_MODE": "enable"}',
1948+
'--log-output',
1949+
'/.tmp_rand0/core_devices.rand0/launch_log.txt',
19421950
bundleId,
19431951
],
19441952
exitCode: 1,
@@ -1961,6 +1969,54 @@ ERROR: The operation couldn?t be completed. (OSStatus error -10814.) (NSOSStatus
19611969
expect(logger.errorText, isEmpty);
19621970
expect(result, isFalse);
19631971
});
1972+
1973+
testWithoutContext('Successful launch with output in log file', () async {
1974+
final Completer<void> launchCompleter = Completer();
1975+
fakeProcessManager.addCommand(
1976+
FakeCommand(
1977+
command: const <String>[
1978+
'xcrun',
1979+
'devicectl',
1980+
'device',
1981+
'process',
1982+
'launch',
1983+
'--device',
1984+
deviceId,
1985+
'--start-stopped',
1986+
'--console',
1987+
'--environment-variables',
1988+
'{"OS_ACTIVITY_DT_MODE": "enable"}',
1989+
'--log-output',
1990+
'/.tmp_rand0/core_devices.rand0/launch_log.txt',
1991+
bundleId,
1992+
],
1993+
onRun: (command) {
1994+
fileSystem.file('/.tmp_rand0/core_devices.rand0/launch_log.txt')
1995+
..createSync(recursive: true)
1996+
..writeAsStringSync('''
1997+
10:04:12 Acquired tunnel connection to device.
1998+
10:04:12 Enabling developer disk image services.
1999+
10:04:12 Acquired usage assertion.
2000+
Launched application with com.example.my_app bundle identifier.
2001+
Waiting for the application to terminate...
2002+
''');
2003+
},
2004+
completer: launchCompleter,
2005+
),
2006+
);
2007+
2008+
final bool result = await deviceControl.launchAppAndStreamLogs(
2009+
deviceId: deviceId,
2010+
bundleId: bundleId,
2011+
coreDeviceLogForwarder: FakeIOSCoreDeviceLogForwarder(),
2012+
startStopped: true,
2013+
);
2014+
launchCompleter.complete();
2015+
2016+
expect(fakeProcessManager, hasNoRemainingExpectations);
2017+
expect(logger.errorText, isEmpty);
2018+
expect(result, isTrue);
2019+
});
19642020
});
19652021

19662022
group('terminate app', () {

0 commit comments

Comments
 (0)