rules_kotlin
rules_kotlin copied to clipboard
KSP subplugin conflicts with guava in @com_github_jetbrains_kotlin//lib/kotlin-compiler.jar
Description:
There are some guava classes in Kotlin-compiler, such as com.google .common.graph, if guava is also used in the subplugin of ksp, then because the parent delegation model of java will go to the father, that is, the guava in MemoryBasedClassLoader, which will lead to
Exception: java.lang. LinkageError: loader constraint violation: loader'app 'wants to load interface com.google common.base. Supplier. A different interface with the same name was previously loaded by org.jetbrains.kotlin.preloading.MemoryBasedClassLoader @4bbfb90a. (com.google common.base. Supplier is in unnamed module of loaderorg.jetbrains.kotlin.preloading.MemoryBasedClassLoader @4bbfb90a, parent loader'bootstrap')
The same error occurs with compile ksp in cli mode
Workaround: Essentially, Kotlinc should solve the problem of isolation from the KSP environment, but considering that rules_kotlin builders currently have a relatively complete guava, I filtered out guava in the MemoryBasedClassLoader load condition, but this solution does not look elegant, and I would like to discuss how to deal with it.
diff --git a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt
index 18ea6c1..0455481 100644
--- a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt
+++ b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt
@@ -20,6 +20,7 @@ import io.bazel.kotlin.builder.utils.BazelRunFiles
import io.bazel.kotlin.builder.utils.resolveVerified
import io.bazel.kotlin.builder.utils.verified
import io.bazel.kotlin.builder.utils.verifiedPath
+import org.jetbrains.kotlin.preloading.ClassCondition
import org.jetbrains.kotlin.preloading.ClassPreloadingUtils
import org.jetbrains.kotlin.preloading.Preloader
import java.io.File
@@ -190,7 +191,12 @@ class KotlinToolchain private constructor(
},
Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE,
classLoader,
- null,
+ // null,
+ object: ClassCondition {
+ override fun accept(className: String): Boolean {
+ return className.startsWith("com.google.common.")
+ }
+ }
)
}.onFailure {
throw RuntimeException("$javaHome, $baseJars", it)
this still repros in the rules_kotlin example app for KSP that uses dagger on 40fecf58:
cd examples/ksp && bazelisk build //:coffee_app
with this patch applied to use the latest rules
diff --git a/examples/ksp/WORKSPACE b/examples/ksp/WORKSPACE
index a728fb17..d6aa2795 100644
--- a/examples/ksp/WORKSPACE
+++ b/examples/ksp/WORKSPACE
@@ -4,8 +4,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_kotlin",
- sha256 = "34e8c0351764b71d78f76c8746e98063979ce08dcf1a91666f3f3bc2949a533d",
- url = "https://github.com/bazelbuild/rules_kotlin/releases/download/v1.9.5/rules_kotlin-v1.9.5.tar.gz",
+ sha256 = "d89723cc9ebbb7bdb2ebaca1af7d2383e074615643cf97a366b758a76b7dc443",
+ url = "https://github.com/bazelbuild/rules_kotlin/releases/download/v2.0.0/rules_kotlin-v2.0.0.tar.gz",
)
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
diff --git a/examples/ksp/third_party/BUILD.bazel b/examples/ksp/third_party/BUILD.bazel
index 4af98cda..c6786fed 100644
--- a/examples/ksp/third_party/BUILD.bazel
+++ b/examples/ksp/third_party/BUILD.bazel
@@ -72,9 +72,7 @@ kt_ksp_plugin(
kt_ksp_plugin(
name = "dagger_ksp_plugin",
- generates_java = True,
processor_class = "dagger.internal.codegen.KspComponentProcessor",
- target_embedded_compiler = True,
visibility = ["//visibility:public"],
deps = ["@maven//:com_google_dagger_dagger_compiler"],
)