ZacSweers / kotlin-compile-testing

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

0.5.0 Generated Sources Not Produced #257

Open austinarbor opened 3 months ago

austinarbor commented 3 months ago

After upgrading to 0.5.0, my KSP Generated sources are no longer produced

class MyTests {
@TempDir @JvmField var tempDir: Path = Paths.get(".")

fun compile(vararg sourceFiles: SourceFile): JvmCompilationResult {
 val compilation =
        KotlinCompilation().apply {
          workingDir = tempDir.toFile()
          sources = sourceFiles.toList()
          inheritClassPath = true
          symbolProcessorProviders = mutableListOf(ExposedProcessorProvider())
          kspIncremental = false
        }
    val pass1 = compilation.compile()
    assertThat(pass1.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
    // https://github.com/tschuchortdev/kotlin-compile-testing/issues/72#issuecomment-744475289
    return KotlinCompilation()
        .apply {
          inheritClassPath = true
          sources = compilation.sources + compilation.kspGeneratedSourceFiles
        }
        .compile()

}

private val KotlinCompilation.kspGeneratedSourceFiles: List<SourceFile>
    get() =
        kspSourcesDir
            .resolve("kotlin")
            .walk()
            .filter { it.isFile }
            .map { SourceFile.fromPath(it.absoluteFile) }
            .toList()

When running this compilation, none of the generated outputs are produced in the test I am using Kotlin 2.0.0 KSP 2.0.0-1.0.22, with ksp.useKSP=true set in gradle.properties. If I downgrade to 0.4.1, everything succeeds as expected. I didn't see any migration steps in the release notes, am I missing something?

I can try and create a reproducer if you need one, but wanted to check if I was missing anything obvious before doing so

Zweistein2 commented 2 months ago

I have the same issue. Compilation seems to be successfull (ExitCode.OK), but no ksp-generated files exist.

rossbacher commented 2 months ago

You have to set the languageVersion to "1.9" if you are still using KSP 1, see https://github.com/ZacSweers/kotlin-compile-testing/pull/196#issuecomment-2169240442

Zweistein2 commented 2 months ago

I'm using KSP2 already

austinarbor commented 2 months ago

I am using KSP2 as well

Zweistein2 commented 2 months ago

@austinarbor I found out how to get KSP2 running. You have to use the configureKsp-block like this:

val compilation = KotlinCompilation().apply {
  sources = sourceFiles
  configureKsp(useKsp2 = true) {
    symbolProcessorProviders.add(...)
  }
}

after adding this, it started to create ksp-generated files again.

austinarbor commented 2 months ago

@Zweistein2 thanks for this! confirmed working for me as well

galex commented 1 week ago

As of right now by default no code is generated, so languageVersion = "1.9" should be the default value.