Skip to content

Conversation

@Achal1607
Copy link
Collaborator

@Achal1607 Achal1607 commented Nov 12, 2025

Fixes crashes and incorrect behavior when resolving completion items across different javac contexts.

When completion items are created, they capture Element references from the initial javac context. Later, when the LSP client calls completionItem/resolve to fetch additionalTextEdits, a new javac context may be used (especially if the file was modified). The old Element instances are invalid in the new context, causing Assertion Error rootPackage missing!; currModule: java.base.

So, store ElementHandles instead of stale Elements when creating completion items.

Thanks @lahodaj for helping out.

@Achal1607 Achal1607 requested a review from lahodaj November 12, 2025 08:40
@Achal1607 Achal1607 added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests labels Nov 12, 2025
@apache apache locked and limited conversation to collaborators Nov 12, 2025
@apache apache unlocked this conversation Nov 12, 2025
@mbien mbien added this to the NB29 milestone Nov 12, 2025
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. I am looking at possibilities to increase coverage for this problem, but I think this patch can go in without it.

wc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
ExecutableElement currentElem = elemHandle.resolve(wc);
if (currentElem == null) {
//cannot resolve?
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - maybe remove this comment, as other places don't have it either. (Or add logging to this and other places.)

if (handle != null) {
builder.documentation(getDocumentation(doc, offset, handle));

if (SUPPORTED_ELEMENT_KINDS.contains(elem.getKind().name())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the SUPPORTED_ELEMENT_KINDS check here does not serve any function anymore. I believe the point here was to not call ElementHandle.create with an unsupported ElementKind, but: a) I think all kinds that can occur here are supported; b) we have already created the ElementHandle by this point, so if that would not work, it would have failed already. I think I would suggest to simply delete the condition here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants