Skip to content

Commit

Permalink
[JS IR] Invalidate an incremental cache in case of errors
Browse files Browse the repository at this point in the history
 Errors may corrupt the incremental cache file, which may affect
 the subsequent build and generate broken JS code.
 The patch invalidates cache directories in case of the error,
 so prevents the cache corruption.

^KT-56282 Fixed
  • Loading branch information
AlexK0 authored and qodana-bot committed Jan 31, 2023
1 parent 08dd52f commit b6cde89
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ class CacheUpdater(
)
}

fun actualizeCaches(eventCallback: (String) -> Unit = {}): List<ModuleArtifact> {
private fun actualizeCachesImpl(eventCallback: (String) -> Unit = {}): List<ModuleArtifact> {
dirtyFileStats.clear()

val modifiedFiles = loadModifiedFiles()
Expand Down Expand Up @@ -732,6 +732,15 @@ class CacheUpdater(

return artifacts
}

fun actualizeCaches(eventCallback: (String) -> Unit = {}): List<ModuleArtifact> {
try {
return actualizeCachesImpl(eventCallback)
} catch (e: Exception) {
cacheMap.values.forEach { cacheDir -> File(cacheDir).deleteRecursively() }
throw e
}
}
}

// Used for tests only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ class Kotlin2JsIrGradlePluginIT : AbstractKotlin2JsGradlePluginIT(true) {
}
}

@DisplayName("JS IR incremental recompilation after an error")
@GradleTest
fun testJsIrIncrementalRebuildAfterError(gradleVersion: GradleVersion) {
project("kotlin-js-ir-ic-rebuild-after-error", gradleVersion) {
for (i in 0..1) {
buildAndFail("compileDevelopmentExecutableKotlinJs") {
assertTasksFailed(":app:compileDevelopmentExecutableKotlinJs")

projectPath.resolve("app/build/klib/cache/").toFile().walk().forEach { cachedFile ->
// could be empty directories
assertTrue("Cache should be empty") { cachedFile.isDirectory }
}
}
}
}
}

@DisplayName("Remove unused dependency from klib")
@GradleTest
fun testJsIrIncrementalKlibRemoveUnusedDependency(gradleVersion: GradleVersion) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
kotlin("js")
}

kotlin {
js {
browser {
}
binaries.executable()
}
}

configurations["compileClasspath"].apply {
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, "kotlin-runtime"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package com.example

private fun someFunction(x: Any? = null) {}

fun getSomething(x: dynamic) = with(x) {
someFunction(::unkownFunction)
"Hello!"
}

fun main() {
println(getSomething(null))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
kotlin("js").apply(false)
}

group = "com.example"
version = "1.0"

allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kotlin.incremental.js.klib=true
kotlin.incremental.js.ir=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rootProject.name = "kotlin-js-ir-ic-rebuild-after-error"

include("app")

0 comments on commit b6cde89

Please sign in to comment.