Skip to content

Commit e066796

Browse files
aSemysksamuel
andauthored
Monkey patch EmbeddedKotlinPlugin to disable 'Unsupported Kotlin plugin version' warning (kotest#4301)
Workaround for gradle/gradle#13020 Gradle _always_ logs an annoying warning: Unsupported Kotlin plugin version. The warning is always logged, no matter what, and it doesn't ever seem to represent an actual problem. So, this code downloads the original EmbeddedKotlinPlugin and disables the warning. Because Gradle's classloader is hierarchical, the original will be replaced. --------- Co-authored-by: Sam <[email protected]>
1 parent 1ac7575 commit e066796

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

buildSrc/build.gradle.kts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
import org.gradle.api.attributes.DocsType.DOCS_TYPE_ATTRIBUTE
4+
import org.gradle.api.attributes.DocsType.SOURCES
5+
import org.gradle.kotlin.dsl.support.expectedKotlinDslPluginsVersion
6+
import org.gradle.kotlin.dsl.support.serviceOf
7+
18
plugins {
29
`kotlin-dsl`
310
}
@@ -13,3 +20,82 @@ tasks.withType<AbstractArchiveTask>().configureEach {
1320
isPreserveFileTimestamps = false
1421
isReproducibleFileOrder = true
1522
}
23+
24+
//region workaround for https://github.com/gradle/gradle/issues/13020
25+
// Gradle _always_ logs an annoying warning: Unsupported Kotlin plugin version.
26+
// The warning is always logged, no matter what, and it doesn't ever seem to represent an actual problem.
27+
// So, this code downloads the original EmbeddedKotlinPlugin and disables the warning.
28+
// Because Gradle's classloader is hierarchical, the original will be replaced.
29+
30+
val kotlinDslPluginSources: Configuration by configurations.creating {
31+
description = "Download the original Gradle Kotlin DSL plugin source code."
32+
isCanBeConsumed = false
33+
isCanBeResolved = false
34+
isCanBeDeclared = true
35+
isVisible = false
36+
defaultDependencies {
37+
add(project.dependencies.create("org.gradle.kotlin:gradle-kotlin-dsl-plugins:$expectedKotlinDslPluginsVersion"))
38+
}
39+
}
40+
41+
val kotlinDslPluginSourcesResolver: Configuration by configurations.creating {
42+
description = "Resolve files from ${kotlinDslPluginSources.name}."
43+
isCanBeConsumed = false
44+
isCanBeResolved = true
45+
isCanBeDeclared = false
46+
isVisible = false
47+
extendsFrom(kotlinDslPluginSources)
48+
attributes {
49+
attribute(DOCS_TYPE_ATTRIBUTE, objects.named(SOURCES))
50+
}
51+
}
52+
53+
val fixGradlePluginWarning by tasks.registering {
54+
description = "Download EmbeddedKotlinPlugin.kt and patch it to disable the warning."
55+
56+
val src = kotlinDslPluginSourcesResolver.incoming.files
57+
inputs.files(src).withNormalizer(ClasspathNormalizer::class)
58+
59+
outputs.dir(temporaryDir).withPropertyName("outputDir")
60+
61+
val archives = serviceOf<ArchiveOperations>()
62+
63+
doLast {
64+
val embeddedKotlinPlugin = src.flatMap { s ->
65+
archives.zipTree(s).matching {
66+
include("**/EmbeddedKotlinPlugin.kt")
67+
}
68+
}.firstOrNull()
69+
70+
if (embeddedKotlinPlugin == null) {
71+
// If EmbeddedKotlinPlugin.kt can't be found then maybe this workaround
72+
// is no longer necessary, or it needs to be updated.
73+
logger.warn("[$path] could not find EmbeddedKotlinPlugin.kt in $src")
74+
} else {
75+
logger.info("[$path] Patching EmbeddedKotlinPlugin.kt to remove 'Unsupported Kotlin plugin version' warning")
76+
temporaryDir.deleteRecursively()
77+
temporaryDir.mkdirs()
78+
temporaryDir.resolve(embeddedKotlinPlugin.name).apply {
79+
writeText(
80+
embeddedKotlinPlugin.readText()
81+
// This is the key change: converting 'warn' into 'info'.
82+
.replace("\n warn(\n", "\n info(\n")
83+
// Mark internal things as internal to prevent compiler warnings about unused code,
84+
// and to stop them leaking into build scripts.
85+
.replace("\n\nfun Logger.", "\n\nprivate fun Logger.")
86+
.replace(
87+
"*/\nabstract class EmbeddedKotlinPlugin",
88+
"*/\ninternal abstract class EmbeddedKotlinPlugin"
89+
)
90+
)
91+
}
92+
}
93+
}
94+
}
95+
96+
sourceSets {
97+
main {
98+
kotlin.srcDir(fixGradlePluginWarning)
99+
}
100+
}
101+
//endregion

0 commit comments

Comments
 (0)