runner.dart 9.36 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5

6

7 8 9
import 'dart:async';

import 'package:args/command_runner.dart';
10 11
import 'package:intl/intl.dart' as intl;
import 'package:intl/intl_standalone.dart' as intl_standalone;
12

13
import 'src/base/async_guard.dart';
14 15
import 'src/base/common.dart';
import 'src/base/context.dart';
16
import 'src/base/error_handling_io.dart';
17 18 19 20
import 'src/base/file_system.dart';
import 'src/base/io.dart';
import 'src/base/logger.dart';
import 'src/base/process.dart';
21
import 'src/context_runner.dart';
22
import 'src/doctor.dart';
23
import 'src/globals.dart' as globals;
24
import 'src/reporting/crash_reporting.dart';
25 26 27 28 29 30
import 'src/runner/flutter_command.dart';
import 'src/runner/flutter_command_runner.dart';

/// Runs the Flutter tool with support for the specified list of [commands].
Future<int> run(
  List<String> args,
31
  List<FlutterCommand> Function() commands, {
32 33 34
    bool muteCommandLogging = false,
    bool verbose = false,
    bool verboseHelp = false,
35 36 37
    bool? reportCrashes,
    String? flutterVersion,
    Map<Type, Generator>? overrides,
38
    required ShutdownHooks shutdownHooks,
39
  }) async {
40 41 42
  if (muteCommandLogging) {
    // Remove the verbose option; for help and doctor, users don't need to see
    // verbose logs.
43
    args = List<String>.of(args);
44
    args.removeWhere((String option) => option == '-vv' || option == '-v' || option == '--verbose');
45 46
  }

47
  return runInContext<int>(() async {
48
    reportCrashes ??= !await globals.isRunningOnBot;
49
    final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp);
50
    commands().forEach(runner.addCommand);
51

52
    // Initialize the system locale.
53 54 55
    final String systemLocale = await intl_standalone.findSystemLocale();
    intl.Intl.defaultLocale = intl.Intl.verifiedLocale(
      systemLocale, intl.NumberFormat.localeExists,
56
      onFailure: (String _) => 'en_US',
57
    );
58

59
    String getVersion() => flutterVersion ?? globals.flutterVersion.getVersionString(redactUnknownBranches: true);
60 61
    Object? firstError;
    StackTrace? firstStackTrace;
62
    return runZoned<Future<int>>(() async {
63 64
      try {
        await runner.run(args);
65 66

        // Triggering [runZoned]'s error callback does not necessarily mean that
67
        // we stopped executing the body. See https://github.com/dart-lang/sdk/issues/42150.
68
        if (firstError == null) {
69
          return await _exit(0, shutdownHooks: shutdownHooks);
70 71
        }

72
        // We already hit some error, so don't return success. The error path
73 74
        // (which should be in progress) is responsible for calling _exit().
        return 1;
75 76
      } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses
        // This catches all exceptions to send to crash logging, etc.
77 78
        firstError = error;
        firstStackTrace = stackTrace;
79
        return _handleToolError(error, stackTrace, verbose, args, reportCrashes!, getVersion, shutdownHooks);
80
      }
81
    }, onError: (Object error, StackTrace stackTrace) async { // ignore: deprecated_member_use
82 83 84
      // If sending a crash report throws an error into the zone, we don't want
      // to re-try sending the crash report with *that* error. Rather, we want
      // to send the original error that triggered the crash report.
85 86
      firstError ??= error;
      firstStackTrace ??= stackTrace;
87
      await _handleToolError(firstError!, firstStackTrace, verbose, args, reportCrashes!, getVersion, shutdownHooks);
88
    });
89
  }, overrides: overrides);
90 91 92
}

Future<int> _handleToolError(
93 94
  Object error,
  StackTrace? stackTrace,
95 96 97
  bool verbose,
  List<String> args,
  bool reportCrashes,
98
  String Function() getFlutterVersion,
99
  ShutdownHooks shutdownHooks,
100
) async {
101
  if (error is UsageException) {
102 103
    globals.printError('${error.message}\n');
    globals.printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.");
104
    // Argument error exit code.
105
    return _exit(64, shutdownHooks: shutdownHooks);
106
  } else if (error is ToolExit) {
107
    if (error.message != null) {
108
      globals.printError(error.message!);
109 110
    }
    if (verbose) {
111
      globals.printError('\n$stackTrace\n');
112
    }
113
    return _exit(error.exitCode ?? 1, shutdownHooks: shutdownHooks);
114 115 116 117 118 119
  } else if (error is ProcessExit) {
    // We've caught an exit code.
    if (error.immediate) {
      exit(error.exitCode);
      return error.exitCode;
    } else {
120
      return _exit(error.exitCode, shutdownHooks: shutdownHooks);
121 122 123
    }
  } else {
    // We've crashed; emit a log report.
124
    globals.stdio.stderrWrite('\n');
125 126 127

    if (!reportCrashes) {
      // Print the stack trace on the bots - don't write a crash report.
128 129
      globals.stdio.stderrWrite('$error\n');
      globals.stdio.stderrWrite('$stackTrace\n');
130
      return _exit(1, shutdownHooks: shutdownHooks);
131
    }
132

133
    // Report to both [Usage] and [CrashReportSender].
134
    globals.flutterUsage.sendException(error);
135 136 137 138 139 140 141 142 143
    await asyncGuard(() async {
      final CrashReportSender crashReportSender = CrashReportSender(
        usage: globals.flutterUsage,
        platform: globals.platform,
        logger: globals.logger,
        operatingSystemUtils: globals.os,
      );
      await crashReportSender.sendReport(
        error: error,
144
        stackTrace: stackTrace!,
145 146 147 148 149 150
        getFlutterVersion: getFlutterVersion,
        command: args.join(' '),
      );
    }, onError: (dynamic error) {
      globals.printError('Error sending crash report: $error');
    });
151

152
    globals.printError('Oops; flutter has exited unexpectedly: "$error".');
153 154

    try {
155 156 157 158 159 160 161
      final BufferLogger logger = BufferLogger(
        terminal: globals.terminal,
        outputPreferences: globals.outputPreferences,
      );

      final DoctorText doctorText = DoctorText(logger);

162 163 164
      final CrashDetails details = CrashDetails(
        command: _crashCommand(args),
        error: error,
165
        stackTrace: stackTrace!,
166
        doctorText: doctorText,
167 168
      );
      final File file = await _createLocalCrashReport(details);
169
      await globals.crashReporter!.informUser(details, file);
170

171
      return _exit(1, shutdownHooks: shutdownHooks);
172
    // This catch catches all exceptions to ensure the message below is printed.
173
    } catch (error, st) { // ignore: avoid_catches_without_on_clauses
174
      globals.stdio.stderrWrite(
175
        'Unable to generate crash report due to secondary error: $error\n$st\n'
176 177
        '${globals.userMessages.flutterToolBugInstructions}\n',
      );
178
      // Any exception thrown here (including one thrown by `_exit()`) will
179 180 181 182
      // get caught by our zone's `onError` handler. In order to avoid an
      // infinite error loop, we throw an error that is recognized above
      // and will trigger an immediate exit.
      throw ProcessExit(1, immediate: true);
183 184 185 186
    }
  }
}

187 188 189 190
String _crashCommand(List<String> args) => 'flutter ${args.join(' ')}';

String _crashException(dynamic error) => '${error.runtimeType}: $error';

191
/// Saves the crash report to a local file.
192
Future<File> _createLocalCrashReport(CrashDetails details) async {
193
  final StringBuffer buffer = StringBuffer();
194

195 196
  buffer.writeln('Flutter crash report.');
  buffer.writeln('${globals.userMessages.flutterToolBugInstructions}\n');
197 198

  buffer.writeln('## command\n');
199
  buffer.writeln('${details.command}\n');
200 201

  buffer.writeln('## exception\n');
202 203
  buffer.writeln('${_crashException(details.error)}\n');
  buffer.writeln('```\n${details.stackTrace}```\n');
204 205

  buffer.writeln('## flutter doctor\n');
206
  buffer.writeln('```\n${await details.doctorText.text}```');
207

208 209
  late File crashFile;
  ErrorHandlingFileSystem.noExitOnFailure(() {
210
    try {
211 212 213 214 215
      crashFile = globals.fsUtils.getUniqueFile(
        globals.fs.currentDirectory,
        'flutter',
        'log',
      );
216
      crashFile.writeAsStringSync(buffer.toString());
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    } on FileSystemException catch (_) {
      // Fallback to the system temporary directory.
      try {
        crashFile = globals.fsUtils.getUniqueFile(
          globals.fs.systemTempDirectory,
          'flutter',
          'log',
        );
        crashFile.writeAsStringSync(buffer.toString());
      } on FileSystemException catch (e) {
        globals.printError('Could not write crash report to disk: $e');
        globals.printError(buffer.toString());

        rethrow;
      }
232
    }
233
  });
234 235 236 237

  return crashFile;
}

238
Future<int> _exit(int code, {required ShutdownHooks shutdownHooks}) async {
239
  // Prints the welcome message if needed.
240
  globals.flutterUsage.printWelcome();
241 242 243

  // Send any last analytics calls that are in progress without overly delaying
  // the tool's exit (we wait a maximum of 250ms).
244
  if (globals.flutterUsage.enabled) {
245
    final Stopwatch stopwatch = Stopwatch()..start();
246
    await globals.flutterUsage.ensureAnalyticsSent();
247
    globals.printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
248 249 250
  }

  // Run shutdown hooks before flushing logs
251
  await shutdownHooks.runShutdownHooks(globals.logger);
252

253
  final Completer<void> completer = Completer<void>();
254 255 256 257

  // Give the task / timer queue one cycle through before we hard exit.
  Timer.run(() {
    try {
258
      globals.printTrace('exiting with code $code');
259 260
      exit(code);
      completer.complete();
261
    // This catches all exceptions because the error is propagated on the
262 263
    // completer.
    } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses
264 265 266 267 268 269 270
      completer.completeError(error, stackTrace);
    }
  });

  await completer.future;
  return code;
}