Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generated java sources from KSP not getting compiled #172

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {
val kaptStubsDir get() = kaptBaseDir.resolve("stubs")
val kaptIncrementalDataDir get() = kaptBaseDir.resolve("incrementalData")

private val extraGeneratedSources = mutableListOf<File>()

/** Registers extra directories with generated sources, such as sources generated by KSP. */
fun registerGeneratedSourcesDir(dir: File) {
extraGeneratedSources.add(dir)
}

/** ExitCode of the entire Kotlin compilation process */
enum class ExitCode {
OK, INTERNAL_ERROR, COMPILATION_ERROR, SCRIPT_EXECUTION_ERROR
Expand Down Expand Up @@ -473,8 +480,11 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {

/** Performs the 4th compilation step to compile Java source files */
private fun compileJava(sourceFiles: List<File>): ExitCode {
val javaSources = (sourceFiles + kaptSourceDir.listFilesRecursively())
.filterNot<File>(File::hasKotlinFileExtension)
val javaSources = sourceFiles
.plus(kaptSourceDir.listFilesRecursively())
.plus(extraGeneratedSources.flatMap(File::listFilesRecursively))
.distinct()
.filterNot(File::hasKotlinFileExtension)

if(javaSources.isEmpty())
return ExitCode.OK
Expand Down Expand Up @@ -532,11 +542,12 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {
val diagnosticCollector = DiagnosticCollector<JavaFileObject>()

fun printDiagnostics() = diagnosticCollector.diagnostics.forEach { diag ->
// Print toString() for these to get the full error message
when(diag.kind) {
Diagnostic.Kind.ERROR -> error(diag.getMessage(null))
Diagnostic.Kind.ERROR -> error(diag.toString())
Diagnostic.Kind.WARNING,
Diagnostic.Kind.MANDATORY_WARNING -> warn(diag.getMessage(null))
else -> log(diag.getMessage(null))
Diagnostic.Kind.MANDATORY_WARNING -> warn(diag.toString())
else -> log(diag.toString())
}
}

Expand All @@ -557,7 +568,7 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {
ExitCode.COMPILATION_ERROR
}
catch (e: Exception) {
if(e is RuntimeException || e is IllegalArgumentException) {
if (e is RuntimeException) {
printDiagnostics()
error(e.toString())
return ExitCode.INTERNAL_ERROR
Expand Down
3 changes: 2 additions & 1 deletion ksp/src/main/kotlin/com/tschuchort/compiletesting/Ksp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private class KspCompileTestingComponentRegistrar(
this.javaOutputDir = compilation.kspJavaSourceDir.also {
it.deleteRecursively()
it.mkdirs()
compilation.registerGeneratedSourcesDir(it)
}
this.kotlinOutputDir = compilation.kspKotlinSourceDir.also {
it.deleteRecursively()
Expand Down Expand Up @@ -244,6 +245,6 @@ private fun KotlinCompilation.getKspRegistrar(): KspCompileTestingComponentRegis
return it
}
val kspRegistrar = KspCompileTestingComponentRegistrar(this)
componentRegistrars = componentRegistrars + kspRegistrar
componentRegistrars += kspRegistrar
return kspRegistrar
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,4 @@ internal open class AbstractTestSymbolProcessor(
override fun process(resolver: Resolver): List<KSAnnotated> {
return emptyList()
}
}

// Would be nice if SymbolProcessorProvider was a fun interface
internal fun processorProviderOf(
body: (environment: SymbolProcessorEnvironment) -> SymbolProcessor
): SymbolProcessorProvider {
return object : SymbolProcessorProvider {
override fun create(
environment: SymbolProcessorEnvironment
): SymbolProcessor {
return body(environment)
}
}
}
122 changes: 102 additions & 20 deletions ksp/src/test/kotlin/com/tschuchort/compiletesting/KspTest.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.tschuchort.compiletesting

import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.inOrder
import com.nhaarman.mockitokotlin2.mock
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import java.util.Locale
import java.util.concurrent.atomic.AtomicInteger
import kotlin.text.Typography.ellipsis
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mockito.`when`
import java.util.concurrent.atomic.AtomicInteger
import kotlin.text.Typography.ellipsis

@RunWith(JUnit4::class)
class KspTest {
Expand Down Expand Up @@ -95,11 +99,11 @@ class KspTest {
)
val result = KotlinCompilation().apply {
sources = listOf(annotation, targetClass)
symbolProcessorProviders = listOf(processorProviderOf { env ->
symbolProcessorProviders = listOf(SymbolProcessorProvider { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
val symbols = resolver.getSymbolsWithAnnotation("foo.bar.TestAnnotation").toList()
if (symbols.isNotEmpty()) {
if (symbols.isNotEmpty()) {
assertThat(symbols.size).isEqualTo(1)
val klass = symbols.first()
check(klass is KSClassDeclaration)
Expand All @@ -110,11 +114,13 @@ class KspTest {
dependencies = Dependencies.ALL_FILES,
packageName = genPackage,
fileName = genClassName
).bufferedWriter(Charsets.UTF_8).use {
it.write("""
).bufferedWriter().use {
it.write(
"""
package $genPackage
class $genClassName() {}
""".trimIndent())
""".trimIndent()
)
}
}
return emptyList()
Expand All @@ -140,10 +146,10 @@ class KspTest {
val result = KotlinCompilation().apply {
sources = listOf(source)
symbolProcessorProviders = listOf(
processorProviderOf { env -> ClassGeneratingProcessor(env.codeGenerator, "generated", "A") },
processorProviderOf { env -> ClassGeneratingProcessor(env.codeGenerator, "generated", "B") })
SymbolProcessorProvider { env -> ClassGeneratingProcessor(env.codeGenerator, "generated", "A") },
SymbolProcessorProvider { env -> ClassGeneratingProcessor(env.codeGenerator, "generated", "B") })
symbolProcessorProviders = symbolProcessorProviders +
processorProviderOf { env -> ClassGeneratingProcessor(env.codeGenerator, "generated", "C") }
SymbolProcessorProvider { env -> ClassGeneratingProcessor(env.codeGenerator, "generated", "C") }
}.compile()
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
}
Expand Down Expand Up @@ -179,7 +185,7 @@ class KspTest {
fun outputDirectoryContents() {
val compilation = KotlinCompilation().apply {
sources = listOf(DUMMY_KOTLIN_SRC)
symbolProcessorProviders = listOf(processorProviderOf { env ->
symbolProcessorProviders = listOf(SymbolProcessorProvider { env ->
ClassGeneratingProcessor(env.codeGenerator, "generated", "Gen")
})
}
Expand Down Expand Up @@ -212,7 +218,7 @@ class KspTest {
val result = mutableListOf<String>()
val compilation = KotlinCompilation().apply {
sources = listOf(javaSource, kotlinSource)
symbolProcessorProviders += processorProviderOf { env ->
symbolProcessorProviders += SymbolProcessorProvider { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getSymbolsWithAnnotation(
Expand Down Expand Up @@ -246,11 +252,13 @@ class KspTest {
dependencies = Dependencies.ALL_FILES,
packageName = packageName,
fileName = className
).bufferedWriter(Charsets.UTF_8).use {
it.write("""
).bufferedWriter().use {
it.write(
"""
package $packageName
class $className() {}
""".trimIndent())
""".trimIndent()
)
}
}
return emptyList()
Expand All @@ -274,7 +282,7 @@ class KspTest {
)
val result = KotlinCompilation().apply {
sources = listOf(annotation, targetClass)
symbolProcessorProviders = listOf(processorProviderOf { env ->
symbolProcessorProviders = listOf(SymbolProcessorProvider { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
env.logger.logging("This is a log message")
Expand Down Expand Up @@ -308,7 +316,7 @@ class KspTest {
)
val result = KotlinCompilation().apply {
sources = listOf(annotation, targetClass)
symbolProcessorProviders = listOf(processorProviderOf { env ->
symbolProcessorProviders = listOf(SymbolProcessorProvider { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
env.logger.error("This is an error message")
Expand Down Expand Up @@ -340,7 +348,7 @@ class KspTest {
)
val result = KotlinCompilation().apply {
sources = listOf(annotation, targetClass)
symbolProcessorProviders = listOf(processorProviderOf { env ->
symbolProcessorProviders = listOf(SymbolProcessorProvider { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
env.logger.logging("This is a log message with ellipsis $ellipsis")
Expand All @@ -357,6 +365,80 @@ class KspTest {
assertThat(result.messages).contains("This is an warn message with emoji 🔥")
}

// This test exercises both using withCompilation (for in-process compilation of generated sources)
// and generating Java sources (to ensure generated java files are compiled too)
@Test
fun withCompilationAndJavaTest() {
val annotation = SourceFile.kotlin(
"TestAnnotation.kt", """
package foo.bar
annotation class TestAnnotation
""".trimIndent()
)
val targetClass = SourceFile.kotlin(
"AppCode.kt", """
package foo.bar
@TestAnnotation
class AppCode
""".trimIndent()
)
val compilation = KotlinCompilation()
val result = compilation.apply {
sources = listOf(annotation, targetClass)
symbolProcessorProviders = listOf(SymbolProcessorProvider { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getSymbolsWithAnnotation("foo.bar.TestAnnotation")
.forEach { symbol ->
check(symbol is KSClassDeclaration) { "Expected class declaration" }
@Suppress("DEPRECATION")
val simpleName = "${symbol.simpleName.asString().capitalize(Locale.US)}Dummy"
env.codeGenerator.createNewFile(
dependencies = Dependencies.ALL_FILES,
packageName = "foo.bar",
fileName = simpleName,
extensionName = "java"
).bufferedWriter().use {
//language=JAVA
it.write(
"""
package foo.bar;

class ${simpleName}Java {

}
""".trimIndent()
)
}
env.codeGenerator.createNewFile(
dependencies = Dependencies.ALL_FILES,
packageName = "foo.bar",
fileName = "${simpleName}Kt",
extensionName = "kt"
).bufferedWriter().use {
//language=KOTLIN
it.write(
"""
package foo.bar

class ${simpleName}Kt {

}
""".trimIndent()
)
}
}
return emptyList()
}
}
})
kspWithCompilation = true
}.compile()
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
assertThat(result.classLoader.loadClass("foo.bar.AppCodeDummyJava")).isNotNull()
assertThat(result.classLoader.loadClass("foo.bar.AppCodeDummyKt")).isNotNull()
}

companion object {
private val DUMMY_KOTLIN_SRC = SourceFile.kotlin(
"foo.bar.Dummy.kt", """
Expand Down