Visitar URL original
Spotless formats code differently between JDK versions (17 vs 21) · Issue #2503 · diffplug/spotless · GitHub
Skip to content

Spotless formats code differently between JDK versions (17 vs 21) #2503

Closed

Description

@MarkEWaite

We've seen cases in the Jenkins project use of spotless formatting where a comment adjacent to a case statement inside a switch will be formatted on Java 17 with a different indentation than on Java 21, even though the Java version is declared as Java 17 in the Maven project using Spotless.

A recent example is:

When we move the comment so that it is either inside the case or outside the switch, then the Java version dependent formatting does not happen.

Activity

  1. nedtwigg commented on Jun 3, 2025

    @nedtwigg
    Member

    I'm sure this bug is real, but it is almost definitely a bug in the formatter you are using in Spotless, rather than in Spotless itself. So either googleJavaFormat or eclipse, depending on which one you are using.

    When we move the comment so that it is either inside the case or outside the switch, then the platform dependent formatting does not happen.

    That is a useful and actionable bug report for whichever formatter you are using through Spotless.

  2. basil commented on Jun 3, 2025

    @basil

    @nedtwigg I think the problem is in Spotless itself; see #2447 (comment).

  3. changed the title [-]Spotless formats code differently between platforms (Windows vs Linux)[/-] [+]Spotless formats code differently between JDK versions (17 vs 21)[/+] on Jun 3, 2025
  4. nedtwigg commented on Jun 7, 2025

    @nedtwigg
    Member

    my summary of that comment

    • yes, if you don't specify a version for a formatter, Spotless picks the latest available which will run on the JVM you are using
    • if you want the same behavior on development environments running on multiple JRE versions, you will need to set the version explicitly
    • looking at the source-level of the project for JVM_SUPPORT calculations is a neat idea, but plumbing that out would be a significant task. I'm open to a PR which adds it, but I bet just pinning a specific version is going to way more bang for your buck
  5. jglick commented on Jun 26, 2025

    @jglick
    Contributor

    Hints in case I or someone else finds the time to fix this:

    private static final int VERSION;
    static {
    String jre = System.getProperty("java.version");
    if (jre.startsWith("1.8")) {
    VERSION = 8;
    } else {
    Matcher matcher = Pattern.compile("(\\d+)").matcher(jre);
    if (!matcher.find()) {
    throw new IllegalArgumentException("Expected " + jre + " to start with an integer");
    }
    VERSION = Integer.parseInt(matcher.group(1));
    if (VERSION <= 8) {
    throw new IllegalArgumentException("Expected " + jre + " to start with an integer greater than 8");
    }
    }
    }
    is the problem (this code should really not exist), and specifically
    /** @return Highest formatter version recommended for this JVM (null, if JVM not supported) */
    @Nullable
    public V getRecommendedFormatterVersion() {
    Integer configuredJvmVersionOrNull = jvm2fmtMaxVersion.floorKey(Jvm.version());
    return (null == configuredJvmVersionOrNull) ? null : jvm2fmtMaxVersion.get(configuredJvmVersionOrNull);
    }
    private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0").add(21, "2.57.0");
    String version = this.version != null ? this.version : PalantirJavaFormatStep.defaultVersion();
    needs to inspect the mojo configuration for https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#release falling back to https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source and finally falling back to the historical default of JDK 5.

    (Ideally the source level for src/test/java/ would be calculated independently, in case it is using a newer version than src/main/java/, but I suppose the mojo does not understand how to run two formatters on the same module.)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions