From 6d29bb5814eee502a63fac2449c6e11a0305e529 Mon Sep 17 00:00:00 2001 From: Hung Nguyen Date: Thu, 29 Oct 2020 22:52:00 +0000 Subject: [PATCH] Fix configuration cache issue in AbstractKotlinTarget AbstractKotlinTarget#buildAdhocComponentsFromKotlinVariants passes a non-serializable local object to a lambda, which will fail during serialization when configuration caching is enabled. To fix that, this commit passes only the relevant parts of the object to the lambda. Bug: https://youtrack.jetbrains.com/issue/KT-43054 Test: Manually verified on https://android.googlesource.com/platform/tools/metalava/+/refs/heads/master/ where the issue was detected (I haven't been able to create a mininal reproducible project) --- .../kotlin/gradle/plugin/mpp/kotlinTargets.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinTargets.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinTargets.kt index 15a1cbc1ce2..e90ba73a194 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinTargets.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinTargets.kt @@ -125,14 +125,19 @@ abstract class AbstractKotlinTarget( adhocVariant as SoftwareComponent + // Resolve the values early here instead of passing `adhocVariant` to the SoftwareComponent object below (which would break + // configuration caching---see KT-43054). Passing `kotlinVariant` to the object did not break configuration caching, but we also + // avoid doing that just to be safe. + val coordinates = (kotlinVariant as? ComponentWithCoordinates)?.coordinates + val variants = (kotlinVariant as? KotlinVariantWithMetadataVariant)?.variants.orEmpty() + val name = adhocVariant.name + val usages = (adhocVariant as SoftwareComponentInternal).usages + object : ComponentWithVariants, ComponentWithCoordinates, SoftwareComponentInternal { - override fun getCoordinates() = (kotlinVariant as? ComponentWithCoordinates)?.coordinates - - override fun getVariants(): Set = - (kotlinVariant as? KotlinVariantWithMetadataVariant)?.variants.orEmpty() - - override fun getName(): String = adhocVariant.name - override fun getUsages(): MutableSet = (adhocVariant as SoftwareComponentInternal).usages + override fun getCoordinates() = coordinates + override fun getVariants(): Set = variants + override fun getName(): String = name + override fun getUsages(): MutableSet = usages } }.toSet() }