Skip to content

Commit 7569b95

Browse files
committed
Don't calculate incremental info when incremental processing is disabled
1 parent a9c1f7f commit 7569b95

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt

+36-18
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,24 @@ class FileToFilesMap(storageFile: File) : BasicMap<File, Collection<File>>(stora
124124
}
125125

126126
object symbolCollector : KSDefaultVisitor<(LookupSymbol) -> Unit, Unit>() {
127-
override fun defaultHandler(node: KSNode, collect: (LookupSymbol) -> Unit) = Unit
127+
override fun defaultHandler(node: KSNode, data: (LookupSymbol) -> Unit) = Unit
128128

129-
override fun visitDeclaration(declaration: KSDeclaration, collect: (LookupSymbol) -> Unit) {
129+
override fun visitDeclaration(declaration: KSDeclaration, data: (LookupSymbol) -> Unit) {
130130
if (declaration.isPrivate())
131131
return
132132

133133
val name = declaration.simpleName.asString()
134134
val scope = declaration.qualifiedName?.asString()?.let { it.substring(0, Math.max(it.length - name.length - 1, 0))} ?: return
135-
collect(LookupSymbol(name, scope))
135+
data(LookupSymbol(name, scope))
136136
}
137137

138-
override fun visitDeclarationContainer(declarationContainer: KSDeclarationContainer, collect: (LookupSymbol) -> Unit) {
138+
override fun visitDeclarationContainer(declarationContainer: KSDeclarationContainer, data: (LookupSymbol) -> Unit) {
139139
// Local declarations aren't visible to other files / classes.
140140
if (declarationContainer is KSFunctionDeclaration)
141141
return
142142

143143
declarationContainer.declarations.forEach {
144-
it.accept(this, collect)
144+
it.accept(this, data)
145145
}
146146
}
147147
}
@@ -155,15 +155,20 @@ class IncrementalContext(
155155
private val options: KspOptions,
156156
private val ksFiles: List<KSFile>,
157157
private val componentProvider: ComponentProvider,
158-
private val anyChangesWildcard: File,
159-
private val isIncremental: Boolean
158+
private val anyChangesWildcard: File
160159
) {
161160
// Symbols defined in changed files. This is used to update symbolsMap in the end.
162161
private val updatedSymbols = MultiMap.createSet<File, LookupSymbol>()
163162

164-
// Symbols defined in each file. This is
163+
// Symbols defined in each file. This is saved across processing.
165164
private val symbolsMap = FileToSymbolsMap(File(options.cachesDir, "symbols"))
166165

166+
private val cachesUpToDateFile = File(options.cachesDir, "caches.uptodate")
167+
private val isIncremental = options.incremental
168+
private var rebuild = !isIncremental || !cachesUpToDateFile.exists()
169+
|| (options.knownModified.isEmpty() && options.knownRemoved.isEmpty())
170+
|| (options.knownModified + options.knownRemoved).any { !it.isKotlinFile(listOf("kt")) && !it.isJavaFile() }
171+
167172
private val baseDir = options.projectBaseDir
168173

169174
private val logsDir = File(baseDir, "build").apply { mkdir() }
@@ -195,7 +200,7 @@ class IncrementalContext(
195200
}
196201
}
197202

198-
fun updateLookupCache(dirtyFiles: Collection<File>) {
203+
private fun updateLookupCache(dirtyFiles: Collection<File>) {
199204
if (lookupTracker is LookupTrackerImpl) {
200205
lookupCache.update(lookupTracker, dirtyFiles, options.knownRemoved)
201206
lookupCache.flush(false)
@@ -261,7 +266,7 @@ class IncrementalContext(
261266
return visited
262267
}
263268

264-
fun logDirtyFilesByDeps(dirtyFiles: Collection<File>) {
269+
private fun logDirtyFilesByDeps(dirtyFiles: Collection<File>) {
265270
if (!options.incrementalLog)
266271
return
267272

@@ -276,7 +281,7 @@ class IncrementalContext(
276281
logFile.appendText("\n")
277282
}
278283

279-
fun logDirtyFilesByOutputs(dirtyFiles: Collection<File>) {
284+
private fun logDirtyFilesByOutputs(dirtyFiles: Collection<File>) {
280285
if (!options.incrementalLog)
281286
return
282287

@@ -299,7 +304,7 @@ class IncrementalContext(
299304
logFile.appendText("\n")
300305
}
301306

302-
fun logSourceToOutputs() {
307+
private fun logSourceToOutputs() {
303308
if (!options.incrementalLog)
304309
return
305310

@@ -315,7 +320,7 @@ class IncrementalContext(
315320
logFile.appendText("\n")
316321
}
317322

318-
fun logDirtyFiles(files: List<KSFile>) {
323+
private fun logDirtyFiles(files: List<KSFile>) {
319324
if (!options.incrementalLog)
320325
return
321326

@@ -333,7 +338,12 @@ class IncrementalContext(
333338

334339
// Beware: no side-effects here; Caches should only be touched in updateCaches.
335340
fun calcDirtyFiles(): Collection<KSFile> {
336-
if (isIncremental) {
341+
if (!isIncremental) {
342+
cleanIncrementalCache()
343+
return ksFiles
344+
}
345+
346+
if (!rebuild) {
337347
val dirtyFilesByDeps = calcDirtySetByDeps()
338348

339349
logDirtyFilesByDeps(dirtyFilesByDeps)
@@ -357,7 +367,7 @@ class IncrementalContext(
357367
}
358368
}
359369

360-
fun updateSourceToOutputs(dirtyFiles: Collection<File>, outputs: Set<File>, sourceToOutputs: Map<File, Set<File>>) {
370+
private fun updateSourceToOutputs(dirtyFiles: Collection<File>, outputs: Set<File>, sourceToOutputs: Map<File, Set<File>>) {
361371
// Prune deleted sources in source-to-outputs map.
362372
removed.forEach {
363373
sourceToOutputsMap.remove(it)
@@ -375,7 +385,7 @@ class IncrementalContext(
375385
}
376386

377387
// TODO: Recover if processing failed.
378-
fun updateOutputs(outputs: Set<File>, cleanOutputs: Collection<File>) {
388+
private fun updateOutputs(outputs: Set<File>, cleanOutputs: Collection<File>) {
379389
val outRoot = options.kspOutputDir
380390
val bakRoot = File(options.cachesDir, "backups")
381391

@@ -406,12 +416,12 @@ class IncrementalContext(
406416
}
407417

408418
// TODO: Don't do anything if processing failed.
409-
fun updateCaches(dirtyFiles: Collection<File>, outputs: Set<File>, sourceToOutputs: Map<File, Set<File>>) {
419+
private fun updateCaches(dirtyFiles: Collection<File>, outputs: Set<File>, sourceToOutputs: Map<File, Set<File>>) {
410420
updateSourceToOutputs(dirtyFiles, outputs, sourceToOutputs)
411421
updateLookupCache(dirtyFiles)
412422

413423
// Update symbolsMap
414-
if (isIncremental) {
424+
if (!rebuild) {
415425
// Update symbol caches from modified files.
416426
options.knownModified.forEach {
417427
symbolsMap.set(it, updatedSymbols[it].toSet())
@@ -432,6 +442,12 @@ class IncrementalContext(
432442
}
433443

434444
fun updateCachesAndOutputs(dirtyFiles: Collection<KSFile>, outputs: Set<File>, sourceToOutputs: Map<File, Set<File>>) {
445+
if (!isIncremental)
446+
return
447+
448+
cachesUpToDateFile.delete()
449+
assert(!cachesUpToDateFile.exists())
450+
435451
val cleanOutputs = mutableSetOf<File>()
436452
val dirtyFilePaths = dirtyFiles.map { it.relativeFile }
437453
sourceToOutputsMap.keys.forEach { source ->
@@ -442,6 +458,8 @@ class IncrementalContext(
442458
updateCaches(dirtyFilePaths, outputs, sourceToOutputs)
443459
updateOutputs(outputs, cleanOutputs)
444460

461+
cachesUpToDateFile.createNewFile()
462+
assert(cachesUpToDateFile.exists())
445463
}
446464

447465
// Insert Java file -> names lookup records.

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/KotlinSymbolProcessingExtension.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ abstract class AbstractKotlinSymbolProcessingExtension(val options: KspOptions,
8484

8585
val anyChangesWildcard = AnyChanges(options.projectBaseDir)
8686
val ksFiles = files.map { KSFileImpl.getCached(it) } + javaFiles.map { KSFileJavaImpl.getCached(it) }
87-
val isIncremental = options.incremental && (options.knownModified.isNotEmpty() || options.knownRemoved.isNotEmpty()) &&
88-
(options.knownModified + options.knownRemoved).all { it.isKotlinFile(listOf("kt")) || it.isJavaFile() }
8987
val incrementalContext = IncrementalContext(
9088
options, ksFiles, componentProvider,
91-
File(anyChangesWildcard.filePath).relativeTo(options.projectBaseDir),
92-
isIncremental
89+
File(anyChangesWildcard.filePath).relativeTo(options.projectBaseDir)
9390
)
9491
val dirtyFiles = incrementalContext.calcDirtyFiles()
9592

0 commit comments

Comments
 (0)