runner.dart 10.7 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 'src/base/async_guard.dart';
12 13
import 'src/base/common.dart';
import 'src/base/context.dart';
14
import 'src/base/error_handling_io.dart';
15 16 17 18
import 'src/base/file_system.dart';
import 'src/base/io.dart';
import 'src/base/logger.dart';
import 'src/base/process.dart';
19
import 'src/context_runner.dart';
20
import 'src/doctor.dart';
21
import 'src/globals.dart' as globals;
22
import 'src/reporting/crash_reporting.dart';
23
import 'src/reporting/reporting.dart';
24 25 26 27 28 29
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,
30
  List<FlutterCommand> Function() commands, {
31 32 33
    bool muteCommandLogging = false,
    bool verbose = false,
    bool verboseHelp = false,
34 35 36
    bool? reportCrashes,
    String? flutterVersion,
    Map<Type, Generator>? overrides,
37
    required ShutdownHooks shutdownHooks,
38
  }) async {
39 40 41
  if (muteCommandLogging) {
    // Remove the verbose option; for help and doctor, users don't need to see
    // verbose logs.
42
    args = List<String>.of(args);
43
    args.removeWhere((String option) => option == '-vv' || option == '-v' || option == '--verbose');
44 45
  }

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

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
    return runZoned<Future<int>>(() async {
62
      try {
63 64
        if (args.contains('--disable-analytics') &&
            args.contains('--enable-analytics')) {
65
          throwToolExit(
66
              'Both enable and disable analytics commands were detected '
67 68 69 70
              'when only one can be supplied per invocation.',
              exitCode: 1);
        }

71 72
        // Disable analytics if user passes in the `--disable-analytics` option
        // "flutter --disable-analytics"
73
        //
74
        // Same functionality as "flutter config --no-analytics" for disabling
75
        // except with the `value` hard coded as false
76
        if (args.contains('--disable-analytics')) {
77 78
          // The tool sends the analytics event *before* toggling the flag
          // intentionally to be sure that opt-out events are sent correctly.
79 80 81 82 83 84 85 86
          AnalyticsConfigEvent(enabled: false).send();

          // Normally, the tool waits for the analytics to all send before the
          // tool exits, but only when analytics are enabled. When reporting that
          // analytics have been disable, the wait must be done here instead.
          await globals.flutterUsage.ensureAnalyticsSent();

          globals.flutterUsage.enabled = false;
87 88 89 90 91
          globals.printStatus('Analytics reporting disabled.');

          // TODO(eliasyishak): Set the telemetry for the unified_analytics
          //  package as well, the above will be removed once we have
          //  fully transitioned to using the new package
92 93 94
          await globals.analytics.setTelemetry(false);
        }

95 96
        // Enable analytics if user passes in the `--enable-analytics` option
        // `flutter --enable-analytics`
97 98 99
        //
        // Same functionality as `flutter config --analytics` for enabling
        // except with the `value` hard coded as true
100
        if (args.contains('--enable-analytics')) {
101 102 103 104 105 106 107
          // The tool sends the analytics event *before* toggling the flag
          // intentionally to be sure that opt-out events are sent correctly.
          AnalyticsConfigEvent(enabled: true).send();

          globals.flutterUsage.enabled = true;
          globals.printStatus('Analytics reporting enabled.');

108 109 110
          // TODO(eliasyishak): Set the telemetry for the unified_analytics
          //  package as well, the above will be removed once we have
          //  fully transitioned to using the new package
111
          await globals.analytics.setTelemetry(true);
112 113
        }

114

115
        await runner.run(args);
116 117

        // Triggering [runZoned]'s error callback does not necessarily mean that
118
        // we stopped executing the body. See https://github.com/dart-lang/sdk/issues/42150.
119
        if (firstError == null) {
120
          return await exitWithHooks(0, shutdownHooks: shutdownHooks);
121 122
        }

123
        // We already hit some error, so don't return success. The error path
124 125
        // (which should be in progress) is responsible for calling _exit().
        return 1;
126 127
      } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses
        // This catches all exceptions to send to crash logging, etc.
128 129
        firstError = error;
        firstStackTrace = stackTrace;
130
        return _handleToolError(error, stackTrace, verbose, args, reportCrashes!, getVersion, shutdownHooks);
131
      }
132
    }, onError: (Object error, StackTrace stackTrace) async { // ignore: deprecated_member_use
133 134 135
      // 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.
136 137
      firstError ??= error;
      firstStackTrace ??= stackTrace;
138
      await _handleToolError(firstError!, firstStackTrace, verbose, args, reportCrashes!, getVersion, shutdownHooks);
139
    });
140
  }, overrides: overrides);
141 142 143
}

Future<int> _handleToolError(
144 145
  Object error,
  StackTrace? stackTrace,
146 147 148
  bool verbose,
  List<String> args,
  bool reportCrashes,
149
  String Function() getFlutterVersion,
150
  ShutdownHooks shutdownHooks,
151
) async {
152
  if (error is UsageException) {
153 154
    globals.printError('${error.message}\n');
    globals.printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.");
155
    // Argument error exit code.
156
    return exitWithHooks(64, shutdownHooks: shutdownHooks);
157
  } else if (error is ToolExit) {
158
    if (error.message != null) {
159
      globals.printError(error.message!);
160 161
    }
    if (verbose) {
162
      globals.printError('\n$stackTrace\n');
163
    }
164
    return exitWithHooks(error.exitCode ?? 1, shutdownHooks: shutdownHooks);
165 166 167 168 169 170
  } else if (error is ProcessExit) {
    // We've caught an exit code.
    if (error.immediate) {
      exit(error.exitCode);
      return error.exitCode;
    } else {
171
      return exitWithHooks(error.exitCode, shutdownHooks: shutdownHooks);
172 173 174
    }
  } else {
    // We've crashed; emit a log report.
175
    globals.stdio.stderrWrite('\n');
176 177 178

    if (!reportCrashes) {
      // Print the stack trace on the bots - don't write a crash report.
179 180
      globals.stdio.stderrWrite('$error\n');
      globals.stdio.stderrWrite('$stackTrace\n');
181
      return exitWithHooks(1, shutdownHooks: shutdownHooks);
182
    }
183

184
    // Report to both [Usage] and [CrashReportSender].
185
    globals.flutterUsage.sendException(error);
186 187 188 189 190 191 192 193 194
    await asyncGuard(() async {
      final CrashReportSender crashReportSender = CrashReportSender(
        usage: globals.flutterUsage,
        platform: globals.platform,
        logger: globals.logger,
        operatingSystemUtils: globals.os,
      );
      await crashReportSender.sendReport(
        error: error,
195
        stackTrace: stackTrace!,
196 197 198 199 200 201
        getFlutterVersion: getFlutterVersion,
        command: args.join(' '),
      );
    }, onError: (dynamic error) {
      globals.printError('Error sending crash report: $error');
    });
202

203
    globals.printError('Oops; flutter has exited unexpectedly: "$error".');
204 205

    try {
206 207 208 209 210 211 212
      final BufferLogger logger = BufferLogger(
        terminal: globals.terminal,
        outputPreferences: globals.outputPreferences,
      );

      final DoctorText doctorText = DoctorText(logger);

213 214 215
      final CrashDetails details = CrashDetails(
        command: _crashCommand(args),
        error: error,
216
        stackTrace: stackTrace!,
217
        doctorText: doctorText,
218 219
      );
      final File file = await _createLocalCrashReport(details);
220
      await globals.crashReporter!.informUser(details, file);
221

222
      return exitWithHooks(1, shutdownHooks: shutdownHooks);
223
    // This catch catches all exceptions to ensure the message below is printed.
224
    } catch (error, st) { // ignore: avoid_catches_without_on_clauses
225
      globals.stdio.stderrWrite(
226
        'Unable to generate crash report due to secondary error: $error\n$st\n'
227 228
        '${globals.userMessages.flutterToolBugInstructions}\n',
      );
229
      // Any exception thrown here (including one thrown by `_exit()`) will
230 231 232 233
      // 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);
234 235 236 237
    }
  }
}

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

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

242
/// Saves the crash report to a local file.
243
Future<File> _createLocalCrashReport(CrashDetails details) async {
244
  final StringBuffer buffer = StringBuffer();
245

246 247
  buffer.writeln('Flutter crash report.');
  buffer.writeln('${globals.userMessages.flutterToolBugInstructions}\n');
248 249

  buffer.writeln('## command\n');
250
  buffer.writeln('${details.command}\n');
251 252

  buffer.writeln('## exception\n');
253 254
  buffer.writeln('${_crashException(details.error)}\n');
  buffer.writeln('```\n${details.stackTrace}```\n');
255 256

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

259 260
  late File crashFile;
  ErrorHandlingFileSystem.noExitOnFailure(() {
261
    try {
262 263 264 265 266
      crashFile = globals.fsUtils.getUniqueFile(
        globals.fs.currentDirectory,
        'flutter',
        'log',
      );
267
      crashFile.writeAsStringSync(buffer.toString());
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    } 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;
      }
283
    }
284
  });
285 286 287

  return crashFile;
}