blob: 02760126fd232c54719769348444da73f127a202 [file] [log] [blame]
Todd Volkert454db9d2017-11-09 21:45:31 -08001// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6
7import 'package:args/command_runner.dart';
Devon Carew1c6078c2018-05-24 13:42:46 -07008import 'package:intl/intl.dart' as intl;
9import 'package:intl/intl_standalone.dart' as intl_standalone;
Todd Volkert454db9d2017-11-09 21:45:31 -080010import 'package:meta/meta.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080011
Todd Volkert454db9d2017-11-09 21:45:31 -080012import 'src/base/common.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080013import 'src/base/context.dart';
14import 'src/base/file_system.dart';
15import 'src/base/io.dart';
16import 'src/base/logger.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080017import 'src/base/process.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080018import 'src/base/utils.dart';
Todd Volkert8d11f5c2018-03-28 10:58:28 -070019import 'src/context_runner.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080020import 'src/doctor.dart';
21import 'src/globals.dart';
Todd Volkertaa9a1152019-07-16 13:21:06 -070022import 'src/reporting/crash_reporting.dart';
23import 'src/reporting/usage.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080024import 'src/runner/flutter_command.dart';
25import 'src/runner/flutter_command_runner.dart';
Todd Volkert454db9d2017-11-09 21:45:31 -080026import 'src/version.dart';
27
28/// Runs the Flutter tool with support for the specified list of [commands].
29Future<int> run(
30 List<String> args,
31 List<FlutterCommand> commands, {
Alexandre Ardhuin09276be2018-06-05 08:50:40 +020032 bool muteCommandLogging = false,
33 bool verbose = false,
34 bool verboseHelp = false,
Todd Volkert454db9d2017-11-09 21:45:31 -080035 bool reportCrashes,
36 String flutterVersion,
Jonah Williams81c7af32018-11-06 14:36:35 -080037 Map<Type, Generator> overrides,
Todd Volkert8d11f5c2018-03-28 10:58:28 -070038}) {
Todd Volkert454db9d2017-11-09 21:45:31 -080039 reportCrashes ??= !isRunningOnBot;
40
jcollins-g26102c92018-02-01 10:27:55 -080041 if (muteCommandLogging) {
42 // Remove the verbose option; for help and doctor, users don't need to see
43 // verbose logs.
Alexandre Ardhuind927c932018-09-12 08:29:29 +020044 args = List<String>.from(args);
Todd Volkert454db9d2017-11-09 21:45:31 -080045 args.removeWhere((String option) => option == '-v' || option == '--verbose');
46 }
47
Alexandre Ardhuind927c932018-09-12 08:29:29 +020048 final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp);
Todd Volkert454db9d2017-11-09 21:45:31 -080049 commands.forEach(runner.addCommand);
50
Todd Volkert8d11f5c2018-03-28 10:58:28 -070051 return runInContext<int>(() async {
Todd Volkert454db9d2017-11-09 21:45:31 -080052 // Initialize the system locale.
Devon Carew1c6078c2018-05-24 13:42:46 -070053 final String systemLocale = await intl_standalone.findSystemLocale();
54 intl.Intl.defaultLocale = intl.Intl.verifiedLocale(
55 systemLocale, intl.NumberFormat.localeExists,
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010056 onFailure: (String _) => 'en_US',
Devon Carew1c6078c2018-05-24 13:42:46 -070057 );
Todd Volkert454db9d2017-11-09 21:45:31 -080058
Alexander Aprelevd9179782019-06-10 09:52:22 -070059 String getVersion() => flutterVersion ?? FlutterVersion.instance.getVersionString(redactUnknownBranches: true);
60 return await runZoned<Future<int>>(() async {
61 try {
62 await runner.run(args);
63 return await _exit(0);
64 } catch (error, stackTrace) {
65 return await _handleToolError(
66 error, stackTrace, verbose, args, reportCrashes, getVersion);
67 }
68 }, onError: (Object error, StackTrace stackTrace) async {
69 await _handleToolError(
70 error, stackTrace, verbose, args, reportCrashes, getVersion);
71 });
Jonah Williams81c7af32018-11-06 14:36:35 -080072 }, overrides: overrides);
Todd Volkert454db9d2017-11-09 21:45:31 -080073}
74
Todd Volkert454db9d2017-11-09 21:45:31 -080075Future<int> _handleToolError(
Alexandre Ardhuin5169ab52019-02-21 09:27:07 +010076 dynamic error,
77 StackTrace stackTrace,
78 bool verbose,
79 List<String> args,
80 bool reportCrashes,
81 String getFlutterVersion(),
82) async {
Todd Volkert454db9d2017-11-09 21:45:31 -080083 if (error is UsageException) {
Greg Spencer7caa6592018-09-19 15:22:43 -070084 printError('${error.message}\n');
85 printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.");
Todd Volkert454db9d2017-11-09 21:45:31 -080086 // Argument error exit code.
87 return _exit(64);
88 } else if (error is ToolExit) {
89 if (error.message != null)
Greg Spencer7caa6592018-09-19 15:22:43 -070090 printError(error.message);
91 if (verbose)
92 printError('\n$stackTrace\n');
Todd Volkert454db9d2017-11-09 21:45:31 -080093 return _exit(error.exitCode ?? 1);
94 } else if (error is ProcessExit) {
95 // We've caught an exit code.
96 if (error.immediate) {
97 exit(error.exitCode);
98 return error.exitCode;
99 } else {
100 return _exit(error.exitCode);
101 }
102 } else {
103 // We've crashed; emit a log report.
Todd Volkert2c898f62018-03-29 05:55:32 -0700104 stderr.writeln();
Todd Volkert454db9d2017-11-09 21:45:31 -0800105
106 if (!reportCrashes) {
107 // Print the stack trace on the bots - don't write a crash report.
Todd Volkert2c898f62018-03-29 05:55:32 -0700108 stderr.writeln('$error');
109 stderr.writeln(stackTrace.toString());
Todd Volkert454db9d2017-11-09 21:45:31 -0800110 return _exit(1);
111 } else {
Todd Volkertaa9a1152019-07-16 13:21:06 -0700112 // Report to both [Usage] and [CrashReportSender].
113 flutterUsage.sendException(error);
114 await CrashReportSender.instance.sendReport(
115 error: error,
116 stackTrace: stackTrace,
117 getFlutterVersion: getFlutterVersion,
118 );
Todd Volkert454db9d2017-11-09 21:45:31 -0800119
120 if (error is String)
Todd Volkert2c898f62018-03-29 05:55:32 -0700121 stderr.writeln('Oops; flutter has exited unexpectedly: "$error".');
Todd Volkert454db9d2017-11-09 21:45:31 -0800122 else
Todd Volkert2c898f62018-03-29 05:55:32 -0700123 stderr.writeln('Oops; flutter has exited unexpectedly.');
Todd Volkert454db9d2017-11-09 21:45:31 -0800124
Todd Volkert454db9d2017-11-09 21:45:31 -0800125 try {
126 final File file = await _createLocalCrashReport(args, error, stackTrace);
Todd Volkert2c898f62018-03-29 05:55:32 -0700127 stderr.writeln(
Todd Volkert454db9d2017-11-09 21:45:31 -0800128 'Crash report written to ${file.path};\n'
129 'please let us know at https://github.com/flutter/flutter/issues.',
130 );
131 return _exit(1);
132 } catch (error) {
Todd Volkert2c898f62018-03-29 05:55:32 -0700133 stderr.writeln(
Todd Volkert454db9d2017-11-09 21:45:31 -0800134 'Unable to generate crash report due to secondary error: $error\n'
135 'please let us know at https://github.com/flutter/flutter/issues.',
136 );
137 // Any exception throw here (including one thrown by `_exit()`) will
138 // get caught by our zone's `onError` handler. In order to avoid an
139 // infinite error loop, we throw an error that is recognized above
140 // and will trigger an immediate exit.
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200141 throw ProcessExit(1, immediate: true);
Todd Volkert454db9d2017-11-09 21:45:31 -0800142 }
143 }
144 }
145}
146
147/// File system used by the crash reporting logic.
148///
149/// We do not want to use the file system stored in the context because it may
150/// be recording. Additionally, in the case of a crash we do not trust the
151/// integrity of the [AppContext].
152@visibleForTesting
153FileSystem crashFileSystem = const LocalFileSystem();
154
155/// Saves the crash report to a local file.
156Future<File> _createLocalCrashReport(List<String> args, dynamic error, StackTrace stackTrace) async {
157 File crashFile = getUniqueFile(crashFileSystem.currentDirectory, 'flutter', 'log');
158
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200159 final StringBuffer buffer = StringBuffer();
Todd Volkert454db9d2017-11-09 21:45:31 -0800160
161 buffer.writeln('Flutter crash report; please file at https://github.com/flutter/flutter/issues.\n');
162
163 buffer.writeln('## command\n');
164 buffer.writeln('flutter ${args.join(' ')}\n');
165
166 buffer.writeln('## exception\n');
167 buffer.writeln('${error.runtimeType}: $error\n');
168 buffer.writeln('```\n$stackTrace```\n');
169
170 buffer.writeln('## flutter doctor\n');
171 buffer.writeln('```\n${await _doctorText()}```');
172
173 try {
174 await crashFile.writeAsString(buffer.toString());
175 } on FileSystemException catch (_) {
176 // Fallback to the system temporary directory.
177 crashFile = getUniqueFile(crashFileSystem.systemTempDirectory, 'flutter', 'log');
178 try {
179 await crashFile.writeAsString(buffer.toString());
180 } on FileSystemException catch (e) {
181 printError('Could not write crash report to disk: $e');
182 printError(buffer.toString());
183 }
184 }
185
186 return crashFile;
187}
188
189Future<String> _doctorText() async {
190 try {
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200191 final BufferLogger logger = BufferLogger();
Todd Volkert454db9d2017-11-09 21:45:31 -0800192
Todd Volkertf8058d72018-03-28 15:17:29 -0700193 await context.run<bool>(
Todd Volkert8d11f5c2018-03-28 10:58:28 -0700194 body: () => doctor.diagnose(verbose: true),
195 overrides: <Type, Generator>{
196 Logger: () => logger,
197 },
198 );
Todd Volkert454db9d2017-11-09 21:45:31 -0800199
200 return logger.statusText;
201 } catch (error, trace) {
202 return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
203 }
204}
205
206Future<int> _exit(int code) async {
207 if (flutterUsage.isFirstRun)
208 flutterUsage.printWelcome();
209
210 // Send any last analytics calls that are in progress without overly delaying
211 // the tool's exit (we wait a maximum of 250ms).
212 if (flutterUsage.enabled) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200213 final Stopwatch stopwatch = Stopwatch()..start();
Todd Volkert454db9d2017-11-09 21:45:31 -0800214 await flutterUsage.ensureAnalyticsSent();
215 printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
216 }
217
218 // Run shutdown hooks before flushing logs
219 await runShutdownHooks();
220
Alexandre Ardhuin2d3ff102018-10-05 07:54:56 +0200221 final Completer<void> completer = Completer<void>();
Todd Volkert454db9d2017-11-09 21:45:31 -0800222
223 // Give the task / timer queue one cycle through before we hard exit.
224 Timer.run(() {
225 try {
226 printTrace('exiting with code $code');
227 exit(code);
228 completer.complete();
229 } catch (error, stackTrace) {
230 completer.completeError(error, stackTrace);
231 }
232 });
233
234 await completer.future;
235 return code;
236}