Ensure all output directories are cleared on IC rebuild

In some cases IC needs to perform a rebuild.
Before this change IC was not clearing output directories
besides destination dir for classes, so for example
kapt stubs were not cleared.

Stalled stubs might lead to compile errors.
For example:
1. foo/XGen.java is generated from annotated class foo/X (XGen also
references X).
2. foo/X is moved to bar/X and some other change forces IC to rebuild.
3. kapt generates bar/X stub, but foo/X stub
was not removed because stubs dir is not cleared.
4. kapt runs annotation processors, foo/XGen.java is generated from
foo/X stub, bar/XGen.java is generated from bar/X stub.
5. kotlinc rebuilds. Since destination dir is cleared properly,
only bar/X.class exists.
6. javac tries to compile foo/XGen and fails, because it
compiles against actual Kotlin classes, not stubs.

This commit fixes the issue by passing all output directories
of a task from Gradle to Kotlin IC.

   #KT-21735 fixed
This commit is contained in:
Alexey Tsvetkov
2018-03-18 22:15:15 +03:00
parent afce075dc8
commit 30d0cc3a34
15 changed files with 180 additions and 36 deletions
@@ -193,6 +193,54 @@ open class Kapt3IT : Kapt3BaseIT() {
}
}
@Test
fun testRemoveJavaClassICRebuild() {
testICRebuild { project ->
project.projectFile("Foo.java").delete()
}
}
@Test
fun testChangeClasspathICRebuild() {
testICRebuild { project ->
project.projectFile("build.gradle").modify {
"$it\ndependencies { compile 'org.jetbrains.kotlin:kotlin-reflect:' + kotlin_version }"
}
}
}
// tests all output directories are cleared when IC rebuilds
private fun testICRebuild(performChange: (Project) -> Unit) {
val project = Project("incrementalRebuild", directoryPrefix = "kapt2")
val options = defaultBuildOptions().copy(incremental = true)
val generatedSrc = "build/generated/source/kapt/main"
project.build("build", options = options) {
assertSuccessful()
// generated sources
assertFileExists("$generatedSrc/bar/UseBar_MembersInjector.java")
}
performChange(project)
project.projectFile("UseBar.kt").modify { it.replace("package bar", "package foo.bar") }
project.build("build", options = options) {
assertSuccessful()
assertTasksExecuted(listOf(":kaptGenerateStubsKotlin", ":kaptKotlin", ":compileKotlin", ":compileJava"))
// generated sources
assertFileExists("$generatedSrc/foo/bar/UseBar_MembersInjector.java")
assertNoSuchFile("$generatedSrc/bar/UseBar_MembersInjector.java")
// classes
assertFileExists(kotlinClassesDir() + "foo/bar/UseBar.class")
assertNoSuchFile(kotlinClassesDir() + "bar/UseBar.class")
assertFileExists(javaClassesDir() + "foo/bar/UseBar_MembersInjector.class")
assertNoSuchFile(javaClassesDir() + "bar/UseBar_MembersInjector.class")
}
}
@Test
fun testRemoveAnnotationIC() {
val project = Project("simple", directoryPrefix = "kapt2")
@@ -0,0 +1,24 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "java"
apply plugin: "kotlin"
apply plugin: "kotlin-kapt"
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.14.1'
kapt 'com.google.dagger:dagger-compiler:2.14.1'
}
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package bar
import javax.inject.Inject
import foo.*
class UseBar {
@Inject
lateinit var bar: Bar
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package foo;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class Bar {
@Inject
public Bar(){
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package foo;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class Foo {
@Inject
public Foo(){
}
}