vi-eclipse / Eclipse-JDT

Umbrella repository for managing a backlog of Eclipse-JDT-related features/issues
0 stars 0 forks source link

IndexOutOfBoundsException when saving changes #10

Open fedejeanne opened 1 year ago

fedejeanne commented 1 year ago

This exception popped up when saving content in a Java class

!ENTRY org.eclipse.jdt.ui 4 10006 2023-08-10 13:47:57.221
!MESSAGE The save participant 'org.eclipse.jdt.ui.postsavelistener.cleanup' caused an exception: java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 570
!STACK 0
java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 570
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.get(ArrayList.java:459)
    at org.eclipse.jdt.internal.formatter.TokenManager.get(TokenManager.java:77)
    at org.eclipse.jdt.internal.formatter.TokenManager.findIndex(TokenManager.java:170)
    at org.eclipse.jdt.internal.formatter.TokenManager.lastIndexIn(TokenManager.java:196)
    at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.handleBracedCode(LineBreaksPreparator.java:713)
    at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.handleBracedCode(LineBreaksPreparator.java:705)
    at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.visit(LineBreaksPreparator.java:155)
    at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:482)
    at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3214)
    at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3285)
    at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:258)
    at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3214)
    at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareLineBreaks(DefaultCodeFormatter.java:417)
    at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareFormattedCode(DefaultCodeFormatter.java:229)
    at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:185)
    at org.eclipse.jdt.internal.corext.util.CodeFormatterUtil.reformat(CodeFormatterUtil.java:396)
    at org.eclipse.jdt.internal.corext.fix.CodeFormatFix.createCleanUp(CodeFormatFix.java:81)
    at org.eclipse.jdt.internal.ui.fix.CodeFormatCleanUp.createFix(CodeFormatCleanUp.java:64)
    at org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.calculateChange(CleanUpRefactoring.java:768)
    at org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener.saved(CleanUpPostSaveListener.java:384)

Image

Expected behavior

The exception shouldn't happen

How to reproduce

The problem happens seldom but a possible way of reproducing it could be:

sagarrohat commented 1 year ago

On quick google search I found this comment in a bug which has code to reproduce the bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=471825#c11

It fails when we try to Save an syntactically incorrect code.

As far I can understand, it fails because we ignore errors if kind is K_COMPILATION_UNIT: https://github.com/eclipse-jdt/eclipse.jdt.core/blob/master/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java#L317

For me if a code is not syntactically we should not format it at all but I can't find any comment on why ignoreErrors was set to true for K_COMPILATION_UNIT in the original code which was introduced in this commit: https://github.com/eclipse-jdt/eclipse.jdt.core/commit/b0e753009ffb9bf0d92f75f621fb0d6188adb57e

sagarrohat commented 1 year ago

Submitted issue in jdt.core repo: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1437

HeikoKlare commented 11 months ago

Pushed back to ready. Can be picked again.