rules_kotlin icon indicating copy to clipboard operation
rules_kotlin copied to clipboard

Support generating Java code with KSP

Open lukaciko opened this issue 2 years ago • 1 comments

As noted in the README, kt_ksp_plugin doesn't support generating Java code. Some annotation processors, like Dagger 2.48+, generate Java code and support KSP.


When implemented, updating examples/dagger to use KSP and building it should work:

diff --git a/examples/dagger/WORKSPACE b/examples/dagger/WORKSPACE
index 213945f..d74fe47 100644
--- a/examples/dagger/WORKSPACE
+++ b/examples/dagger/WORKSPACE
@@ -31,9 +31,9 @@ load("@rules_jvm_external//:defs.bzl", "maven_install")

 maven_install(
     artifacts = [
-        "com.google.dagger:dagger:2.45",
-        "com.google.dagger:dagger-compiler:2.45",
-        "com.google.dagger:dagger-producers:2.45",
+        "com.google.dagger:dagger:2.48",
+        "com.google.dagger:dagger-compiler:2.48",
+        "com.google.dagger:dagger-producers:2.48",
         "javax.inject:javax.inject:1",
         "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2",
     ],
diff --git a/examples/dagger/third_party/BUILD.bazel b/examples/dagger/third_party/BUILD.bazel
index e970b92..53ceec0 100644
--- a/examples/dagger/third_party/BUILD.bazel
+++ b/examples/dagger/third_party/BUILD.bazel
@@ -1,8 +1,8 @@
-load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
+load("@rules_kotlin//kotlin:core.bzl", "kt_ksp_plugin")
+load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

-java_plugin(
+kt_ksp_plugin(
     name = "dagger_component_plugin",
-    generates_api = True,
     processor_class = "dagger.internal.codegen.ComponentProcessor",
     visibility = ["//visibility:private"],
     deps = [
@@ -10,9 +10,9 @@ java_plugin(
     ],
 )

-java_library(
+kt_jvm_library(
     name = "dagger",
-    exported_plugins = [":dagger_component_plugin"],
+    plugins = [":dagger_component_plugin"],
     visibility = ["//visibility:public"],
     exports = [
         "@maven//:com_google_dagger_dagger",

cd examples/dagger
bazelisk build coffee_app

lukaciko avatar Nov 16 '23 09:11 lukaciko

Oh cool! Dagger has initial KSP support. That's news to me. It does present a very tricky aspect, in that dagger still generates java, even when running in KSP. That's not a use-case I would have predicted.

cgruber avatar Nov 28 '23 20:11 cgruber