From 36f8b8ee2f42ab52b6c7ca0c62447f354d3d89c6 Mon Sep 17 00:00:00 2001 From: Hal Eisen Date: Tue, 17 Nov 2026 17:44:34 -0800 Subject: [PATCH 1/2] ADFA-5763: skip indexing a KtFile that FIR no longer resolves to The full pass indexed PsiManager's disk copy even for open files, and the edit path could index a parse a later edit had superseded. FIR resolves an open path to its newest parse, so analysing another instance finds no FIR for its declarations ("No fir element was found", ~1.3k GlitchTip events/week). indexSourceFile now asks KtSymbolIndex.resolvesTo (an identity check against getKtFile: non-blocking, never refreshes) before it touches the index, and skips a mismatched instance. The refresh that produced the newer instance already queued that instance's own pass, so nothing is requeued. Failures that still reach Sentry carry an instanceMatches tag: false means the instance was superseded mid-pass, true means the failure has another cause than this one. IndexWorkerOpenFileTest runs a full pass over an open file whose buffer differs from disk; without the guard it indexes the disk copy. --- .../lsp/kotlin/compiler/index/IndexWorker.kt | 5 +- .../kotlin/compiler/index/KtSymbolIndex.kt | 11 +++ .../compiler/index/SourceFileIndexer.kt | 23 ++++++- .../compiler/index/IndexWorkerDisposalTest.kt | 1 + .../compiler/index/IndexWorkerOpenFileTest.kt | 69 +++++++++++++++++++ .../index/IndexWorkerPhaseLifecycleTest.kt | 7 +- .../compiler/index/SourceFileIndexerTest.kt | 21 ++++-- 7 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerOpenFileTest.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt index 80e267aa49..d6125f5a94 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt @@ -28,6 +28,8 @@ internal class IndexWorker( private val fileIndex: KtFileMetadataIndex, private val sourceIndex: JvmSymbolIndex, private val scope: CoroutineScope, + /** [KtSymbolIndex.resolvesTo]. */ + private val resolvesTo: (Path, KtFile) -> Boolean, ) { companion object { private val logger = LoggerFactory.getLogger(IndexWorker::class.java) @@ -59,7 +61,7 @@ internal class IndexWorker( logger.debug("Indexing modified file: {}", path) try { // Not counted in sourceIndexCount: this runs on another coroutine, and an edit is not part of a pass. - indexSourceFile(project, ktFile, fileIndex, sourceIndex, cancelChecker) + indexSourceFile(project, ktFile, fileIndex, sourceIndex, cancelChecker, resolvesTo) } catch (e: AnalysisPreemptedException) { // Preempted by higher-priority analysis; re-queue so the edit still gets indexed. logger.debug("Indexing of modified file {} preempted; re-queueing", path) @@ -270,6 +272,7 @@ internal class IndexWorker( // A real (cancellable) checker so the scheduler can preempt this pass // in favour of completion/diagnostics. cancelChecker = ICancelChecker.Default(), + resolvesTo = resolvesTo, ) } if (wrote) IndexOutcome.INDEXED else IndexOutcome.SKIPPED diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt index f2a738517a..2cc4d7e723 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt @@ -93,6 +93,7 @@ internal class KtSymbolIndex( fileIndex = fileIndex, sourceIndex = sourceIndex, scope = scope, + resolvesTo = ::resolvesTo, ) private val scanningWorker = @@ -553,6 +554,16 @@ internal class KtSymbolIndex( .mapNotNull { Paths.get(it).toVirtualFileOrNull() } .toList() + /** + * Whether FIR resolves [path] to [ktFile]. Non-blocking and never refreshes, so the indexer can ask + * under `project.read` without queueing more indexing. + */ + @OptIn(ResolutionSideKtFileAccess::class) + internal fun resolvesTo( + path: Path, + ktFile: KtFile, + ): Boolean = getKtFile(path) === ktFile + /** [getKtFile] for [vf], keyed by the path it maps to. */ @ResolutionSideKtFileAccess internal fun getKtFile(vf: VirtualFile): KtFile? = getKtFile(vf.toNioPath(), vf) diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt index a54b93b1f7..1b86d42f0b 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt @@ -57,6 +57,7 @@ import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments import org.slf4j.LoggerFactory import java.io.FileNotFoundException import java.nio.file.NoSuchFileException +import java.nio.file.Path import java.time.Instant import kotlin.io.path.pathString @@ -102,7 +103,9 @@ internal fun KtFile.toMetadata( * Indexes [ktFile]'s declarations into [symbolsIndex] and records it in [fileIndex]. * * Returns true only when it wrote the file's symbols; false when the file was unchanged, could not - * be analyzed, or an index was closed. + * be analyzed, is not the instance FIR resolves its path to, or an index was closed. + * + * [resolvesTo] tells whether FIR resolves a path to a given instance. It must not block or refresh. */ @OptIn(UnpinnedAnalysis::class) internal suspend fun indexSourceFile( @@ -111,6 +114,7 @@ internal suspend fun indexSourceFile( fileIndex: KtFileMetadataIndex, symbolsIndex: JvmSymbolIndex, cancelChecker: ICancelChecker, + resolvesTo: (Path, KtFile) -> Boolean, ): Boolean { // Indexing runs at the lowest priority, yielding to completion and diagnostics. Wrapping the checker // lets the scheduler preempt an in-progress pass; the preemption surfaces as AnalysisPreemptedException @@ -127,7 +131,17 @@ internal suspend fun indexSourceFile( val newFile = project.read { if (project.isDisposed) return@read null - ktFile.toMetadata(project, isIndexed = true) + val metadata = ktFile.toMetadata(project, isIndexed = true) + /* + * FIR resolves an open path to its newest parse, so analysing any other instance (the disk copy + * on a full pass, or an edit's superseded parse) finds no FIR for its declarations (ADFA-5763). + * Skip it: the refresh that produced the newer instance queued that instance's own pass. + */ + if (!resolvesTo(Path.of(metadata.filePath), ktFile)) { + logger.debug("Skipping {}: FIR resolves its path to another instance", metadata.filePath) + return@read null + } + metadata } ?: return false val existingFile = fileIndex.get(newFile.filePath) checker.abortIfCancelled() @@ -163,7 +177,7 @@ internal suspend fun indexSourceFile( object : KtTreeVisitorVoid() { override fun visitDeclaration(dcl: KtDeclaration) { checker.abortIfCancelled() - val symbol = with(session) { analyzeDeclaration(newFile.filePath, dcl) } + val symbol = with(session) { analyzeDeclaration(newFile.filePath, dcl, resolvesTo) } checker.abortIfCancelled() symbol?.let { list.add(it) } super.visitDeclaration(dcl) @@ -195,6 +209,7 @@ internal suspend fun indexSourceFile( private fun KaSession.analyzeDeclaration( filePath: String, dcl: KtDeclaration, + resolvesTo: (Path, KtFile) -> Boolean, ): JvmSymbol? { dcl.name ?: return null return runCatching { @@ -216,6 +231,8 @@ private fun KaSession.analyzeDeclaration( Sentry.captureException(err) { scope -> scope.apply { setExtra("fpth", filePath) + // False means the instance was superseded mid-pass; true means the failure has another cause. + setTag("instanceMatches", resolvesTo(Path.of(filePath), dcl.containingKtFile).toString()) setExtra("dcl", dcl.name) setExtra("dcl.dbg", dcl.getDebugText()) setExtra( diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerDisposalTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerDisposalTest.kt index dfabdb5c78..001c8431b7 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerDisposalTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerDisposalTest.kt @@ -45,6 +45,7 @@ class IndexWorkerDisposalTest : KtLspTest() { fileIndex = fileIndex(), sourceIndex = jvmIndex(), scope = CoroutineScope(Dispatchers.Default + SupervisorJob()), + resolvesTo = env.ktSymbolIndex::resolvesTo, ) /* diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerOpenFileTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerOpenFileTest.kt new file mode 100644 index 0000000000..6a6fbbf308 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerOpenFileTest.kt @@ -0,0 +1,69 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.eventbus.events.editor.DocumentCloseEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import com.itsaky.androidide.projects.FileManager +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.appdevforall.codeonthego.indexing.InMemoryIndex +import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolDescriptor +import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolIndex +import org.appdevforall.codeonthego.indexing.jvm.KtFileMetadataDescriptor +import org.appdevforall.codeonthego.indexing.jvm.KtFileMetadataIndex +import org.appdevforall.codeonthego.indexing.util.BackgroundIndexer +import org.junit.After +import org.junit.Test +import java.nio.file.Path + +/** + * A full pass over an open file must not analyse PsiManager's disk copy: FIR resolves the path to the + * live buffer's parse, so the disk copy's declarations have no FIR ("No fir element was found", + * ADFA-5763). The refresh that made the live parse queues its own index pass. + */ +internal class IndexWorkerOpenFileTest : KtLspTest() { + override val enableParserEventSystem = true + + private var openedPath: Path? = null + + @After + fun closeDoc() { + openedPath?.let { FileManager.onDocumentClose(DocumentCloseEvent(it)) } + } + + @Test + fun `a full pass skips the disk copy of an open file`(): Unit = + runBlocking { + val onDisk = "package p\n\nfun onDisk() = 0\n" + val vf = createSourceFile("Main.kt", onDisk).virtualFile + val path = env.sourceRoots.first().resolve("Main.kt") + FileManager.onDocumentOpen(DocumentOpenEvent(path, "$onDisk\nfun onlyInBuffer() = 1\n", 1)) + openedPath = path + env.ktSymbolIndex.refreshCurrentKtFile(path) + + val backing = InMemoryIndex(JvmSymbolDescriptor) + val sourceIndex = + object : JvmSymbolIndex(backing, BackgroundIndexer(backing)) { + override fun visibleSourceIds(): Collection? = null + } + val worker = + IndexWorker( + project = env.project, + queue = WorkerQueue(), + fileIndex = KtFileMetadataIndex(InMemoryIndex(KtFileMetadataDescriptor)), + sourceIndex = sourceIndex, + scope = CoroutineScope(Dispatchers.Default + SupervisorJob()), + resolvesTo = env.ktSymbolIndex::resolvesTo, + ) + + worker.submitCommand(IndexCommand.IndexSourceFile(vf)) + worker.submitCommand(IndexCommand.Stop) + withTimeout(10_000) { worker.start() } + + assertThat(sourceIndex.findByPrefix("").map { it.shortName }.toList()).isEmpty() + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerPhaseLifecycleTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerPhaseLifecycleTest.kt index 3a2aa2a6c3..5004a60777 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerPhaseLifecycleTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorkerPhaseLifecycleTest.kt @@ -72,6 +72,7 @@ class IndexWorkerPhaseLifecycleTest { fileIndex = fileIndex, sourceIndex = sourceIndex, scope = CoroutineScope(Dispatchers.Default + SupervisorJob()), + resolvesTo = { _, _ -> true }, ) } @@ -203,7 +204,7 @@ class IndexWorkerPhaseLifecycleTest { mockPsiManager(ktFile = mockk()) mockkStatic("com.itsaky.androidide.lsp.kotlin.compiler.index.SourceFileIndexerKt") var attempts = 0 - coEvery { indexSourceFile(any(), any(), any(), any(), any()) } answers { + coEvery { indexSourceFile(any(), any(), any(), any(), any(), any()) } answers { if (++attempts == 1) throw AnalysisPreemptedException() sink.events += "indexed retry" true @@ -237,7 +238,7 @@ class IndexWorkerPhaseLifecycleTest { runBlocking { Memprof.sink = sink mockkStatic("com.itsaky.androidide.lsp.kotlin.compiler.index.SourceFileIndexerKt") - coEvery { indexSourceFile(any(), any(), any(), any(), any()) } answers { + coEvery { indexSourceFile(any(), any(), any(), any(), any(), any()) } answers { sink.events += "indexed edit" true } @@ -267,7 +268,7 @@ class IndexWorkerPhaseLifecycleTest { Memprof.sink = sink mockPsiManager(ktFile = mockk()) mockkStatic("com.itsaky.androidide.lsp.kotlin.compiler.index.SourceFileIndexerKt") - coEvery { indexSourceFile(any(), any(), any(), any(), any()) } returns false + coEvery { indexSourceFile(any(), any(), any(), any(), any(), any()) } returns false val worker = worker() worker.submitCommand(IndexCommand.SourceScanningStarted(pass = 1)) diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt index ad0333f7fd..5840ec3876 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt @@ -13,6 +13,7 @@ import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolIndex import org.appdevforall.codeonthego.indexing.jvm.KtFileMetadataDescriptor import org.appdevforall.codeonthego.indexing.jvm.KtFileMetadataIndex import org.appdevforall.codeonthego.indexing.util.BackgroundIndexer +import org.jetbrains.kotlin.psi.KtFile import org.junit.Test import java.io.FileNotFoundException import java.nio.file.NoSuchFileException @@ -27,6 +28,12 @@ class SourceFileIndexerTest : KtLspTest() { private fun buildFileIndex() = KtFileMetadataIndex(InMemoryIndex(KtFileMetadataDescriptor)) + private suspend fun index( + file: KtFile, + fileIndex: KtFileMetadataIndex, + symbolsIndex: JvmSymbolIndex, + ) = indexSourceFile(env.project, file, fileIndex, symbolsIndex, ICancelChecker.NOOP, env.ktSymbolIndex::resolvesTo) + @Test fun `indexer ignores local property inside function body`(): Unit = runBlocking { @@ -52,7 +59,7 @@ class SourceFileIndexerTest : KtLspTest() { val symbolsIndex = buildSymbolIndex() - indexSourceFile(env.project, file, mockk(relaxed = true), symbolsIndex, ICancelChecker.NOOP) + index(file, mockk(relaxed = true), symbolsIndex) val names = symbolsIndex.findByPrefix("").map { it.shortName }.toSet() assertThat(names).containsExactly("doWork", "fetchStatus") @@ -74,7 +81,7 @@ class SourceFileIndexerTest : KtLspTest() { val symbolsIndex = buildSymbolIndex() - indexSourceFile(env.project, file, mockk(relaxed = true), symbolsIndex, ICancelChecker.NOOP) + index(file, mockk(relaxed = true), symbolsIndex) val names = symbolsIndex.findByPrefix("").map { it.shortName }.toSet() assertThat(names).containsExactly("outer") @@ -94,7 +101,7 @@ class SourceFileIndexerTest : KtLspTest() { val symbolsIndex = buildSymbolIndex() - indexSourceFile(env.project, file, mockk(relaxed = true), symbolsIndex, ICancelChecker.NOOP) + index(file, mockk(relaxed = true), symbolsIndex) val names = symbolsIndex.findByPrefix("").map { it.shortName }.toSet() assertThat(names).containsExactly("globalConfig", "compute") @@ -105,7 +112,7 @@ class SourceFileIndexerTest : KtLspTest() { runBlocking { val file = createSourceFile("Fresh.kt", "fun fresh() = 0") - val wrote = indexSourceFile(env.project, file, buildFileIndex(), buildSymbolIndex(), ICancelChecker.NOOP) + val wrote = index(file, buildFileIndex(), buildSymbolIndex()) assertThat(wrote).isTrue() } @@ -116,9 +123,9 @@ class SourceFileIndexerTest : KtLspTest() { val file = createSourceFile("Unchanged.kt", "fun unchanged() = 0") val fileIndex = buildFileIndex() val symbolsIndex = buildSymbolIndex() - indexSourceFile(env.project, file, fileIndex, symbolsIndex, ICancelChecker.NOOP) + index(file, fileIndex, symbolsIndex) - val wrote = indexSourceFile(env.project, file, fileIndex, symbolsIndex, ICancelChecker.NOOP) + val wrote = index(file, fileIndex, symbolsIndex) assertThat(wrote).isFalse() } @@ -136,7 +143,7 @@ class SourceFileIndexerTest : KtLspTest() { override fun visibleSourceIds(): Collection? = null } - val wrote = indexSourceFile(env.project, file, buildFileIndex(), symbolsIndex, ICancelChecker.NOOP) + val wrote = index(file, buildFileIndex(), symbolsIndex) assertThat(wrote).isFalse() } From 89589c9c1c9afa54a44f9b6b1929822606da5a16 Mon Sep 17 00:00:00 2001 From: Hal Eisen Date: Tue, 17 Nov 2026 18:12:57 -0800 Subject: [PATCH 2/2] ADFA-5763: keep a throwing resolver from escaping the index worker getKtFile can throw (getNow on a refresh future that completed exceptionally). resolvesTo ran outside every catch, on a scope with no exception handler. A throw now skips the file; cancellation still propagates. The instanceMatches tag reports unknown instead of dropping the file. --- .../compiler/index/SourceFileIndexer.kt | 21 +++++++++++++-- .../compiler/index/SourceFileIndexerTest.kt | 26 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt index 1b86d42f0b..3e1fa3eb1e 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexer.kt @@ -79,6 +79,23 @@ internal fun Throwable.isMissingClasspathFile(): Boolean = .take(32) .any { it is NoSuchFileException || it is FileNotFoundException } +/** + * This resolver's answer, or null when it throws: e.g. the path's last refresh failed, so its future + * completed exceptionally. Callers run outside any catch on a scope with no exception handler. + */ +private fun ((Path, KtFile) -> Boolean).orNull( + path: Path, + ktFile: KtFile, +): Boolean? = + try { + invoke(path, ktFile) + } catch (e: CancellationException) { + throw e + } catch (e: Throwable) { + logger.warn("Could not tell which instance FIR resolves {} to", path, e) + null + } + internal fun KtFile.toMetadata( project: Project, isIndexed: Boolean = false, @@ -137,7 +154,7 @@ internal suspend fun indexSourceFile( * on a full pass, or an edit's superseded parse) finds no FIR for its declarations (ADFA-5763). * Skip it: the refresh that produced the newer instance queued that instance's own pass. */ - if (!resolvesTo(Path.of(metadata.filePath), ktFile)) { + if (resolvesTo.orNull(Path.of(metadata.filePath), ktFile) != true) { logger.debug("Skipping {}: FIR resolves its path to another instance", metadata.filePath) return@read null } @@ -232,7 +249,7 @@ private fun KaSession.analyzeDeclaration( scope.apply { setExtra("fpth", filePath) // False means the instance was superseded mid-pass; true means the failure has another cause. - setTag("instanceMatches", resolvesTo(Path.of(filePath), dcl.containingKtFile).toString()) + setTag("instanceMatches", resolvesTo.orNull(Path.of(filePath), dcl.containingKtFile)?.toString() ?: "unknown") setExtra("dcl", dcl.name) setExtra("dcl.dbg", dcl.getDebugText()) setExtra( diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt index 5840ec3876..deedd711b7 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/SourceFileIndexerTest.kt @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest import com.itsaky.androidide.progress.ICancelChecker import io.mockk.mockk +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.runBlocking import org.appdevforall.codeonthego.indexing.InMemoryIndex import org.appdevforall.codeonthego.indexing.api.Index @@ -148,6 +149,31 @@ class SourceFileIndexerTest : KtLspTest() { assertThat(wrote).isFalse() } + @Test + fun `a throwing resolver skips the file instead of escaping`(): Unit = + runBlocking { + val file = createSourceFile("Throws.kt", "fun throws() = 0") + val symbolsIndex = buildSymbolIndex() + + val wrote = + indexSourceFile(env.project, file, buildFileIndex(), symbolsIndex, ICancelChecker.NOOP) { _, _ -> + throw IllegalStateException("refresh failed") + } + + assertThat(wrote).isFalse() + assertThat(symbolsIndex.findByPrefix("").toList()).isEmpty() + } + + @Test(expected = CancellationException::class) + fun `a cancelled resolver still cancels the pass`(): Unit = + runBlocking { + val file = createSourceFile("Cancelled.kt", "fun cancelled() = 0") + + indexSourceFile(env.project, file, buildFileIndex(), buildSymbolIndex(), ICancelChecker.NOOP) { _, _ -> + throw CancellationException("cancelled") + } + } + @Test fun `isMissingClasspathFile detects a direct NoSuchFileException`() { assertThat(NoSuchFileException("/caches/transforms/x/classes.jar").isMissingClasspathFile())