Skip to content

ADFA-6564: Analyse completion's file copy in a dangling module - #2196

Merged
itsaky-adfa merged 3 commits into
stagefrom
fix/ADFA-6564
Oct 30, 2026
Merged

itsaky-adfa merged 3 commits into
stagefrom
fix/ADFA-6564

Conversation

@itsaky-adfa

@itsaky-adfa itsaky-adfa commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

ADFA-6564

Fixes #2152: typing a space inside a function body marked the whole class as Redeclaration: class LocalC : Any.

Root cause

Not the ADFA-4165 / ADFA-5231 bug (two live KtFile instances for one path); the pin from ADR 0015 still holds. Here the second LocalC is completion's placeholder copy of the file.

Any non-newline keystroke triggers completion, which analyses that copy through analyzingVariant. ProjectStructureProvider.getModuleImpl resolved the copy's module through originalKtFile.backingFilePath, so the copy became an ordinary member of the original's source module. When completion was the first analysis after an edit minted a new KtFile, FIR saw LocalC declared twice: once in the live file, once in the copy. The error then stayed until the next edit.

A newline does not trigger completion, which is why the report says "any non-newline character".

Fix

Copies (copyOrigin != null) now go through the base class's computeSpecialModule, which gives them a dangling-file module whose context is the original's module. The check is on copyOrigin, not isDangling, because our live in-memory files also report isDangling, and they must stay in their source module.

Commits

  1. Spotless reformat of ProjectStructureProvider.kt, no functional change. Touching the file pulled it under the ratchet.
  2. The fix and its regression test.

Verification

  • InBodyEditRedeclarationTest: open, diagnose, type a space in the body, run completion's analysis step, diagnose again. Fails without the fix with CLASSIFIER_REDECLARATION: Redeclaration: class LocalC : Any, passes with it.
  • :lsp:kotlin:testV7DebugUnitTest: 589 tests, 0 failures.
  • Device (moto g34, arm64): reproduced on 26.44 with the reporter's snippet. With the fix, the same keystroke gives Found 0 diagnostics (26.44: Found 1), and completion still offers both library (print) and same-file (ori) members.
  • Sites checked: analyzingVariant is the only place in :lsp:kotlin that creates a file copy.

No UI change, so no font-scale check.

Typing any non-newline character triggers completion, which analyses a
placeholder copy of the file. getModuleImpl mapped that copy into its
original's source module through originalKtFile.backingFilePath, so when
completion was the first analysis after an edit minted a new KtFile, FIR
saw the class declared twice and diagnostics reported the whole class as
a redeclaration of itself.

Copies now go through computeSpecialModule, which gives them a dangling
module whose context is the original's. Live files are unaffected: the
check is on copyOrigin, not isDangling, because the live in-memory files
report isDangling too.
@itsaky-adfa itsaky-adfa self-assigned this Oct 28, 2026
@itsaky-adfa
itsaky-adfa requested a review from a team October 28, 2026 11:30

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@github-actions github-actions Bot deleted a comment from atlassian Bot Oct 28, 2026
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 3c678f81-44f0-4773-b1d9-555336a11cbd
Commits

Reviewing files that changed from the base of the PR and between 237e1df and 616a389.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5d4784f1-d477-4c95-be7c-e04979303e63
Commits

Reviewing files that changed from the base of the PR and between ddb1ba1 and 237e1df.

Files selected for processing: 2
  • lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt
  • lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBodyEditRedeclarationTest.kt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

📝 Summary
  • Route Kotlin file copies with a non-null copyOrigin to a special module before virtual-file and module lookup. This prevents FIR from seeing declarations in the copy and original source module twice.
  • Keep registered live in-memory files on their existing source-module path.
  • Add InBodyEditRedeclarationTest for typing a space in a function body and analyzing a completion-style variant. The test checks that diagnostics remain empty before and after the edit.
  • The PR reports that the regression test and :lsp:kotlin:testV7DebugUnitTest pass. Test results were not independently verified here.
  • Risk: the special-module routing applies to all Kotlin files with a non-null copyOrigin; verify other copy-analysis flows retain the expected module behavior.

The Kotlin project structure provider now checks for a special module when a Kotlin file has a copyOrigin. A regression test checks that diagnostics remain empty after an in-body edit and completion analysis.

Changes: Kotlin module resolution

Layer / File(s) Summary
Copied-file module lookup
lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt
getModuleImpl checks whether the containing Kotlin file has a copyOrigin. If computeSpecialModule returns a module, the method returns it before the existing virtual-file lookup.
In-body edit regression test
lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBodyEditRedeclarationTest.kt
The test checks diagnostics before and after inserting a space inside a function body and running completion analysis.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: daniel-adfa

Merge Risk: Minimal · previous review 237e1

The change isolates completion copies to prevent spurious redeclaration diagnostics. No merge-blocking issue was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the completion file-copy issue and the dangling-module analysis change.
Description check Passed The description directly explains the redeclaration issue, root cause, fix, regression test, and verification results.
Linked Issues check Passed [#2152] Requirement: typing a non-newline character in a function body must not mark the class with Redeclaration: class LocalC : Any. The diff to ProjectStructureProvider.getModuleImpl adds an earl…
Out of Scope Changes check Passed Changes are limited to ProjectStructureProvider.kt (fix plus a formatting-only reflow) and the new InBodyEditRedeclarationTest.kt. The fix hunk and the regression test directly serve #2152. The remain…
Full details: Linked Issues check

Explanation

[#2152] Requirement: typing a non-newline character in a function body must not mark the class with Redeclaration: class LocalC : Any. The diff to ProjectStructureProvider.getModuleImpl adds an early branch: when element.containingFile is a KtFile with copyOrigin != null, the method returns computeSpecialModule(containingFile) when it is non-null. This routes completion's placeholder copy to a dangling module instead of the original's source module, which removes the duplicate LocalC declaration that FIR saw. The check uses copyOrigin, not isDangling, so live in-memory files keep their source module. InBodyEditRedeclarationTest (added in the diff) covers the reported sequence: open the file, diagnose, insert a space in the body, run a completion-style analyzingVariant step, and assert that diagnostics stay empty. The PR reports a failing run before the fix and a passing run after it, plus a device check (0 diagnostics after the fix, 1 before). I did not run the tests. The code change matches the stated root cause. No unmet coding requirement found.

Full details: Out of Scope Changes check

Explanation

Changes are limited to ProjectStructureProvider.kt (fix plus a formatting-only reflow) and the new InBodyEditRedeclarationTest.kt. The fix hunk and the regression test directly serve #2152. The remaining hunks in ProjectStructureProvider.kt are formatting or equivalent syntax changes: trailing commas, wrapped parameter lists, expression-body conversions, and reordering of the superclass list (KotlinProjectStructureProviderBase(), KtLspService). None changes behavior as shown in the diff. The PR describes these as a separate Spotless commit required because the file was touched, which connects them to the file that the fix modifies. No unrelated functional change found.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit taps a space with care,
Then checks the code for errors there.
The module finds its copied trail,
Diagnostics stay clear and pale.
One happy hop completes the tale.

Comment @coderabbitai help to get the list of available commands.

@itsaky-adfa
itsaky-adfa merged commit 4d1c978 into stage Oct 30, 2026
5 checks passed
@itsaky-adfa
itsaky-adfa deleted the fix/ADFA-6564 branch October 30, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diagnosis of incorrect redeclaration

2 participants