[Gradle] Extract task outputs backup restore conditions

This commit is contained in:
Alexander.Likhachev
2023-10-18 01:19:42 +02:00
committed by Space Team
parent 664b54de5f
commit 65f6f6a07f
4 changed files with 31 additions and 17 deletions
@@ -79,13 +79,12 @@ internal class GradleCompilerRunnerWithWorkers(
// In the other cases where there is nothing the user can fix in their project, we should not restore the outputs.
// Otherwise, the next build(s) will likely fail in exactly the same way as this build because their inputs and outputs are
// the same.
if (taskOutputsBackup != null && (e is CompilationErrorException || e is OOMErrorException)) {
taskOutputsBackup?.tryRestoringOnRecoverableException(e) { restoreAction ->
parameters.metricsReporter.get().measure(GradleBuildTime.RESTORE_OUTPUT_FROM_BACKUP) {
logger.info("Restoring task outputs to pre-compilation state")
taskOutputsBackup.restoreOutputs()
logger.info(DEFAULT_BACKUP_RESTORE_MESSAGE)
restoreAction()
}
}
throw e
} finally {
taskOutputsBackup?.deleteSnapshot()
@@ -253,12 +253,11 @@ internal open class GradleCompilerRunner(
kotlinCompilerRunnable.run()
} catch (e: FailedCompilationException) {
// Restore outputs only for CompilationErrorException or OOMErrorException (see GradleKotlinCompilerWorkAction.execute)
if (taskOutputsBackup != null && (e is CompilationErrorException || e is OOMErrorException)) {
taskOutputsBackup?.tryRestoringOnRecoverableException(e) { restoreAction ->
buildMetrics.measure(GradleBuildTime.RESTORE_OUTPUT_FROM_BACKUP) {
taskOutputsBackup.restoreOutputs()
restoreAction()
}
}
throw e
}
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.gradle.plugin.BuildFinishedListenerService
import org.jetbrains.kotlin.gradle.plugin.internal.BuildIdService
import org.jetbrains.kotlin.gradle.plugin.internal.state.TaskLoggers
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.CompilationErrorException
import org.jetbrains.kotlin.gradle.tasks.FailedCompilationException
import org.jetbrains.kotlin.gradle.tasks.OOMErrorException
import org.jetbrains.kotlin.gradle.tasks.TaskOutputsBackup
@@ -171,15 +170,9 @@ internal abstract class BuildToolsApiCompilationWork @Inject constructor(
}
throwExceptionIfCompilationFailed(result.asExitCode, executionStrategy)
} catch (e: FailedCompilationException) {
// Restore outputs only in cases where we expect that the user will make some changes to their project:
// - For a compilation error, the user will need to fix their source code
// - For an OOM error, the user will need to increase their memory settings
// In the other cases where there is nothing the user can fix in their project, we should not restore the outputs.
// Otherwise, the next build(s) will likely fail in exactly the same way as this build because their inputs and outputs are
// the same.
if (backup != null && (e is CompilationErrorException || e is OOMErrorException)) {
log.info("Restoring task outputs to pre-compilation state")
backup.restoreOutputs()
backup?.tryRestoringOnRecoverableException(e) { restoreAction ->
log.info(DEFAULT_BACKUP_RESTORE_MESSAGE)
restoreAction()
}
throw e
} finally {
@@ -166,3 +166,26 @@ internal class TaskOutputsBackup(
private val Int.asSnapshotDirectoryName: String
get() = "$this"
}
internal fun interface BackupRestoreWrapper {
fun wrap(restoreAction: () -> Unit)
}
internal fun TaskOutputsBackup.tryRestoringOnRecoverableException(
e: FailedCompilationException,
restoreWrapper: BackupRestoreWrapper,
) {
// Restore outputs only in cases where we expect that the user will make some changes to their project:
// - For a compilation error, the user will need to fix their source code
// - For an OOM error, the user will need to increase their memory settings
// In the other cases where there is nothing the user can fix in their project, we should not restore the outputs.
// Otherwise, the next build(s) will likely fail in exactly the same way as this build because their inputs and outputs are
// the same.
if (e is CompilationErrorException || e is OOMErrorException) {
restoreWrapper.wrap {
restoreOutputs()
}
}
}
internal const val DEFAULT_BACKUP_RESTORE_MESSAGE = "Restoring task outputs to pre-compilation state"