ZacSweers/kotlin-compile-testing

Compilation doesn't fail when ksp processor is added

evant opened this issue · 1 comments

evant commented

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.

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.