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

import 'dart:async';

import 'package:args/command_runner.dart';
8 9
import 'package:intl/intl.dart' as intl;
import 'package:intl/intl_standalone.dart' as intl_standalone;
10 11 12 13 14 15 16 17
import 'package:meta/meta.dart';

import 'src/base/common.dart';
import 'src/base/context.dart';
import 'src/base/file_system.dart';
import 'src/base/io.dart';
import 'src/base/logger.dart';
import 'src/base/process.dart';
18
import 'src/base/terminal.dart';
19
import 'src/context_runner.dart';
20
import 'src/doctor.dart';
21
import 'src/globals.dart' as globals;
22
import 'src/reporting/github_template.dart';
23
import 'src/reporting/reporting.dart';
24 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,
  List<FlutterCommand> commands, {
31 32 33
  bool muteCommandLogging = false,
  bool verbose = false,
  bool verboseHelp = false,
34 35
  bool reportCrashes,
  String flutterVersion,
36
  Map<Type, Generator> overrides,
37
}) async {
38 39 40
  if (muteCommandLogging) {
    // Remove the verbose option; for help and doctor, users don't need to see
    // verbose logs.
41
    args = List<String>.from(args);
42 43 44
    args.removeWhere((String option) => option == '-v' || option == '--verbose');
  }

45
  final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp);
46 47
  commands.forEach(runner.addCommand);

48
  return runInContext<int>(() async {
49 50
    reportCrashes ??= !await globals.isRunningOnBot;

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

58
    String getVersion() => flutterVersion ?? globals.flutterVersion.getVersionString(redactUnknownBranches: true);
59 60
    Object firstError;
    StackTrace firstStackTrace;
61 62 63 64
    return await runZoned<Future<int>>(() async {
      try {
        await runner.run(args);
        return await _exit(0);
65 66
      // This catches all exceptions to send to crash logging, etc.
      } catch (error, stackTrace) {  // ignore: avoid_catches_without_on_clauses
67 68
        firstError = error;
        firstStackTrace = stackTrace;
69 70 71 72
        return await _handleToolError(
            error, stackTrace, verbose, args, reportCrashes, getVersion);
      }
    }, onError: (Object error, StackTrace stackTrace) async {
73 74 75 76 77 78
      // 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.
      final Object e = firstError ?? error;
      final StackTrace s = firstStackTrace ?? stackTrace;
      await _handleToolError(e, s, verbose, args, reportCrashes, getVersion);
79
    });
80
  }, overrides: overrides);
81 82 83
}

Future<int> _handleToolError(
84 85 86 87 88 89 90
  dynamic error,
  StackTrace stackTrace,
  bool verbose,
  List<String> args,
  bool reportCrashes,
  String getFlutterVersion(),
) async {
91
  if (error is UsageException) {
92 93
    globals.printError('${error.message}\n');
    globals.printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.");
94 95 96
    // Argument error exit code.
    return _exit(64);
  } else if (error is ToolExit) {
97
    if (error.message != null) {
98
      globals.printError(error.message);
99 100
    }
    if (verbose) {
101
      globals.printError('\n$stackTrace\n');
102
    }
103 104 105 106 107 108 109 110 111 112 113
    return _exit(error.exitCode ?? 1);
  } else if (error is ProcessExit) {
    // We've caught an exit code.
    if (error.immediate) {
      exit(error.exitCode);
      return error.exitCode;
    } else {
      return _exit(error.exitCode);
    }
  } else {
    // We've crashed; emit a log report.
114
    globals.stdio.stderrWrite('\n');
115 116 117

    if (!reportCrashes) {
      // Print the stack trace on the bots - don't write a crash report.
118 119
      globals.stdio.stderrWrite('$error\n');
      globals.stdio.stderrWrite('$stackTrace\n');
120
      return _exit(1);
121
    }
122

123
    // Report to both [Usage] and [CrashReportSender].
124
    globals.flutterUsage.sendException(error);
125 126 127 128 129 130
    await CrashReportSender.instance.sendReport(
      error: error,
      stackTrace: stackTrace,
      getFlutterVersion: getFlutterVersion,
      command: args.join(' '),
    );
131

132
    final String errorString = error.toString();
133
    globals.printError('Oops; flutter has exited unexpectedly: "$errorString".');
134 135

    try {
136 137
      await _informUserOfCrash(args, error, stackTrace, errorString);

138
      return _exit(1);
139 140
    // This catch catches all exceptions to ensure the message below is printed.
    } catch (error) { // ignore: avoid_catches_without_on_clauses
141
      globals.stdio.stderrWrite(
142
        'Unable to generate crash report due to secondary error: $error\n'
143
        'please let us know at https://github.com/flutter/flutter/issues.\n',
144 145 146 147 148 149
      );
      // Any exception throw here (including one thrown by `_exit()`) will
      // 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);
150 151 152 153
    }
  }
}

154 155 156 157
Future<void> _informUserOfCrash(List<String> args, dynamic error, StackTrace stackTrace, String errorString) async {
  final String doctorText = await _doctorText();
  final File file = await _createLocalCrashReport(args, error, stackTrace, doctorText);

158 159
  globals.printError('A crash report has been written to ${file.path}.');
  globals.printStatus('This crash may already be reported. Check GitHub for similar crashes.', emphasis: true);
160 161 162

  final GitHubTemplateCreator gitHubTemplateCreator = context.get<GitHubTemplateCreator>() ?? GitHubTemplateCreator();
  final String similarIssuesURL = await gitHubTemplateCreator.toolCrashSimilarIssuesGitHubURL(errorString);
163 164 165 166
  globals.printStatus('$similarIssuesURL\n', wrap: false);
  globals.printStatus('To report your crash to the Flutter team, first read the guide to filing a bug.', emphasis: true);
  globals.printStatus('https://flutter.dev/docs/resources/bug-reports\n', wrap: false);
  globals.printStatus('Create a new GitHub issue by pasting this link into your browser and completing the issue template. Thank you!', emphasis: true);
167 168 169 170 171 172 173 174 175

  final String command = _crashCommand(args);
  final String gitHubTemplateURL = await gitHubTemplateCreator.toolCrashIssueTemplateGitHubURL(
    command,
    errorString,
    _crashException(error),
    stackTrace,
    doctorText
  );
176
  globals.printStatus('$gitHubTemplateURL\n', wrap: false);
177 178 179 180 181 182
}

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

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

183 184 185 186 187 188 189 190 191
/// File system used by the crash reporting logic.
///
/// We do not want to use the file system stored in the context because it may
/// be recording. Additionally, in the case of a crash we do not trust the
/// integrity of the [AppContext].
@visibleForTesting
FileSystem crashFileSystem = const LocalFileSystem();

/// Saves the crash report to a local file.
192
Future<File> _createLocalCrashReport(List<String> args, dynamic error, StackTrace stackTrace, String doctorText) async {
193 194 195 196 197
  File crashFile = globals.fsUtils.getUniqueFile(
    crashFileSystem.currentDirectory,
    'flutter',
    'log',
  );
198

199
  final StringBuffer buffer = StringBuffer();
200 201 202 203

  buffer.writeln('Flutter crash report; please file at https://github.com/flutter/flutter/issues.\n');

  buffer.writeln('## command\n');
204
  buffer.writeln('${_crashCommand(args)}\n');
205 206

  buffer.writeln('## exception\n');
207
  buffer.writeln('${_crashException(error)}\n');
208 209 210
  buffer.writeln('```\n$stackTrace```\n');

  buffer.writeln('## flutter doctor\n');
211
  buffer.writeln('```\n$doctorText```');
212 213

  try {
214
    crashFile.writeAsStringSync(buffer.toString());
215 216
  } on FileSystemException catch (_) {
    // Fallback to the system temporary directory.
217 218 219 220 221
    crashFile = globals.fsUtils.getUniqueFile(
      crashFileSystem.systemTempDirectory,
      'flutter',
      'log',
    );
222
    try {
223
      crashFile.writeAsStringSync(buffer.toString());
224
    } on FileSystemException catch (e) {
225 226
      globals.printError('Could not write crash report to disk: $e');
      globals.printError(buffer.toString());
227 228 229 230 231 232 233 234
    }
  }

  return crashFile;
}

Future<String> _doctorText() async {
  try {
235
    final BufferLogger logger = BufferLogger(
236
      terminal: globals.terminal,
237 238
      outputPreferences: outputPreferences,
    );
239

240
    await context.run<bool>(
241
      body: () => doctor.diagnose(verbose: true, showColor: false),
242 243 244 245
      overrides: <Type, Generator>{
        Logger: () => logger,
      },
    );
246 247

    return logger.statusText;
248
  } on Exception catch (error, trace) {
249 250 251 252 253
    return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
  }
}

Future<int> _exit(int code) async {
254
  // Prints the welcome message if needed.
255
  globals.flutterUsage.printWelcome();
256 257 258

  // Send any last analytics calls that are in progress without overly delaying
  // the tool's exit (we wait a maximum of 250ms).
259
  if (globals.flutterUsage.enabled) {
260
    final Stopwatch stopwatch = Stopwatch()..start();
261
    await globals.flutterUsage.ensureAnalyticsSent();
262
    globals.printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
263 264 265
  }

  // Run shutdown hooks before flushing logs
266
  await shutdownHooks.runShutdownHooks();
267

268
  final Completer<void> completer = Completer<void>();
269 270 271 272

  // Give the task / timer queue one cycle through before we hard exit.
  Timer.run(() {
    try {
273
      globals.printTrace('exiting with code $code');
274 275
      exit(code);
      completer.complete();
276 277 278
    // This catches all exceptions becauce the error is propagated on the
    // completer.
    } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses
279 280 281 282 283 284 285
      completer.completeError(error, stackTrace);
    }
  });

  await completer.future;
  return code;
}