Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jonah Williams | 74bd7b6 | 2021-01-27 15:17:53 -0800 | [diff] [blame] | 5 | // @dart = 2.8 |
| 6 | |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 7 | import 'dart:async'; |
| 8 | |
| 9 | import 'package:args/command_runner.dart'; |
Devon Carew | 1c6078c | 2018-05-24 13:42:46 -0700 | [diff] [blame] | 10 | import 'package:intl/intl.dart' as intl; |
| 11 | import 'package:intl/intl_standalone.dart' as intl_standalone; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 12 | |
Jonah Williams | b1e9918 | 2021-01-13 14:14:07 -0800 | [diff] [blame] | 13 | import 'src/base/async_guard.dart'; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 14 | import 'src/base/common.dart'; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 15 | import 'src/base/context.dart'; |
| 16 | import 'src/base/file_system.dart'; |
| 17 | import 'src/base/io.dart'; |
| 18 | import 'src/base/logger.dart'; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 19 | import 'src/base/process.dart'; |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 20 | import 'src/context_runner.dart'; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 21 | import 'src/doctor.dart'; |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 22 | import 'src/globals.dart' as globals; |
Jenn Magder | e29595d | 2021-04-09 15:24:15 -0700 | [diff] [blame^] | 23 | import 'src/reporting/crash_reporting.dart'; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 24 | import 'src/runner/flutter_command.dart'; |
| 25 | import 'src/runner/flutter_command_runner.dart'; |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 26 | |
| 27 | /// Runs the Flutter tool with support for the specified list of [commands]. |
| 28 | Future<int> run( |
| 29 | List<String> args, |
Jonah Williams | bd3eee7 | 2020-09-03 11:55:55 -0700 | [diff] [blame] | 30 | List<FlutterCommand> Function() commands, { |
Jenn Magder | 8109dcc | 2020-04-20 15:23:09 -0700 | [diff] [blame] | 31 | bool muteCommandLogging = false, |
| 32 | bool verbose = false, |
| 33 | bool verboseHelp = false, |
| 34 | bool reportCrashes, |
| 35 | String flutterVersion, |
| 36 | Map<Type, Generator> overrides, |
| 37 | }) async { |
jcollins-g | 26102c9 | 2018-02-01 10:27:55 -0800 | [diff] [blame] | 38 | if (muteCommandLogging) { |
| 39 | // Remove the verbose option; for help and doctor, users don't need to see |
| 40 | // verbose logs. |
James D. Lin | 566c1d1 | 2020-04-21 22:09:50 -0700 | [diff] [blame] | 41 | args = List<String>.of(args); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 42 | args.removeWhere((String option) => option == '-v' || option == '--verbose'); |
| 43 | } |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 44 | |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 45 | return runInContext<int>(() async { |
Jonah Williams | 2133343 | 2020-03-10 11:35:52 -0700 | [diff] [blame] | 46 | reportCrashes ??= !await globals.isRunningOnBot; |
Jonah Williams | f5de6aa | 2020-06-04 16:35:36 -0700 | [diff] [blame] | 47 | final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp); |
Jonah Williams | bd3eee7 | 2020-09-03 11:55:55 -0700 | [diff] [blame] | 48 | commands().forEach(runner.addCommand); |
Jonah Williams | 2133343 | 2020-03-10 11:35:52 -0700 | [diff] [blame] | 49 | |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 50 | // Initialize the system locale. |
Devon Carew | 1c6078c | 2018-05-24 13:42:46 -0700 | [diff] [blame] | 51 | final String systemLocale = await intl_standalone.findSystemLocale(); |
| 52 | intl.Intl.defaultLocale = intl.Intl.verifiedLocale( |
| 53 | systemLocale, intl.NumberFormat.localeExists, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 54 | onFailure: (String _) => 'en_US', |
Devon Carew | 1c6078c | 2018-05-24 13:42:46 -0700 | [diff] [blame] | 55 | ); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 56 | |
Jenn Magder | 7f71562 | 2020-01-27 17:38:01 -0800 | [diff] [blame] | 57 | String getVersion() => flutterVersion ?? globals.flutterVersion.getVersionString(redactUnknownBranches: true); |
Zachary Anderson | c7596da | 2019-07-31 13:51:19 -0700 | [diff] [blame] | 58 | Object firstError; |
| 59 | StackTrace firstStackTrace; |
Michael Goderbauer | cb867bb | 2021-03-05 18:38:15 -0800 | [diff] [blame] | 60 | return runZoned<Future<int>>(() async { |
Alexander Aprelev | d917978 | 2019-06-10 09:52:22 -0700 | [diff] [blame] | 61 | try { |
| 62 | await runner.run(args); |
James D. Lin | c21b323 | 2020-06-15 09:35:04 -0700 | [diff] [blame] | 63 | |
| 64 | // Triggering [runZoned]'s error callback does not necessarily mean that |
| 65 | // we stopped executing the body. See https://github.com/dart-lang/sdk/issues/42150. |
| 66 | if (firstError == null) { |
| 67 | return await _exit(0); |
| 68 | } |
| 69 | |
| 70 | // We already hit some error, so don't return success. The error path |
| 71 | // (which should be in progress) is responsible for calling _exit(). |
| 72 | return 1; |
Zachary Anderson | 6c408a0 | 2020-03-06 10:22:12 -0800 | [diff] [blame] | 73 | // This catches all exceptions to send to crash logging, etc. |
| 74 | } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses |
Zachary Anderson | c7596da | 2019-07-31 13:51:19 -0700 | [diff] [blame] | 75 | firstError = error; |
| 76 | firstStackTrace = stackTrace; |
Michael Goderbauer | cb867bb | 2021-03-05 18:38:15 -0800 | [diff] [blame] | 77 | return _handleToolError( |
Alexander Aprelev | d917978 | 2019-06-10 09:52:22 -0700 | [diff] [blame] | 78 | error, stackTrace, verbose, args, reportCrashes, getVersion); |
| 79 | } |
Lasse R.H. Nielsen | af5194d | 2020-03-24 16:34:17 +0100 | [diff] [blame] | 80 | }, onError: (Object error, StackTrace stackTrace) async { // ignore: deprecated_member_use |
Zachary Anderson | c7596da | 2019-07-31 13:51:19 -0700 | [diff] [blame] | 81 | // If sending a crash report throws an error into the zone, we don't want |
| 82 | // to re-try sending the crash report with *that* error. Rather, we want |
| 83 | // to send the original error that triggered the crash report. |
James D. Lin | c21b323 | 2020-06-15 09:35:04 -0700 | [diff] [blame] | 84 | firstError ??= error; |
| 85 | firstStackTrace ??= stackTrace; |
| 86 | await _handleToolError(firstError, firstStackTrace, verbose, args, reportCrashes, getVersion); |
Alexander Aprelev | d917978 | 2019-06-10 09:52:22 -0700 | [diff] [blame] | 87 | }); |
Jonah Williams | 81c7af3 | 2018-11-06 14:36:35 -0800 | [diff] [blame] | 88 | }, overrides: overrides); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 91 | Future<int> _handleToolError( |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 92 | dynamic error, |
| 93 | StackTrace stackTrace, |
| 94 | bool verbose, |
| 95 | List<String> args, |
| 96 | bool reportCrashes, |
Michael Goderbauer | 7b251f5 | 2021-03-04 08:59:17 -0800 | [diff] [blame] | 97 | String Function() getFlutterVersion, |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 98 | ) async { |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 99 | if (error is UsageException) { |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 100 | globals.printError('${error.message}\n'); |
| 101 | globals.printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options."); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 102 | // Argument error exit code. |
| 103 | return _exit(64); |
| 104 | } else if (error is ToolExit) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 105 | if (error.message != null) { |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 106 | globals.printError(error.message); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 107 | } |
| 108 | if (verbose) { |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 109 | globals.printError('\n$stackTrace\n'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 110 | } |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 111 | return _exit(error.exitCode ?? 1); |
| 112 | } else if (error is ProcessExit) { |
| 113 | // We've caught an exit code. |
| 114 | if (error.immediate) { |
| 115 | exit(error.exitCode); |
| 116 | return error.exitCode; |
| 117 | } else { |
| 118 | return _exit(error.exitCode); |
| 119 | } |
| 120 | } else { |
| 121 | // We've crashed; emit a log report. |
Zachary Anderson | 68ed5c2 | 2020-01-28 07:58:02 -0800 | [diff] [blame] | 122 | globals.stdio.stderrWrite('\n'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 123 | |
| 124 | if (!reportCrashes) { |
| 125 | // Print the stack trace on the bots - don't write a crash report. |
Zachary Anderson | 68ed5c2 | 2020-01-28 07:58:02 -0800 | [diff] [blame] | 126 | globals.stdio.stderrWrite('$error\n'); |
| 127 | globals.stdio.stderrWrite('$stackTrace\n'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 128 | return _exit(1); |
Zachary Anderson | 36e8b93 | 2019-08-21 13:07:52 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Report to both [Usage] and [CrashReportSender]. |
Jenn Magder | f6a5512 | 2020-03-06 12:14:29 -0800 | [diff] [blame] | 132 | globals.flutterUsage.sendException(error); |
Jonah Williams | b1e9918 | 2021-01-13 14:14:07 -0800 | [diff] [blame] | 133 | await asyncGuard(() async { |
| 134 | final CrashReportSender crashReportSender = CrashReportSender( |
Jonah Williams | b1e9918 | 2021-01-13 14:14:07 -0800 | [diff] [blame] | 135 | usage: globals.flutterUsage, |
| 136 | platform: globals.platform, |
| 137 | logger: globals.logger, |
| 138 | operatingSystemUtils: globals.os, |
| 139 | ); |
| 140 | await crashReportSender.sendReport( |
| 141 | error: error, |
| 142 | stackTrace: stackTrace, |
| 143 | getFlutterVersion: getFlutterVersion, |
| 144 | command: args.join(' '), |
| 145 | ); |
| 146 | }, onError: (dynamic error) { |
| 147 | globals.printError('Error sending crash report: $error'); |
| 148 | }); |
Zachary Anderson | 36e8b93 | 2019-08-21 13:07:52 -0700 | [diff] [blame] | 149 | |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 150 | globals.printError('Oops; flutter has exited unexpectedly: "$error".'); |
Zachary Anderson | 36e8b93 | 2019-08-21 13:07:52 -0700 | [diff] [blame] | 151 | |
| 152 | try { |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 153 | final CrashDetails details = CrashDetails( |
| 154 | command: _crashCommand(args), |
| 155 | error: error, |
| 156 | stackTrace: stackTrace, |
| 157 | doctorText: await _doctorText(), |
| 158 | ); |
| 159 | final File file = await _createLocalCrashReport(details); |
| 160 | await globals.crashReporter.informUser(details, file); |
Jenn Magder | 7d8f820 | 2019-11-26 14:06:31 -0800 | [diff] [blame] | 161 | |
Zachary Anderson | 36e8b93 | 2019-08-21 13:07:52 -0700 | [diff] [blame] | 162 | return _exit(1); |
Zachary Anderson | 6c408a0 | 2020-03-06 10:22:12 -0800 | [diff] [blame] | 163 | // This catch catches all exceptions to ensure the message below is printed. |
| 164 | } catch (error) { // ignore: avoid_catches_without_on_clauses |
Zachary Anderson | 68ed5c2 | 2020-01-28 07:58:02 -0800 | [diff] [blame] | 165 | globals.stdio.stderrWrite( |
Zachary Anderson | 36e8b93 | 2019-08-21 13:07:52 -0700 | [diff] [blame] | 166 | 'Unable to generate crash report due to secondary error: $error\n' |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 167 | '${globals.userMessages.flutterToolBugInstructions}\n'); |
| 168 | // Any exception thrown here (including one thrown by `_exit()`) will |
Zachary Anderson | 36e8b93 | 2019-08-21 13:07:52 -0700 | [diff] [blame] | 169 | // get caught by our zone's `onError` handler. In order to avoid an |
| 170 | // infinite error loop, we throw an error that is recognized above |
| 171 | // and will trigger an immediate exit. |
| 172 | throw ProcessExit(1, immediate: true); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Jenn Magder | 7d8f820 | 2019-11-26 14:06:31 -0800 | [diff] [blame] | 177 | String _crashCommand(List<String> args) => 'flutter ${args.join(' ')}'; |
| 178 | |
| 179 | String _crashException(dynamic error) => '${error.runtimeType}: $error'; |
| 180 | |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 181 | /// Saves the crash report to a local file. |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 182 | Future<File> _createLocalCrashReport(CrashDetails details) async { |
Zachary Anderson | 2c51efe | 2020-01-31 12:51:20 -0800 | [diff] [blame] | 183 | File crashFile = globals.fsUtils.getUniqueFile( |
Jenn Magder | 8109dcc | 2020-04-20 15:23:09 -0700 | [diff] [blame] | 184 | globals.fs.currentDirectory, |
Zachary Anderson | 2c51efe | 2020-01-31 12:51:20 -0800 | [diff] [blame] | 185 | 'flutter', |
| 186 | 'log', |
| 187 | ); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 188 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 189 | final StringBuffer buffer = StringBuffer(); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 190 | |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 191 | buffer.writeln('Flutter crash report.'); |
| 192 | buffer.writeln('${globals.userMessages.flutterToolBugInstructions}\n'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 193 | |
| 194 | buffer.writeln('## command\n'); |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 195 | buffer.writeln('${details.command}\n'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 196 | |
| 197 | buffer.writeln('## exception\n'); |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 198 | buffer.writeln('${_crashException(details.error)}\n'); |
| 199 | buffer.writeln('```\n${details.stackTrace}```\n'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 200 | |
| 201 | buffer.writeln('## flutter doctor\n'); |
James D. Lin | b7fd24a | 2020-04-28 10:34:03 -0700 | [diff] [blame] | 202 | buffer.writeln('```\n${details.doctorText}```'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 203 | |
| 204 | try { |
Zachary Anderson | 398ac1f | 2019-08-20 13:15:08 -0700 | [diff] [blame] | 205 | crashFile.writeAsStringSync(buffer.toString()); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 206 | } on FileSystemException catch (_) { |
| 207 | // Fallback to the system temporary directory. |
Zachary Anderson | 2c51efe | 2020-01-31 12:51:20 -0800 | [diff] [blame] | 208 | crashFile = globals.fsUtils.getUniqueFile( |
Jenn Magder | 8109dcc | 2020-04-20 15:23:09 -0700 | [diff] [blame] | 209 | globals.fs.systemTempDirectory, |
Zachary Anderson | 2c51efe | 2020-01-31 12:51:20 -0800 | [diff] [blame] | 210 | 'flutter', |
| 211 | 'log', |
| 212 | ); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 213 | try { |
Zachary Anderson | 398ac1f | 2019-08-20 13:15:08 -0700 | [diff] [blame] | 214 | crashFile.writeAsStringSync(buffer.toString()); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 215 | } on FileSystemException catch (e) { |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 216 | globals.printError('Could not write crash report to disk: $e'); |
| 217 | globals.printError(buffer.toString()); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | return crashFile; |
| 222 | } |
| 223 | |
| 224 | Future<String> _doctorText() async { |
| 225 | try { |
Jonah Williams | f7b8d62 | 2020-01-08 12:35:12 -0800 | [diff] [blame] | 226 | final BufferLogger logger = BufferLogger( |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 227 | terminal: globals.terminal, |
Jenn Magder | e0ab6fc | 2020-03-18 15:54:26 -0700 | [diff] [blame] | 228 | outputPreferences: globals.outputPreferences, |
Jonah Williams | f7b8d62 | 2020-01-08 12:35:12 -0800 | [diff] [blame] | 229 | ); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 230 | |
Jenn Magder | 457972b | 2020-04-15 19:44:42 -0700 | [diff] [blame] | 231 | final Doctor doctor = Doctor(logger: logger); |
| 232 | await doctor.diagnose(verbose: true, showColor: false); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 233 | |
| 234 | return logger.statusText; |
Zachary Anderson | 6c408a0 | 2020-03-06 10:22:12 -0800 | [diff] [blame] | 235 | } on Exception catch (error, trace) { |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 236 | return 'encountered exception: $error\n\n${trace.toString().trim()}\n'; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | Future<int> _exit(int code) async { |
Zachary Anderson | 372fe29 | 2019-11-05 10:43:52 -0800 | [diff] [blame] | 241 | // Prints the welcome message if needed. |
Jenn Magder | f6a5512 | 2020-03-06 12:14:29 -0800 | [diff] [blame] | 242 | globals.flutterUsage.printWelcome(); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 243 | |
| 244 | // Send any last analytics calls that are in progress without overly delaying |
| 245 | // the tool's exit (we wait a maximum of 250ms). |
Jenn Magder | f6a5512 | 2020-03-06 12:14:29 -0800 | [diff] [blame] | 246 | if (globals.flutterUsage.enabled) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 247 | final Stopwatch stopwatch = Stopwatch()..start(); |
Jenn Magder | f6a5512 | 2020-03-06 12:14:29 -0800 | [diff] [blame] | 248 | await globals.flutterUsage.ensureAnalyticsSent(); |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 249 | globals.printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Run shutdown hooks before flushing logs |
Jonah Williams | cf903d7 | 2021-03-22 10:35:40 -0700 | [diff] [blame] | 253 | await globals.shutdownHooks.runShutdownHooks(); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 254 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 255 | final Completer<void> completer = Completer<void>(); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 256 | |
| 257 | // Give the task / timer queue one cycle through before we hard exit. |
| 258 | Timer.run(() { |
| 259 | try { |
Zachary Anderson | 23a3d10 | 2020-01-13 10:12:06 -0800 | [diff] [blame] | 260 | globals.printTrace('exiting with code $code'); |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 261 | exit(code); |
| 262 | completer.complete(); |
Jonah Williams | 08576cb | 2020-10-12 09:31:02 -0700 | [diff] [blame] | 263 | // This catches all exceptions because the error is propagated on the |
Zachary Anderson | 6c408a0 | 2020-03-06 10:22:12 -0800 | [diff] [blame] | 264 | // completer. |
| 265 | } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses |
Todd Volkert | 454db9d | 2017-11-09 21:45:31 -0800 | [diff] [blame] | 266 | completer.completeError(error, stackTrace); |
| 267 | } |
| 268 | }); |
| 269 | |
| 270 | await completer.future; |
| 271 | return code; |
| 272 | } |