Skip to content

Commit d507db8

Browse files
committed
Add a workaround for KT-72933
1 parent ef73b87 commit d507db8

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import sun.misc.Unsafe
6+
import java.lang.reflect.Field
7+
8+
@Suppress("UNCHECKED_CAST")
9+
internal object ConfigurationCacheWorkarounds {
10+
private val ignoreBeanFieldsField = Class.forName("org.gradle.internal.serialize.beans.services.Workarounds")
11+
.getDeclaredField("ignoredBeanFields")
12+
.apply { isAccessible = true }
13+
14+
private val unsafe = runCatching {
15+
Unsafe::class.java.getDeclaredField("theUnsafe")
16+
.apply { isAccessible = true }
17+
.get(null) as Unsafe
18+
}.getOrNull()
19+
20+
private var ignoreBeanFields = ignoreBeanFieldsField.get(null) as Array<Pair<String, String>>
21+
set(value) {
22+
val isSet = runCatching { unsafe?.setFinalStatic(ignoreBeanFieldsField, value) }.getOrNull() != null
23+
if (isSet) {
24+
field = value
25+
} else {
26+
// If we can't set a new array to the field, fallback to replacing array's content with new values
27+
for (i in 0..minOf(field.lastIndex, value.lastIndex)) field[i] = value[i]
28+
}
29+
}
30+
31+
/** Registers fields that should be excluded from configuration cache. */
32+
fun addIgnoredBeanFields(vararg fields: Pair<String, String>) {
33+
ignoreBeanFields = fields as Array<Pair<String, String>> + ignoreBeanFields
34+
}
35+
}
36+
37+
@Suppress("DEPRECATION")
38+
private fun Unsafe.setFinalStatic(field: Field, value: Any) {
39+
val fieldBase = staticFieldBase(field)
40+
val fieldOffset = staticFieldOffset(field)
41+
42+
putObject(fieldBase, fieldOffset, value)
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import org.gradle.kotlin.dsl.support.serviceOf
6+
7+
val features = serviceOf<BuildFeatures>()
8+
9+
if (features.configurationCache.requested.get()) {
10+
// KT-72933: Storing these fields leads to OOM
11+
ConfigurationCacheWorkarounds.addIgnoredBeanFields(
12+
"transformationParameters" to "org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyTransformationTask",
13+
"parameters" to "org.jetbrains.kotlin.gradle.targets.native.internal.CInteropMetadataDependencyTransformationTask",
14+
)
15+
16+
// Mark these tasks as incompatible with configuration cache as we partially exclude their state from CC
17+
gradle.beforeProject {
18+
tasks.matching { it::class.java.simpleName.contains("MetadataDependencyTransformationTask") }
19+
.configureEach { notCompatibleWithConfigurationCache("Workaround for KT-72933") }
20+
}
21+
22+
println("Configuration Cache: Workaround for KT-72933 was applied")
23+
}

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ kotlin.mpp.applyDefaultHierarchyTemplate=false
5656
kotlin.apple.xcodeCompatibility.nowarn=true
5757
kotlin.suppressGradlePluginWarnings=IncorrectCompileOnlyDependencyWarning
5858
kotlin.daemon.useFallbackStrategy=false
59+
# Enable new project model to be prepared for enabling isolated projects
60+
# TODO: Remove when we enable isolated projects in Gradle
61+
kotlin.kmp.isolated-projects.support=enable
5962

6063
# dokka
6164
# workaround for resolving platform dependencies, see https://github.com/Kotlin/dokka/issues/3153

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
id("org.gradle.toolchains.foojay-resolver-convention") version "0.9.0"
1313
id("conventions-dependency-resolution-management")
1414
id("conventions-develocity")
15+
id("ktorbuild.configuration-cache")
1516
}
1617

1718
rootProject.name = "ktor"

0 commit comments

Comments
 (0)