Skip to content

Eclipse formatter 4.20 and 4.21 are broken for records #951

Closed

Description

@marko-radosavljevic

Summary of the problem:

Updating from spotless 5.15.0 to 5.15.1/5.15.2 breaks formatting.

After closer inspection, using eclipse() breaks formatting (mostly inside a record), since eclipse version is updated to 4.20, and then 4.21.

Temporary fix is to hardcode eclipse version to '4.19.0'.

Gradle or maven version:

Introduced in gradle 5.15.1;
Reproducible in gradle 5.15.2;

operating system and version:

Ubuntu 20.04

Spotless configuration:

    spotless {
        java {
            // Excludes build folder since it contains generated java classes.
            targetExclude("build/**")
            eclipse().configFile("${rootProject.rootDir}/meta/formatting/google-style-eclipse.xml")
        }
    }

Issue noticed in: Together-Java/TJ-Bot#140, here you can find failing spotless check and diff of affected file.
'Fixed' in: Together-Java/TJ-Bot#151
Root gradle: https://github.com/Together-Java/TJ-Bot/blob/develop/build.gradle
Slightly modified google style xml: https://github.com/Together-Java/TJ-Bot/blob/develop/meta/formatting/google-style-eclipse.xml

Activity

  1. changed the title [-]Updating spotless to 5.15.1 breaks formatting[/-] [+]Eclipse formatter 4.20 and 4.21 are broken for records[/+] on Sep 29, 2021
  2. nedtwigg commented on Sep 29, 2021

    @nedtwigg
    Member

    Each time a new version of an existing formatter comes out and we bump our default version, there is a chance that formatting will change. One way to avoid this is to pin the formatter version (as you have done with eclipse('4.19.0'), and another way is to use ratcheting to make sure that the formatter changes only affect files that are changing anyway.

    Our general philosophy for handling the changing of these defaults is laid out here.

    However, the case you are describing is quite extreme, and it sure looks like the eclipse formatter is just plain broken for java record classes starting with 4.20

    image

    It could be that we need to make some changes to this line

    TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, raw, 0, raw.length(), 0, SpotlessEclipseFramework.LINE_DELIMITER);

    @fvgh is the expert here, but it may be the case that this will just be broken until Eclipse 4.22 or later.

  3. fvgh commented on Sep 29, 2021

    @fvgh
    Member

    I am afraid it seems a Spotless error. Only quickly checked the formatting with Eclipse 2021-06 (4.20.0). I will see what I can achieve on the weekend. Help is always welcome 😄

    @nedtwigg Thanks for the hint. There seems also a bug in the format statement, but that should only affect module-info. We need to extend the interface for the spotless-eclipse-jdt. It should detect a module-info by filename.

  4. fvgh commented on Oct 3, 2021

    @fvgh
    Member

    #957 proves the correct integration of the JDT formatter into Eclipse.
    The erroneous formatting reported within this issue is caused by the configuration used by TJ-Bot.
    Formatting with default Eclipse configuration (build.gradle):

        spotless {
            java {
                // Excludes build folder since it contains generated java classes.
                targetExclude("build/**")
                eclipse('4.21.0')
            }
        }

    Results in formatter/src/main/java/org/togetherjava/formatter/tokenizer/Token.java:

    import java.util.Set;
    
    /**
     * Class representing a Token with a given content and a type
     *
     * @author illuminator3
     */
    public record Token(String content, TokenType type) {
            private static final Set<TokenType> displayTypes = Set.of(TokenType.IDENTIFIER, TokenType.UNKNOWN, TokenType.STRING,
                            TokenType.COMMENT);
    
            @Override
            public String toString() {
                    return type().name() + formatForDisplay();
            }
    
            /**
             * Returns a non-empty string if this token has something to display
             *
             * @return the displayed value
             * @author illuminator3
             */
            private String formatForDisplay() {
                    if (displayTypes.contains(type())) {
                            return "(" + content() + ")";
                    }
    
                    return "";
            }
    }

    @marko-radosavljevic Please check your Eclipse configuration. Seems it causes conflicts using latter formatter versions.

  5. fvgh commented on Oct 10, 2021

    @fvgh
    Member

    @marko-radosavljevic Closing this issue since I found no problems on Spotless side. Feel free to reopen if you have more input I should look into.

  6. marko-radosavljevic commented on Oct 12, 2021

    @marko-radosavljevic
    Author

    Thanks a lot for the help, I really appreciate it. ^^

    I will further investigate our eclipse configuration and make it compatible with newer formatter versions.

  7. moritz-tiedje commented on Sep 18, 2024

    @moritz-tiedje

    We ran into the same problem in a java 17 maven project. We used eclipse-java-google-style out of the box. Here's the plugin configuration from our pom.xml:

    <plugin>
      <groupId>com.diffplug.spotless</groupId>
      <artifactId>spotless-maven-plugin</artifactId>
      <version>2.43.0</version>
      <configuration>
        <java>
          <eclipse>
            <version>4.26</version>
            <file>${project.basedir}/eclipse-java-google-style.xml</file>
          </eclipse>
        </java>
      </configuration>
    </plugin>
    

    The problem was not on the Spotless side. We fixed it by removing the following lines from eclipse-java-google-style.xml:

    <setting id="org.eclipse.jdt.core.compiler.source" value="1.7"/>
    <setting id="org.eclipse.jdt.core.compiler.compliance" value="1.7"/>
    <setting id="org.eclipse.jdt.core.compiler.codegen.targetPlatform" value="1.7"/>
    
  8. added a commit that references this issue on Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions