Skip to content

Commit 0561560

Browse files
demiurg906Space Cloud
authored andcommitted
[IC] Always mark as dirty files referenced by frontend plugins
^KT-55982 Fixed ^KT-75864
1 parent c39b0cb commit 0561560

File tree

15 files changed

+137
-12
lines changed

15 files changed

+137
-12
lines changed

build-common/src/org/jetbrains/kotlin/compilerRunner/OutputItemsCollector.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import java.io.File
2222

2323
interface OutputItemsCollector {
2424
fun add(sourceFiles: Collection<File>, outputFile: File)
25+
fun addSourceReferencedByCompilerPlugin(sourceFile: File)
26+
fun addOutputFileGeneratedForPlugin(outputFile: File)
2527
}
2628

2729
class OutputItemsCollectorImpl : OutputItemsCollector {
@@ -30,9 +32,27 @@ class OutputItemsCollectorImpl : OutputItemsCollector {
3032

3133
private val _outputs: MutableList<SimpleOutputItem> = mutableListOf()
3234

35+
val sourcesReferencedByCompilerPlugin: List<File>
36+
get() = _sourcesReferencedByCompilerPlugin
37+
38+
private val _sourcesReferencedByCompilerPlugin: MutableList<File> = mutableListOf()
39+
40+
val outputsFileGeneratedForPlugin: List<File>
41+
get() = _outputsFileGeneratedForPlugin
42+
43+
private val _outputsFileGeneratedForPlugin: MutableList<File> = mutableListOf()
44+
3345
override fun add(sourceFiles: Collection<File>, outputFile: File) {
3446
_outputs.add(SimpleOutputItem(sourceFiles, outputFile))
3547
}
48+
49+
override fun addSourceReferencedByCompilerPlugin(sourceFile: File) {
50+
_sourcesReferencedByCompilerPlugin.add(sourceFile)
51+
}
52+
53+
override fun addOutputFileGeneratedForPlugin(outputFile: File) {
54+
_outputsFileGeneratedForPlugin.add(outputFile)
55+
}
3656
}
3757

3858
data class SimpleOutputItem(val sourceFiles: Collection<File>, val outputFile: File) {

build-common/src/org/jetbrains/kotlin/incremental/CompilationTransaction.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,13 @@ class TransactionOutputsRegistrar(
279279
transaction.registerAddedOrChangedFile(outputFile.toPath())
280280
origin.add(sourceFiles, outputFile)
281281
}
282-
}
282+
283+
override fun addSourceReferencedByCompilerPlugin(sourceFile: File) {
284+
origin.addSourceReferencedByCompilerPlugin(sourceFile)
285+
}
286+
287+
override fun addOutputFileGeneratedForPlugin(outputFile: File) {
288+
transaction.registerAddedOrChangedFile(outputFile.toPath())
289+
origin.addOutputFileGeneratedForPlugin(outputFile)
290+
}
291+
}

build-common/src/org/jetbrains/kotlin/incremental/ICFileMappingTrackerImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ class ICFileMappingTrackerImpl(private val outputItemsCollector: OutputItemsColl
1515
}
1616

1717
override fun recordSourceReferencedByCompilerPlugin(sourceFile: File) {
18-
// nothing yet
18+
outputItemsCollector.addSourceReferencedByCompilerPlugin(sourceFile)
1919
}
2020

2121
override fun recordOutputFileGeneratedForPlugin(outputFile: File) {
22-
// nothing yet
22+
outputItemsCollector.addOutputFileGeneratedForPlugin(outputFile)
2323
}
2424
}

build-common/test/org/jetbrains/kotlin/incremental/TransactionOutputsRegistrarTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class TransactionOutputsRegistrarTest {
2727
override fun add(sourceFiles: Collection<File>, outputFile: File) {
2828
addedOutputItems.add(sourceFiles to outputFile)
2929
}
30+
31+
override fun addSourceReferencedByCompilerPlugin(sourceFile: File) {}
32+
33+
override fun addOutputFileGeneratedForPlugin(outputFile: File) {}
3034
}
3135

3236
@Test
@@ -65,4 +69,4 @@ class TransactionOutputsRegistrarTest {
6569
)
6670
assertFalse(Files.exists(outputFile), "Output file wasn't reverted")
6771
}
68-
}
72+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.incremental
7+
8+
import com.intellij.util.io.KeyDescriptor
9+
import org.jetbrains.kotlin.build.report.debug
10+
import org.jetbrains.kotlin.incremental.storage.AbstractBasicMap
11+
import org.jetbrains.kotlin.incremental.storage.BasicMapsOwner
12+
import org.jetbrains.kotlin.incremental.storage.IntExternalizer
13+
import org.jetbrains.kotlin.incremental.storage.ListExternalizer
14+
import org.jetbrains.kotlin.incremental.storage.toDescriptor
15+
import java.io.DataInput
16+
import java.io.DataOutput
17+
import java.io.File
18+
19+
class CompilerPluginFilesCache(
20+
workingDir: File,
21+
private val icContext: IncrementalCompilationContext,
22+
) : BasicMapsOwner(workingDir) {
23+
companion object {
24+
private const val SOURCES_REFERENCED_BY_PLUGINS = "sources-referenced-by-plugins"
25+
private const val OUTPUTS_GENERATED_FOR_PLUGINS = "outputs-generated-for-plugins"
26+
}
27+
28+
private val sourceFilesReferencedByPlugins: ListLikeMap = registerMap(
29+
ListLikeMap(
30+
storageFile = SOURCES_REFERENCED_BY_PLUGINS.storageFile,
31+
fileDescriptor = icContext.fileDescriptorForSourceFiles,
32+
icContext = icContext
33+
)
34+
)
35+
36+
private val outputFilesGeneratedForPlugins: ListLikeMap = registerMap(
37+
ListLikeMap(
38+
storageFile = OUTPUTS_GENERATED_FOR_PLUGINS.storageFile,
39+
fileDescriptor = icContext.fileDescriptorForOutputFiles,
40+
icContext = icContext
41+
)
42+
)
43+
44+
fun getSourceFilesReferencedByPlugins(): List<File> {
45+
return sourceFilesReferencedByPlugins.getValue()
46+
}
47+
48+
fun recordSourceFilesReferencedByPlugins(files: List<File>) {
49+
sourceFilesReferencedByPlugins.setValue(files)
50+
}
51+
52+
fun recordOutputFilesGeneratedByPlugins(files: List<File>) {
53+
outputFilesGeneratedForPlugins.setValue(files)
54+
}
55+
56+
fun removeOutputsGeneratedByPlugins() {
57+
val value = outputFilesGeneratedForPlugins.getAndRemoveValue()
58+
value.forEach {
59+
icContext.reporter.debug { "Deleting $it on clearing cache for plugin outputs" }
60+
icContext.transaction.deleteFile(it.toPath())
61+
}
62+
}
63+
}
64+
65+
private class ListLikeMap(
66+
storageFile: File,
67+
fileDescriptor: KeyDescriptor<File>,
68+
icContext: IncrementalCompilationContext,
69+
) : AbstractBasicMap<Int, List<File>>(
70+
storageFile,
71+
keyDescriptor = IntExternalizer.toDescriptor(),
72+
valueExternalizer = ListExternalizer(fileDescriptor),
73+
icContext,
74+
) {
75+
fun getValue(): List<File> {
76+
return get(0).orEmpty()
77+
}
78+
79+
fun getAndRemoveValue(): List<File> {
80+
return getValue().also { remove(0) }
81+
}
82+
83+
fun setValue(value: List<File>) {
84+
set(0, value)
85+
}
86+
}

compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCachesManager.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache
3838

3939
private val inputSnapshotsCacheDir = File(cachesRootDir, "inputs").apply { mkdirs() }
4040
private val lookupCacheDir = File(cachesRootDir, "lookups").apply { mkdirs() }
41+
private val compilerPluginFilesCacheDir = File(cachesRootDir, "compilerPluginFiles").apply { mkdirs() }
4142

4243
val inputsCache: InputsCache = InputsCache(inputSnapshotsCacheDir, icContext).apply { registerCache() }
4344
val lookupCache: LookupStorage = LookupStorage(lookupCacheDir, icContext).apply { registerCache() }
45+
val compilerPluginFilesCache: CompilerPluginFilesCache = CompilerPluginFilesCache(compilerPluginFilesCacheDir, icContext).apply { registerCache() }
4446
abstract val platformCache: PlatformCache
4547

4648
@Suppress("UnstableApiUsage")
@@ -75,4 +77,4 @@ class IncrementalJsCachesManager(
7577
) : IncrementalCachesManager<IncrementalJsCache>(icContext, cachesRootDir) {
7678
private val jsCacheFile = File(cachesRootDir, "js").apply { mkdirs() }
7779
override val platformCache = IncrementalJsCache(jsCacheFile, icContext, serializerProtocol).apply { registerCache() }
78-
}
80+
}

compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,10 @@ abstract class IncrementalCompilerRunner<
472472
while (dirtySources.any() || runWithNoDirtyKotlinSources(caches)) {
473473
val complementaryFiles = caches.platformCache.getComplementaryFilesRecursive(dirtySources)
474474
dirtySources.addAll(complementaryFiles)
475+
dirtySources.addAll(caches.compilerPluginFilesCache.getSourceFilesReferencedByPlugins())
475476
caches.platformCache.markDirty(dirtySources)
476477
caches.inputsCache.removeOutputForSourceFiles(dirtySources)
478+
caches.compilerPluginFilesCache.removeOutputsGeneratedByPlugins()
477479

478480
val lookupTracker = LookupTrackerImpl(getLookupTrackerDelegate())
479481
val expectActualTracker = ExpectActualTrackerImpl()
@@ -546,6 +548,10 @@ abstract class IncrementalCompilerRunner<
546548
caches.platformCache.updateComplementaryFiles(dirtySources, expectActualTracker)
547549
caches.inputsCache.registerOutputForSourceFiles(generatedFiles)
548550
caches.lookupCache.update(lookupTracker, sourcesToCompile, removedKotlinSources)
551+
caches.compilerPluginFilesCache.let {
552+
it.recordSourceFilesReferencedByPlugins(outputItemsCollector.sourcesReferencedByCompilerPlugin)
553+
it.recordOutputFilesGeneratedByPlugins(outputItemsCollector.outputsFileGeneratedForPlugin)
554+
}
549555
updateCaches(services, caches, generatedFiles, changesCollector)
550556
}
551557
if (compilationMode is CompilationMode.Rebuild) {

plugins/plugin-sandbox/plugin-sandbox-ic-test/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515

1616
testRuntimeOnly(project(":core:descriptors.runtime"))
1717
testRuntimeOnly(project(":compiler:fir:fir-serialization"))
18+
testRuntimeOnly(project(":compiler:fir:plugin-utils"))
1819

1920
testRuntimeOnly(commonDependency("org.lz4:lz4-java"))
2021
testRuntimeOnly(commonDependency("org.jetbrains.intellij.deps.jna:jna"))

plugins/plugin-sandbox/plugin-sandbox-ic-test/testData/pureKotlin/addMethodToGeneratedClass/A.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ import org.jetbrains.kotlin.plugin.sandbox.ExternalClassWithNested
55

66
@ExternalClassWithNested
77
class A
8-
9-
FAIL // comment me to actually run test

plugins/plugin-sandbox/plugin-sandbox-ic-test/testData/pureKotlin/addMethodToGeneratedClass/build.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
================ Step #1 =================
22

33
Compiling files:
4+
src/A.kt
45
src/B.kt
56
End of files
67
Exit code: OK

0 commit comments

Comments
 (0)