Skip to content

Commit e21bc4d

Browse files
Kotlin 2.4.0: Fix plugin service file to be version-conditional
The CompilerPluginRegistrar service file must only be included in the 2.4.0 jar. Older Kotlin versions (2.3.x and below) read this service file and try to cast the class to CompilerPluginRegistrar, but the older version extractor only implements ComponentRegistrar, causing a ClassCastException at runtime. For 2.4.0, the registrar implements both ComponentRegistrar (no-op, as extensionArea was removed) and CompilerPluginRegistrar (actual registration via ExtensionStorage). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4a2f244 commit e21bc4d

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

java/kotlin-extractor/BUILD.bazel

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ _resources = [
6464
r[len("src/main/resources/"):],
6565
)
6666
for r in glob(["src/main/resources/**"])
67+
if r != "src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar"
6768
]
6869

70+
_compiler_plugin_registrar_service = (
71+
"src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar",
72+
"META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar",
73+
)
74+
6975
kt_javac_options(
7076
name = "javac-options",
7177
release = "8",
@@ -91,19 +97,26 @@ kt_javac_options(
9197
# * `resource_strip_prefix` is unique per jar, so we must also put other resources under the same version prefix
9298
genrule(
9399
name = "resources-%s" % v,
94-
srcs = [src for src, _ in _resources],
100+
srcs = [src for src, _ in _resources] + (
101+
[_compiler_plugin_registrar_service[0]] if not version_less(v, "2.4.0") else []
102+
),
95103
outs = [
96104
"%s/com/github/codeql/extractor.name" % v,
97105
] + [
98106
"%s/%s" % (v, target)
99107
for _, target in _resources
100-
],
108+
] + (
109+
["%s/%s" % (v, _compiler_plugin_registrar_service[1])] if not version_less(v, "2.4.0") else []
110+
),
101111
cmd = "\n".join([
102112
"echo %s-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (_extractor_name_prefix, v, v),
103113
] + [
104114
"cp $(execpath %s) $(RULEDIR)/%s/%s" % (source, v, target)
105115
for source, target in _resources
106-
]),
116+
] + (
117+
["cp $(execpath %s) $(RULEDIR)/%s/%s" % (_compiler_plugin_registrar_service[0], v, _compiler_plugin_registrar_service[1])]
118+
if not version_less(v, "2.4.0") else []
119+
)),
107120
),
108121
kt_jvm_library(
109122
name = "%s-%s" % (_extractor_name_prefix, v),

java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_4_0/Kotlin2ComponentRegistrar.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1-
@file:Suppress("DEPRECATION")
1+
@file:Suppress("DEPRECATION", "DEPRECATION_ERROR")
22
@file:OptIn(ExperimentalCompilerApi::class)
33

44
package com.github.codeql
55

6+
import com.intellij.mock.MockProject
67
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
8+
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
79
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
810
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
911
import org.jetbrains.kotlin.config.CompilerConfiguration
1012

11-
abstract class Kotlin2ComponentRegistrar : CompilerPluginRegistrar() {
13+
abstract class Kotlin2ComponentRegistrar : CompilerPluginRegistrar(), ComponentRegistrar {
1214
override val supportsK2: Boolean
1315
get() = true
1416

1517
override val pluginId: String
1618
get() = "com.github.codeql.kotlin-extractor"
1719

20+
// ComponentRegistrar implementation (legacy path, still called by Kotlin compiler)
21+
override fun registerProjectComponents(
22+
project: MockProject,
23+
configuration: CompilerConfiguration
24+
) {
25+
// In 2.4.0, we use CompilerPluginRegistrar path instead.
26+
// This is only called if the compiler uses the ComponentRegistrar service file.
27+
// We do nothing here since registerExtensions will be called separately.
28+
}
29+
1830
private var extensionStorage: CompilerPluginRegistrar.ExtensionStorage? = null
31+
private var registeredExtension: IrGenerationExtension? = null
1932

2033
override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
2134
this@Kotlin2ComponentRegistrar.extensionStorage = this

0 commit comments

Comments
 (0)