square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.29k stars 75 forks source link

KSP mode leaks kotlin-compiler-embeddable into javac classpath somehow #935

Closed ZacSweers closed 3 months ago

ZacSweers commented 3 months ago

Repro: https://github.com/zacsweers/catchup

I have no idea why this happens, as it's not visible in the dependencies task either 🤔

> Task :app-scaffold:compileDebugJavaWithJavac
The following annotation processors are not incremental: kotlin-compiler-embeddable-1.9.22.jar (org.jetbrains.kotlin:kotlin-compiler-embeddable:1.9.22).
Make sure all annotation processors are incremental to improve your build speed.
ZacSweers commented 3 months ago

This is actually a false positive warning in AGP. Will link an issue if they accept. In the meantime, the below works around it by removing KSP artifacts from their task that computes these

tasks.withType<JavaPreCompileTask>()
  .configureEach {
    doFirst {
      // JavaPreCompileTask incorrectly reads annotation processors from the ksp classpath
      // and then warns about them ending up in the JavaCompile tasks even though they're
      // not on the classpath. This works around that by clearing out that field before it
      // tries to merge them in with annotationProcessorArtifacts.
      JavaPreCompileTask::class.java.getDeclaredField("kspProcessorArtifacts")
        .apply { isAccessible = true }
        .set(this, null)
    }
  }