tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Class files are not generated

bipokot opened this issue · comments

I'm using this library to test KSP processor and I need to access class files generated by Kotlin compiler (compiled original source files and compiled generated source files).
It turns out that compilation successfully completes but classes directories (root and in ksp subdirectory) are empty.

Yes, this seems to be the case. For some reason, it works for other configurations but not with KSP. I will look into it when I have time.

Ran into that issue too, so I employed a workaround: I just run KotlinCompilation one more time adding ksp-generated sources as inputs - works fine for me :)

@Jeffset Can you show an example of that?

@tschuchortdev Can you point me in the right direction where the change might need to take place? I'd be happy to try and submit a PR. This is a blocker for me

Finding where to make the change should be pretty easy as the library is small (there is only one Ksp.kt file). The question is what to change. The KSP integration is not very complex, it just declares a ComponentRegistrar (i.e. compiler plugin) that registers a subclass of the KSP extension which loads KSP processors from a given list instead of finding them dynamically. So the most likely culprits are:

  1. Some option for the KSP extension is missing that enables class file generation or there is an additional KSP extension that has to be registered in a different compiler extension point (not AnalysisHandlerExtension)
  2. It's not a bug™ and the KSP extension only generates new source files by design, stopping the Kotlin compiler before it gets to the actual compiling/class file generation. Maybe the KSP Gradle plugin registers a new Gradle task that calls the Kotlin compiler with the KSP compiler plugin and then lets the regular "compile Kotlin" task depend on the generated source files, instead of changing the regular "compile Kotlin" task to use the KSP compiler plugin. In that way, the Kotlin compiler would be called twice (first with KSP, then without). To find out if that is true you should look at the source code of the KSP Gradle plugin or you could ask @yigit and @ZacSweers who are always helpful and might know it since they contributed to that file. In this case, the "workaround" that @Jeffset came up with is exactly what we need to implement.

After a moment of code reading, I found that you should set the KspOptions.withCompilation to true to get the code compiled with symbol processors in one invokation. See: KspCliOption.