From dcdd1cd14e55b78f35fd528f3b641932e3957345 Mon Sep 17 00:00:00 2001 From: "nataliya.valtman" Date: Fri, 13 May 2022 13:18:17 +0300 Subject: [PATCH] KT-49780 Do not clean outputs after cache corruption --- .../incremental/IncrementalCompilerRunner.kt | 6 +- .../IncrementalCompilationMultiProjectIT.kt | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt index 3076c11a5d7..eadf86c2677 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt @@ -189,8 +189,10 @@ abstract class IncrementalCompilerRunner< return exitCode } catch (e: Exception) { // todo: catch only cache corruption // todo: warn? - reporter.report { "Rebuilding because of possible caches corruption: $e" } - rebuild(BuildAttribute.CACHE_CORRUPTION) + reporter.report { "Possible caches corruption: $e" } + rebuild(BuildAttribute.CACHE_CORRUPTION).also { + cachesMayBeCorrupted = false + } } finally { if (cachesMayBeCorrupted) { cleanOutputsAndLocalStateOnRebuild(args) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/IncrementalCompilationMultiProjectIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/IncrementalCompilationMultiProjectIT.kt index be50406fc8c..c65442dde88 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/IncrementalCompilationMultiProjectIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/IncrementalCompilationMultiProjectIT.kt @@ -4,6 +4,7 @@ import org.gradle.testkit.runner.BuildResult import org.gradle.util.GradleVersion import org.jetbrains.kotlin.gradle.testbase.* import org.jetbrains.kotlin.gradle.util.checkedReplace +import org.jetbrains.kotlin.test.KtAssert.assertTrue import org.junit.jupiter.api.DisplayName import java.nio.file.Path import kotlin.io.path.* @@ -32,6 +33,13 @@ class IncrementalCompilationJsMultiProjectIT : BaseIncrementalCompilationMultiPr override val compileKotlinTaskName: String get() = "compileKotlin2Js" + + override val compileCacheFolderName: String + get() = "caches-js" + + //compileKotlin2Js's modification doe not work + override fun testIncrementalBuildWithCompilationError(gradleVersion: GradleVersion) {} + override fun testValidOutputsWithCacheCorrupted(gradleVersion: GradleVersion) {} } @JvmGradlePluginTests @@ -42,6 +50,9 @@ open class IncrementalCompilationJvmMultiProjectIT : BaseIncrementalCompilationM override val compileKotlinTaskName: String get() = "compileKotlin" + override val compileCacheFolderName: String + get() = "caches-jvm" + override val defaultProjectName: String = "incrementalMultiproject" // todo: do the same for js backend @@ -262,6 +273,8 @@ abstract class BaseIncrementalCompilationMultiProjectIT : IncrementalCompilation protected abstract val compileKotlinTaskName: String + protected abstract val compileCacheFolderName: String + protected abstract val additionalLibDependencies: String protected fun TestProject.changeMethodSignatureInLib() { @@ -648,4 +661,64 @@ abstract class BaseIncrementalCompilationMultiProjectIT : IncrementalCompilation } } } + + @DisplayName("KT-49780: Valid outputs after cache corruption exception") + @GradleTest + open fun testValidOutputsWithCacheCorrupted(gradleVersion: GradleVersion) { + defaultProject(gradleVersion) { + breakCachesAfterCompileKotlinExecution(this) + + build("assemble") + + subProject("lib").kotlinSourcesDir().resolve("bar/B.kt").modify { + it.replace("fun b() {}", "fun b() = 123") + } + + build("assemble") { + assertOutputContains("Non-incremental compilation will be performed: CACHE_CORRUPTION") + } + + val lookupFile = projectPath.resolve("lib/build/kotlin/${compileKotlinTaskName}/cacheable/${compileCacheFolderName}/lookups/file-to-id.tab") + assertTrue("Output is empty", lookupFile.exists()) + + } + } + + @DisplayName("KT-49780: No need to rebuild invalid code due to cache corruption") + @GradleTest + open fun testIncrementalBuildWithCompilationError(gradleVersion: GradleVersion) { + defaultProject(gradleVersion) { + breakCachesAfterCompileKotlinExecution(this) + + build("assemble") + + subProject("lib").kotlinSourcesDir().resolve("bar/B.kt").modify { + it.replace("fun b() {}", "fun a() = 123") + } + + buildAndFail("assemble") { + printBuildOutput() + assertOutputDoesNotContain("Possible caches corruption:") + assertOutputDoesNotContain("Non-incremental compilation will be performed:") + } + } + } + + private fun breakCachesAfterCompileKotlinExecution(testProject: TestProject) { + listOf("app", "lib").forEach { + testProject.subProject(it).buildGradle.appendText( + """ + $compileKotlinTaskName { + doLast { + def file = new File(projectDir.path, "/build/kotlin/${compileKotlinTaskName}/cacheable/${compileCacheFolderName}/lookups/file-to-id.tab") + println("Update lookup file " + file.path) + file.write("la-la") + } + } + """.trimIndent() + ) + } + } + } +