From e888495386d11e13a7bccbb5af012683462d1abc Mon Sep 17 00:00:00 2001 From: Oleksandr Karpovich Date: Tue, 8 Aug 2023 14:21:57 +0200 Subject: [PATCH] Handle ClassCastException for ComposeMultiplatformBuildService in Compose Gradle plugin (#3467) * Handle ClassCastException for ComposeMultiplatformBuildService in Compose Gradle plugin ClassCastException might occur when our gradle plugin was loaded more than once. See https://github.com/JetBrains/compose-multiplatform/issues/3459 We throw another exception with a bit more clear message. * Update gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt --- .../ComposeCompilerKotlinSupportPlugin.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt index 7cd7f229f3..caeac692ef 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt @@ -13,6 +13,7 @@ import org.jetbrains.compose.internal.mppExtOrNull import org.jetbrains.compose.internal.webExt import org.jetbrains.kotlin.gradle.plugin.* import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget +import java.lang.ClassCastException import javax.inject.Inject @Suppress("UnstableApiUsage") @@ -50,7 +51,21 @@ class ComposeCompilerKotlinSupportPlugin @Inject constructor( val service = ComposeMultiplatformBuildService.provider(target) buildEventsListenerRegistry.onTaskCompletion(service) - service.get().parameters.unsupportedCompilerPlugins.add( + val providedService = try { + service.get() + } catch (e: ClassCastException) { + // Compose Gradle plugin was probably loaded more than once + // See https://github.com/JetBrains/compose-multiplatform/issues/3459 + + throw IllegalStateException( + "Failed to get ComposeMultiplatformBuildService instance." + + " Compose Gradle plugin was probably loaded more than once." + + " Consider declaring it in the root build.gradle.kts", + e + ) + } + + providedService.parameters.unsupportedCompilerPlugins.add( target.provider { composeCompilerArtifactProvider.compilerArtifact.takeIf { target.hasNonJvmTargets() && it.isNonJBComposeCompiler()