From b1ec2eb18a8210bed9721de1a3da00a0dce185fb Mon Sep 17 00:00:00 2001 From: "Alexander.Likhachev" Date: Wed, 18 Oct 2023 01:23:30 +0200 Subject: [PATCH] [Gradle] Unify a bit logic of compiler execution exceptions wrapping --- .../GradleKotlinCompilerWork.kt | 8 +---- .../btapi/BuildToolsApiCompilationWork.kt | 14 +------- .../kotlin/gradle/tasks/tasksUtils.kt | 34 ++++++++++++++----- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt index 8218a5d8f5e..68e558af097 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt @@ -246,13 +246,7 @@ internal class GradleKotlinCompilerWork @Inject constructor( exitCodeFromProcessExitCode(log, res.get()) } catch (e: Throwable) { bufferingMessageCollector.flush(messageCollector) - if (e is OutOfMemoryError || e.hasOOMCause()) { - throw OOMErrorException(kotlinDaemonOOMHelperMessage) - } else if (e is RemoteException) { - throw DaemonCrashedException(e) - } else { - throw e - } + wrapAndRethrowCompilationException(KotlinCompilerExecutionStrategy.DAEMON, e) } finally { val memoryUsageAfterBuild = runCatching { daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get() }.getOrNull() diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt index 44726295622..d0e763328fe 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt @@ -28,7 +28,6 @@ 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.FailedCompilationException -import org.jetbrains.kotlin.gradle.tasks.OOMErrorException import org.jetbrains.kotlin.gradle.tasks.TaskOutputsBackup import org.jetbrains.kotlin.incremental.ClasspathChanges import org.slf4j.LoggerFactory @@ -130,18 +129,7 @@ internal abstract class BuildToolsApiCompilationWork @Inject constructor( workArguments.compilerArgs.toList(), ) } catch (e: Throwable) { - if (e is OutOfMemoryError || e.hasOOMCause()) { - val helpMessage = when (executionStrategy) { - KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage - KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage - else -> error("The \"$executionStrategy\" execution strategy is not supported by the Build Tools API") - } - throw OOMErrorException(helpMessage) - } else if (e is RemoteException) { - throw DaemonCrashedException(e) - } else { - throw e - } + wrapAndRethrowCompilationException(executionStrategy, e) } finally { log.info(executionStrategy.asFinishLogMessage) } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt index 4b5e589d4c0..26d47920da9 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.logging.kotlinDebug import org.jetbrains.kotlin.incremental.deleteDirectoryContents import org.jetbrains.kotlin.incremental.deleteRecursivelyOrThrow import java.io.File +import java.rmi.RemoteException /** Throws [FailedCompilationException] if compilation completed with [exitCode] != [ExitCode.OK]. */ fun throwExceptionIfCompilationFailed( @@ -20,14 +21,7 @@ fun throwExceptionIfCompilationFailed( ExitCode.COMPILATION_ERROR -> throw CompilationErrorException("Compilation error. See log for more details") ExitCode.INTERNAL_ERROR -> throw FailedCompilationException("Internal compiler error. See log for more details") ExitCode.SCRIPT_EXECUTION_ERROR -> throw FailedCompilationException("Script execution error. See log for more details") - ExitCode.OOM_ERROR -> { - val exceptionMessage = when (executionStrategy) { - KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage - KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage - KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> kotlinOutOfProcessOOMHelperMessage - } - throw OOMErrorException(exceptionMessage) - } + ExitCode.OOM_ERROR -> throw OOMErrorException(executionStrategy) ExitCode.OK -> Unit else -> throw IllegalStateException("Unexpected exit code: $exitCode") } @@ -49,6 +43,21 @@ internal fun Throwable.hasOOMCause(): Boolean = when (cause) { else -> cause?.hasOOMCause() ?: false } +/** + * Wraps an exception occurred during compiler execution. + * Covers the case when compiler invocation failed before returning any [ExitCode]. + * Always throws some kind of exception. + */ +internal fun wrapAndRethrowCompilationException(executionStrategy: KotlinCompilerExecutionStrategy, e: Throwable): Nothing { + if (e is OutOfMemoryError || e.hasOOMCause()) { + throw OOMErrorException(executionStrategy) + } else if (e is RemoteException) { + throw DaemonCrashedException(e) + } else { + throw e + } +} + /** Exception thrown when [ExitCode] != [ExitCode.OK]. */ internal open class FailedCompilationException(message: String, cause: Throwable? = null) : RuntimeException(message, cause) @@ -58,6 +67,15 @@ internal class CompilationErrorException(message: String) : FailedCompilationExc /** Exception thrown when [ExitCode] == [ExitCode.OOM_ERROR]. */ internal class OOMErrorException(message: String) : FailedCompilationException(message) +private fun OOMErrorException(executionStrategy: KotlinCompilerExecutionStrategy): OOMErrorException { + val exceptionMessage = when (executionStrategy) { + KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage + KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage + KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> kotlinOutOfProcessOOMHelperMessage + } + return OOMErrorException(exceptionMessage) +} + /** Exception thrown when during the compilation [java.rmi.RemoteException] is caught */ internal class DaemonCrashedException(cause: Throwable) : FailedCompilationException(kotlinDaemonCrashedMessage, cause)