runner.dart 9.54 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
import 'package:meta/meta.dart';

12
import 'src/base/bot_detector.dart';
13 14 15 16 17 18
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';
19
import 'src/base/terminal.dart';
20
import 'src/context_runner.dart';
21
import 'src/doctor.dart';
22
import 'src/globals.dart' as globals;
23
import 'src/reporting/github_template.dart';
24
import 'src/reporting/reporting.dart';
25 26 27 28 29 30 31
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, {
32 33 34
  bool muteCommandLogging = false,
  bool verbose = false,
  bool verboseHelp = false,
35 36
  bool reportCrashes,
  String flutterVersion,
37
  Map<Type, Generator> overrides,
38
}) {
39
  reportCrashes ??= !isRunningOnBot(globals.platform);
40

41 42 43
  if (muteCommandLogging) {
    // Remove the verbose option; for help and doctor, users don't need to see
    // verbose logs.
44
    args = List<String>.from(args);
45 46 47
    args.removeWhere((String option) => option == '-v' || option == '--verbose');
  }

48
  final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp);
49 50
  commands.forEach(runner.addCommand);

51
  return runInContext<int>(() async {
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 63 64 65 66
    return await runZoned<Future<int>>(() async {
      try {
        await runner.run(args);
        return await _exit(0);
      } catch (error, stackTrace) {
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 124 125 126 127 128 129 130
    // Report to both [Usage] and [CrashReportSender].
    flutterUsage.sendException(error);
    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 139
      return _exit(1);
    } catch (error) {
140
      globals.stdio.stderrWrite(
141
        'Unable to generate crash report due to secondary error: $error\n'
142
        'please let us know at https://github.com/flutter/flutter/issues.\n',
143 144 145 146 147 148
      );
      // 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);
149 150 151 152
    }
  }
}

153 154 155 156
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);

157 158
  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);
159 160 161

  final GitHubTemplateCreator gitHubTemplateCreator = context.get<GitHubTemplateCreator>() ?? GitHubTemplateCreator();
  final String similarIssuesURL = await gitHubTemplateCreator.toolCrashSimilarIssuesGitHubURL(errorString);
162 163 164 165
  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);
166 167 168 169 170 171 172 173 174

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

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

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

182 183 184 185 186 187 188 189 190
/// 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.
191
Future<File> _createLocalCrashReport(List<String> args, dynamic error, StackTrace stackTrace, String doctorText) async {
192 193 194 195 196
  File crashFile = globals.fsUtils.getUniqueFile(
    crashFileSystem.currentDirectory,
    'flutter',
    'log',
  );
197

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

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

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

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

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

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

  return crashFile;
}

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

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

    return logger.statusText;
  } catch (error, trace) {
    return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
  }
}

Future<int> _exit(int code) async {
253 254
  // Prints the welcome message if needed.
  flutterUsage.printWelcome();
255 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).
  if (flutterUsage.enabled) {
259
    final Stopwatch stopwatch = Stopwatch()..start();
260
    await flutterUsage.ensureAnalyticsSent();
261
    globals.printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
262 263 264
  }

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

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

  // Give the task / timer queue one cycle through before we hard exit.
  Timer.run(() {
    try {
272
      globals.printTrace('exiting with code $code');
273 274 275 276 277 278 279 280 281 282
      exit(code);
      completer.complete();
    } catch (error, stackTrace) {
      completer.completeError(error, stackTrace);
    }
  });

  await completer.future;
  return code;
}