Move corrupted zip to a separate handler and ask user (#105054)

This commit is contained in:
Emmanuel Garcia
2022-06-03 10:08:08 -07:00
committed by GitHub
parent d4a9631651
commit 975e04ba6d
2 changed files with 184 additions and 56 deletions

View File

@@ -81,6 +81,7 @@ final List<GradleHandledError> gradleErrors = <GradleHandledError>[
jvm11RequiredHandler,
outdatedGradleHandler,
sslExceptionHandler,
zipExceptionHandler,
];
const String _boxTitle = 'Flutter Fix';
@@ -206,12 +207,6 @@ final GradleHandledError permissionDeniedErrorHandler = GradleHandledError(
/// Gradle crashes for several known reasons when downloading that are not
/// actionable by Flutter.
///
/// The Gradle cache directory must be deleted, otherwise it may attempt to
/// re-use the bad zip file.
///
/// See also:
/// * https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home
@visibleForTesting
final GradleHandledError networkErrorHandler = GradleHandledError(
test: _lineMatcher(const <String>[
@@ -234,20 +229,73 @@ final GradleHandledError networkErrorHandler = GradleHandledError(
'${globals.logger.terminal.warningMark} '
'Gradle threw an error while downloading artifacts from the network.'
);
try {
final String? homeDir = globals.platform.environment['HOME'];
if (homeDir != null) {
final Directory directory = globals.fs.directory(globals.fs.path.join(homeDir, '.gradle'));
ErrorHandlingFileSystem.deleteIfExists(directory, recursive: true);
}
} on FileSystemException catch (err) {
globals.printTrace('Failed to delete Gradle cache: $err');
}
return GradleBuildStatus.retry;
},
eventLabel: 'network',
);
/// Handles corrupted jar or other types of zip files.
///
/// If a terminal is attached, this handler prompts the user if they would like to
/// delete the $HOME/.gradle directory prior to retrying the build.
///
/// If this handler runs on a bot (e.g. a CI bot), the $HOME/.gradle is automatically deleted.
///
/// See also:
/// * https://github.com/flutter/flutter/issues/51195
/// * https://github.com/flutter/flutter/issues/89959
/// * https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home
@visibleForTesting
final GradleHandledError zipExceptionHandler = GradleHandledError(
test: _lineMatcher(const <String>[
'java.util.zip.ZipException: error in opening zip file',
]),
handler: ({
required String line,
required FlutterProject project,
required bool usesAndroidX,
required bool multidexEnabled,
}) async {
globals.printError(
'${globals.logger.terminal.warningMark} '
'Your .gradle directory under the home directory might be corrupted.'
);
bool shouldDeleteUserGradle = await globals.botDetector.isRunningOnBot;
if (!shouldDeleteUserGradle && globals.terminal.stdinHasTerminal) {
try {
final String selection = await globals.terminal.promptForCharInput(
<String>['y', 'n'],
logger: globals.logger,
prompt: 'Do you want to delete the .gradle directory under the home directory?',
defaultChoiceIndex: 0,
);
shouldDeleteUserGradle = selection == 'y';
} on StateError catch(e) {
globals.printError(
e.message,
indent: 0,
);
}
}
if (shouldDeleteUserGradle) {
final String? homeDir = globals.platform.environment['HOME'];
if (homeDir == null) {
globals.logger.printStatus("Could not delete .gradle directory because there isn't a HOME env variable");
return GradleBuildStatus.retry;
}
final Directory userGradle = globals.fs.directory(globals.fs.path.join(homeDir, '.gradle'));
globals.logger.printStatus('Deleting ${userGradle.path}');
try {
ErrorHandlingFileSystem.deleteIfExists(userGradle, recursive: true);
} on FileSystemException catch (err) {
globals.printTrace('Failed to delete Gradle cache: $err');
}
}
return GradleBuildStatus.retry;
},
eventLabel: 'zip-exception',
);
// R8 failure.
@visibleForTesting
final GradleHandledError r8FailureHandler = GradleHandledError(