KT-45168: Avoid generating stubs for generated Kotlin sources

Filter out Kotlin sources coming from KAPT-generated dirs.
Some annotation processors e.g Moshi, generate Kotlin sources,
and when generating stubs in non-incremental runs, stub generation
must ignore those sources. This fixes #KT-45168.

Test: Kapt3IT
This commit is contained in:
Ivan Gavrilovic
2021-02-25 21:15:48 +00:00
committed by nataliya.valtman
parent 34274dd032
commit 08446e2371
4 changed files with 33 additions and 9 deletions
@@ -24,6 +24,8 @@ import org.junit.Assume
import org.junit.Test
import java.io.File
import java.util.zip.ZipFile
import kotlin.test.assertNotNull
import kotlin.test.assertNull
abstract class Kapt3BaseIT : BaseGradleIT() {
companion object {
@@ -455,6 +457,20 @@ open class Kapt3IT : Kapt3BaseIT() {
assertContains("Additional warning message from AP")
}
project.build(options = defaultBuildOptions().copy(incremental = false), params = arrayOf("build")) {
assertSuccessful()
assertNull(
project.projectDir.findFileByName("TestGeneratedKt.java"),
"Java stubs should not be generated for Kotlin sources generated by annotation processors."
)
assertNotNull(project.projectDir.findFileByName("TestGeneratedKt.class"))
assertNull(
project.projectDir.findFileByName("AnotherGenerated.java"),
"Java stubs should not be generated for Kotlin sources generated by annotation processors."
)
assertNotNull(project.projectDir.findFileByName("AnotherGenerated.class"))
}
}
@Test
@@ -48,11 +48,19 @@ class TestAnnotationProcessor : AbstractProcessor() {
// print warning processingEnv.messager.printMessage(WARNING, "Additional warning message from AP")
processingEnv.filer
.createResource(StandardLocation.CLASS_OUTPUT, "abc", "helloWorld.txt")
.openWriter()
.use {
it.write("Hello, world!")
}
.createResource(
StandardLocation.SOURCE_OUTPUT,
"test.generated",
"AnotherGenerated.kt",
).openWriter().use {
it.write("package test.generated; class AnotherGenerated")
}
processingEnv.filer
.createResource(StandardLocation.CLASS_OUTPUT, "abc", "helloWorld.txt")
.openWriter()
.use {
it.write("Hello, world!")
}
return true
}
@@ -563,7 +563,7 @@ class Kapt3GradleSubplugin @Inject internal constructor(private val registry: To
kaptTask.stubsDir = getKaptStubsDir()
kaptTask.setDestinationDir { getKaptIncrementalDataDir() }
kaptTask.mapClasspath { kaptTask.kotlinCompileTask.classpath }
kaptTask.generatedSourcesDir = sourcesOutputDir
kaptTask.generatedSourcesDirs = listOf(sourcesOutputDir, kotlinSourcesOutputDir)
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
@@ -45,7 +45,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
lateinit var stubsDir: File
@get:Internal
lateinit var generatedSourcesDir: File
lateinit var generatedSourcesDirs: List<File>
@get:Classpath
@get:InputFiles
@@ -83,7 +83,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
private fun isSourceRootAllowed(source: File): Boolean =
!destinationDir.isParentOf(source) &&
!stubsDir.isParentOf(source) &&
!generatedSourcesDir.isParentOf(source)
generatedSourcesDirs.none { it.isParentOf(source) }
private val compileKotlinArgumentsContributor by project.provider {
kotlinCompileTask.compilerArgumentsContributor
@@ -106,7 +106,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
private val sourceRoots by project.provider {
kotlinCompileTask.getSourceRoots().let {
val javaSourceRoots = it.javaSourceRoots.filterTo(HashSet()) { isSourceRootAllowed(it) }
val kotlinSourceFiles = it.kotlinSourceFiles
val kotlinSourceFiles = it.kotlinSourceFiles.filterTo(ArrayList()) { isSourceRootAllowed(it) }
SourceRoots.ForJvm(kotlinSourceFiles, javaSourceRoots)
}
}