spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Wrong formatting (missing whitespace) in Java instanceof type pattern with method calls on the left side

Open krusche opened this issue 4 years ago • 5 comments

If you are submitting a bug, please include the following:

  • [x] summary of problem
  • [x] gradle or maven version
  • [x] spotless version
  • [x] operating system and version
  • [x] copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
  • [x] copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace

We still use Spotless 5.15.0 because all subsequent releases introduce an issue that we are affected: a whitespace before instanceof is removed in some cases, not all.

We use Gradle 7.3.3 and a custom Eclipse configuration: https://github.com/ls1intum/Artemis/blob/develop/artemis-spotless-style.xml (Note: In Eclipse, it seems the formatting works correctly for the example that went wrong in our repository)

Our full gradle file is here: https://github.com/ls1intum/Artemis/blob/develop/build.gradle

Here is the relevant spotless configuration:

spotless {
    // allows to execute the code formatting commands ./gradlew spotlessApply and ./gradlew spotlessCheck
    java {
        target project.fileTree(project.rootDir) {
            include "**/*.java"
            exclude "**/src/main/java/de/tum/in/www1/artemis/service/connectors/BambooService.java", "**/src/main/java/de/tum/in/www1/artemis/config/SecurityConfiguration.java", "**/src/main/java/de/tum/in/www1/artemis/config/SAML2Configuration.java", "**/src/test/resources/test-data/repository-export/EncodingISO_8559_1.java", "**/node_modules/**", "**/out/**", "**/repos/**", "**/build/**", "**/src/main/generated/**", "**/src/main/resources/templates/**"
        }
        importOrderFile "artemis-spotless.importorder"
        eclipse().configFile "artemis-spotless-style.xml"

        removeUnusedImports()
    }
}

When I update to the latest spotless version in line 24 of build.gradle

id "com.diffplug.spotless" version "6.0.5"

then using ./gradlew spotlessApply -x webapp will lead to changes that are wrong:

0001-wrong-spotless-changes-git-patch.txt

Here is one simple example:

-                    if (discoveryHealthContributor.getContributor() instanceof HealthIndicator healthIndicator) {
+                    if (discoveryHealthContributor.getContributor()instanceof HealthIndicator healthIndicator) {

Notice the removed whitespace between the method call on the left side and instanceof.

It seems that a method call on the left side of the Java instanceof type pattern is not handled correctly, because it works correctly if the left side is a normal variable.

I went through all Eclipse Code Style settings and could not find an appropriate setting for a whitespace between a method call and instanceof.

My OS version is macOS 12.1, but it seems this is independent of the OS

krusche avatar Dec 23 '21 08:12 krusche

I just played around with different versions, it seems that this issue is introduced in 5.15.1 due to the following update:

Added support and bump Eclipse formatter default versions to 4.20

I was just able to update to the latest version of com.diffplug.spotless 6.0.5 when still using 4.19.0 for the Eclipse formatter. So this might be an issue on the Eclipse side. If someone has a hint, if the configuration needs to be changed, this would be great. Thank you!

krusche avatar Dec 23 '21 08:12 krusche

Seems like a bug in eclipse 4.20.0, glad that eclipse('4.19.0') fixes it for you. Hopefully this gets fixed in 4.22.0, but I'm not sure when we'll add support for that in Spotless.

nedtwigg avatar Dec 23 '21 19:12 nedtwigg

Thanks for the fast feedback, @nedtwigg Do you know if the Eclipse developers are already aware of this issue? Or should we open an issue in their bug tracking system?

krusche avatar Dec 23 '21 19:12 krusche

Do you know if the Eclipse developers are already aware of this issue? Or should we open an issue in their bug tracking system?

I don't know if they are already aware, and if they aren't it would be a good idea to open an issue with them. If you find anything interesting at https://bugs.eclipse.org/bugs/ feel free to post it back here. Spotless integrates with lots of formatters. We don't take responsibility for their bugs, but we are happy to keep issues such as this open so that people can track/coordinate.

nedtwigg avatar Dec 23 '21 20:12 nedtwigg

Looks like this will get fixed when #1059 gets fixed.

nedtwigg avatar Jan 06 '22 17:01 nedtwigg