Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.project.sourcePaths doesn't refresh diagnostics #1771

Merged
merged 1 commit into from
May 25, 2021

Conversation

snjeza
Copy link
Contributor

@snjeza snjeza commented May 17, 2021

Fixes #1769

Signed-off-by: Snjezana Peco [email protected]

Comment on lines +68 to +88
IPath path = unit.getPath();
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
if (file.exists()) {
String contents = null;
try {
if (unit.hasUnsavedChanges()) {
contents = unit.getSource();
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
unit.discardWorkingCopy();
if (unit.equals(CoreASTProvider.getInstance().getActiveJavaElement())) {
CoreASTProvider.getInstance().disposeAST();
}
unit = JavaCore.createCompilationUnitFrom(file);
unit.becomeWorkingCopy(null);
if (contents != null) {
unit.getBuffer().setContents(contents);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't understand the purpose of discardWorkingCopy and becomeWorkingCopy again. For me, it's not necessary.

Copy link
Contributor

@rgrunber rgrunber May 19, 2021

Choose a reason for hiding this comment

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

I haven't looked too closely yet, but I'd be curious whether commitWorkingCopy(..) or reconcile(..) might achieve the same result. If you're doing this to re-trigger problem detection, I believe reconcile has an option to force that.

Copy link
Contributor Author

@snjeza snjeza May 19, 2021

Choose a reason for hiding this comment

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

I didn't understand the purpose of discardWorkingCopy and becomeWorkingCopy again. For me, it's not necessary.

We have to discard the old working copy (discardWorkingCopy, the line 79), create a new compilation unit (JavaCore.createCompilationUnitFrom,the line 83) and change it into a working copy (becomeWorkingCopy,the line 84).
If we remove the line 79, we will make a memory leak. If we remove the line 84, problems aren't reported to the problem requestor of the working copy owner.
You can try the following:

I haven't looked too closely yet, but I'd be curious whether commitWorkingCopy(..) or reconcile(..) might achieve the same result. If you're doing this to re-trigger problem detection, I believe reconcile has an option to force that.

I have tried them, but they don't help.

Copy link
Contributor

Choose a reason for hiding this comment

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

After investigating this a bit more, I can see why the code here is needed.

Generally we create a working copy for a compilation unit when the file is opened, and discard it when it's closed. This makes sense since we need a working copy to make modifications.

When the source path is updated with the correct value, we update the project configuration (source, output, classpaths) but it's likely the working copy needs some updating too, or it will remain in the state with an improper source path). The only hint I could find for updating the working copy is in didChange(..). So looking at DocumentLifeCycleHandler#handleChanged(..) I see sharedASTProvider.disposeAST() and cu.makeConsistent(..) (in triggerValidation(..)) . @snjeza, have you tried diposeAST() combined with makeConsistent() ? If that still doesn't work, we can probably look at taking the change since it does seem to fix things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@snjeza, have you tried diposeAST() combined with makeConsistent()

Yes, I am. It doesn't work.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we do some very similar stuff in https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseDocumentLifeCycleHandler.java and particularly checkPackageDeclaration.

I'll just verify the tests are fine and I think we can merge.

Copy link
Contributor

@rgrunber rgrunber left a comment

Choose a reason for hiding this comment

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

Looks good, just one minor thing.

Copy link
Contributor

@rgrunber rgrunber left a comment

Choose a reason for hiding this comment

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

Looks good. Feel free to merge this.

I wouldn't mind eventually investigating why discardWorkingCopy/becomeWorkingCopy is needed to freshen the diagnostics, though maybe related to the problem requester associated with the working copy owner.

@snjeza
Copy link
Contributor Author

snjeza commented May 25, 2021

I wouldn't mind eventually investigating why discardWorkingCopy/becomeWorkingCopy is needed to freshen the diagnostics, though maybe related to the problem requester associated with the working copy owner.

When we update a source path, all working copies in this source folder are invalid. They have a new package fragment root.
You can reproduce the issue in Eclipse:

  • open a Java file
  • open .classpath in an Eclipse editor and update its source container
  • try to edit opened Java file
    The problems aren't reported properly.

Closing/opening fixes it in Eclipse.

@snjeza snjeza merged commit 3b0045a into eclipse-jdtls:master May 25, 2021
@snjeza snjeza deleted the issue-1769 branch May 25, 2021 22:18
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.

java.project.sourcePaths doesn't refresh diagnostics
3 participants