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:
committed by
nataliya.valtman
parent
34274dd032
commit
08446e2371
+16
@@ -24,6 +24,8 @@ import org.junit.Assume
|
|||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
|
import kotlin.test.assertNotNull
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
|
||||||
abstract class Kapt3BaseIT : BaseGradleIT() {
|
abstract class Kapt3BaseIT : BaseGradleIT() {
|
||||||
companion object {
|
companion object {
|
||||||
@@ -455,6 +457,20 @@ open class Kapt3IT : Kapt3BaseIT() {
|
|||||||
|
|
||||||
assertContains("Additional warning message from AP")
|
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
|
@Test
|
||||||
|
|||||||
+13
-5
@@ -48,11 +48,19 @@ class TestAnnotationProcessor : AbstractProcessor() {
|
|||||||
// print warning processingEnv.messager.printMessage(WARNING, "Additional warning message from AP")
|
// print warning processingEnv.messager.printMessage(WARNING, "Additional warning message from AP")
|
||||||
|
|
||||||
processingEnv.filer
|
processingEnv.filer
|
||||||
.createResource(StandardLocation.CLASS_OUTPUT, "abc", "helloWorld.txt")
|
.createResource(
|
||||||
.openWriter()
|
StandardLocation.SOURCE_OUTPUT,
|
||||||
.use {
|
"test.generated",
|
||||||
it.write("Hello, world!")
|
"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
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -563,7 +563,7 @@ class Kapt3GradleSubplugin @Inject internal constructor(private val registry: To
|
|||||||
kaptTask.stubsDir = getKaptStubsDir()
|
kaptTask.stubsDir = getKaptStubsDir()
|
||||||
kaptTask.setDestinationDir { getKaptIncrementalDataDir() }
|
kaptTask.setDestinationDir { getKaptIncrementalDataDir() }
|
||||||
kaptTask.mapClasspath { kaptTask.kotlinCompileTask.classpath }
|
kaptTask.mapClasspath { kaptTask.kotlinCompileTask.classpath }
|
||||||
kaptTask.generatedSourcesDir = sourcesOutputDir
|
kaptTask.generatedSourcesDirs = listOf(sourcesOutputDir, kotlinSourcesOutputDir)
|
||||||
|
|
||||||
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
|
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -45,7 +45,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
|
|||||||
lateinit var stubsDir: File
|
lateinit var stubsDir: File
|
||||||
|
|
||||||
@get:Internal
|
@get:Internal
|
||||||
lateinit var generatedSourcesDir: File
|
lateinit var generatedSourcesDirs: List<File>
|
||||||
|
|
||||||
@get:Classpath
|
@get:Classpath
|
||||||
@get:InputFiles
|
@get:InputFiles
|
||||||
@@ -83,7 +83,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
|
|||||||
private fun isSourceRootAllowed(source: File): Boolean =
|
private fun isSourceRootAllowed(source: File): Boolean =
|
||||||
!destinationDir.isParentOf(source) &&
|
!destinationDir.isParentOf(source) &&
|
||||||
!stubsDir.isParentOf(source) &&
|
!stubsDir.isParentOf(source) &&
|
||||||
!generatedSourcesDir.isParentOf(source)
|
generatedSourcesDirs.none { it.isParentOf(source) }
|
||||||
|
|
||||||
private val compileKotlinArgumentsContributor by project.provider {
|
private val compileKotlinArgumentsContributor by project.provider {
|
||||||
kotlinCompileTask.compilerArgumentsContributor
|
kotlinCompileTask.compilerArgumentsContributor
|
||||||
@@ -106,7 +106,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
|
|||||||
private val sourceRoots by project.provider {
|
private val sourceRoots by project.provider {
|
||||||
kotlinCompileTask.getSourceRoots().let {
|
kotlinCompileTask.getSourceRoots().let {
|
||||||
val javaSourceRoots = it.javaSourceRoots.filterTo(HashSet()) { isSourceRootAllowed(it) }
|
val javaSourceRoots = it.javaSourceRoots.filterTo(HashSet()) { isSourceRootAllowed(it) }
|
||||||
val kotlinSourceFiles = it.kotlinSourceFiles
|
val kotlinSourceFiles = it.kotlinSourceFiles.filterTo(ArrayList()) { isSourceRootAllowed(it) }
|
||||||
SourceRoots.ForJvm(kotlinSourceFiles, javaSourceRoots)
|
SourceRoots.ForJvm(kotlinSourceFiles, javaSourceRoots)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user