Handle ProcessExceptions due to git missing on the host (#154445)

Fixes https://github.com/flutter/flutter/issues/133585.

This PR elects to add a new catch within `_handleToolError` that checks any uncaught error. This new catch will exit the tool without crashing provided the following conditions are met:

1. the error is a `ProcessException`,
2. the error message contains `git` somewhere in the message (we don't match on the entire string in case it changes or is locale-dependent), and
3. `git` does not appear to be runnable on the host system (`ProcessManager.canRun` returns `false` for git).

This is preferable to checking for runnability of `git` before we run it for 1) its simplicity and 2) lack of performance penalty for users that already have git installed (almost every single one).

This PR also does some light refactoring to runner_test.dart to make room for tests that aren't related to crash reporting.
This commit is contained in:
Andrew Kolos
2024-09-09 13:47:05 -07:00
committed by GitHub
parent 8435e12ce5
commit 9cd2fc90af
3 changed files with 142 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import 'dart:async';
import 'package:args/command_runner.dart';
import 'package:intl/intl.dart' as intl;
import 'package:intl/intl_standalone.dart' as intl_standalone;
import 'package:process/process.dart';
import 'package:unified_analytics/unified_analytics.dart';
import 'src/base/async_guard.dart';
@@ -185,6 +186,16 @@ Future<int> _handleToolError(
} else {
return exitWithHooks(error.exitCode, shutdownHooks: shutdownHooks);
}
} else if (error is ProcessException &&
_isErrorDueToGitMissing(error, globals.processManager, globals.logger)) {
globals.printError('${error.message}\n');
globals.printError(
'An error was encountered when trying to run git.\n'
"Please ensure git is installed and available in your system's search path. "
'See https://docs.flutter.dev/get-started/install for instructions on '
'installing git for your platform.',
);
return exitWithHooks(1, shutdownHooks: shutdownHooks);
} else {
// We've crashed; emit a log report.
globals.stdio.stderrWrite('\n');
@@ -301,3 +312,22 @@ Future<File> _createLocalCrashReport(CrashDetails details) async {
return crashFile;
}
bool _isErrorDueToGitMissing(
ProcessException exception,
ProcessManager processManager,
Logger logger,
) {
if (!exception.message.contains('git')) {
return false;
}
try {
return !processManager.canRun('git');
} on Object catch (error) {
logger.printTrace(
'Unable to check whether git is runnable: $error\n'
);
return true;
}
}

View File

@@ -578,7 +578,7 @@ Future<ChromeTab?> getChromeTabGuarded(
void Function(Object error, StackTrace stackTrace)? onIoError,
}) async {
try {
return asyncGuard(
return await asyncGuard(
() => chromeConnection.getTab(
accept,
retryFor: retryFor,