ADFA-3944: ensure KtSymbolIndex.getKtFile does not fail for non-Kotlin files#1279
ADFA-3944: ensure KtSymbolIndex.getKtFile does not fail for non-Kotlin files#1279itsaky-adfa wants to merge 1 commit intostagefrom
Conversation
Signed-off-by: Akash Yadav <akashyadav@appdevforall.org>
📝 WalkthroughRelease Notes: ADFA-3944 - Ensure KtSymbolIndex.getKtFile Returns Null for Non-Kotlin FilesChanges
Risks and Considerations
WalkthroughKtSymbolIndex's ChangesKotlin File Type Validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 Microsoft Presidio Analyzer (2.2.362)lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.ktMicrosoft Presidio Analyzer failed to scan this file Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt (1)
183-192:⚠️ Potential issue | 🟠 Major | ⚡ Quick win
loadKtFilehas an unsafe cast that can still crash after theisKotlinFileguard.
PsiManager.findFile(vf)returnsPsiFile?— it can returnnull(e.g., the file isn't indexed yet) or a non-KtFilePsiFile(e.g., parsed as a stub). The hardas KtFilecast at line 191 will throwNullPointerExceptionorClassCastExceptionin both cases, even though the PR's intent is forgetKtFileto returnnullfor unresolvable cases.🛡️ Proposed fix: make `loadKtFile` nullable and propagate null
- private fun loadKtFile(vf: VirtualFile): KtFile = project.read { + private fun loadKtFile(vf: VirtualFile): KtFile? = project.read { PsiManager.getInstance(project) - .findFile(vf) as KtFile + .findFile(vf) as? KtFile }And update the call site in
getKtFile:- val ktFile = loadKtFile(vf) - - ktFileCache.put(path, ktFile) - return ktFile + val ktFile = loadKtFile(vf) ?: return null + ktFileCache.put(path, ktFile) + return ktFile🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt` around lines 183 - 192, Change loadKtFile to return a nullable KtFile (signature loadKtFile(vf: VirtualFile): KtFile?) and stop using the unsafe cast; inside project.read return PsiManager.getInstance(project).findFile(vf) as? KtFile so it yields null when the file is missing or not a KtFile. Update getKtFile to accept and propagate the nullable result (do not forcibly cast), only cache when the returned ktFile is non-null, and handle the null path the same way the guard intended (return null for unresolvable cases).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In
`@lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt`:
- Around line 183-192: Change loadKtFile to return a nullable KtFile (signature
loadKtFile(vf: VirtualFile): KtFile?) and stop using the unsafe cast; inside
project.read return PsiManager.getInstance(project).findFile(vf) as? KtFile so
it yields null when the file is missing or not a KtFile. Update getKtFile to
accept and propagate the nullable result (do not forcibly cast), only cache when
the returned ktFile is non-null, and handle the null path the same way the guard
intended (return null for unresolvable cases).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6b43f332-4906-4533-9ea2-62d2ca444305
📒 Files selected for processing (3)
lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.ktlsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/DeclarationsProvider.ktlsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/DirectInheritorsProvider.kt
See ADFA-3944 for more details.