ZacSweers / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Mozilla Public License 2.0
110 stars 7 forks source link

Compilation doesn't fail when ksp processor is added #197

Open evant opened 11 months ago

evant commented 11 months ago

I would expect code that doesn't compile to continue not to compile when a ksp processor is added. For example:

val result = KotlinCompilation().apply {
  workingDir = this@RoundsTest.workingDir
  sources = listOf(SourceFile.kotlin("MyTest.kt", "class Bar(val foo: Foo)"))
  inheritClassPath = true
  messageOutputStream = System.out
}.compile()

fails with

e: file:///tmp/junit10613545484622425070/sources/MyTest.kt:1:20 Unresolved reference: Foo

but as soon as you add a processor that does nothing,

val result = KotlinCompilation().apply {
  workingDir = this@RoundsTest.workingDir
  sources = listOf(SourceFile.kotlin("MyTest.kt", "class Bar(val foo: Foo)"))
  symbolProcessorProviders = listOf(object : SymbolProcessorProvider {
    override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
      return object : SymbolProcessor {
        override fun process(resolver: Resolver): List<KSAnnotated> {
          return emptyList()
        }
      }
    }
  })
  inheritClassPath = true
  messageOutputStream = System.out
}.compile()

then compilation succeeds.

enabling

kspWithCompilation = true

does make it fail again, but I don't understand why that should impact this behavior.

ZacSweers commented 9 months ago

I think this is more or less expected behavior tbh. The default implementation from the origin project never compiled KSP-generated files unless kspWithCompilation was enabled, which forced KSP to run within the same KotlinCompilation. I think there's a limitation that causes this, either that it cannot do incremental processing or multiple rounds. The same situation exists in KSP2, KSP alone doesn't attempt to compile the generated sources.