palantir / palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Apache License 2.0
403 stars 43 forks source link

spotless-maven-palantirJavaFormat : last character not a `\n` -> check fails #975

Open bodote opened 6 months ago

bodote commented 6 months ago

Problem: using palantirJavaFormat with spotless maven : last character not a \n -> check fails

when creating a new java class using Intellij with palantir-java-format for formating all java files AND using spotless with palantirJavaFormat AND the last line in a java file is not an empty line but just } with no \n appended THEN the spotless:check fails

what i would expect instead

I would expect that the check should pass no matter of the \n as the last character in java files, just as the palantir-java-plugin for intellij does not care.

EDIT: and/or I expect that the Intellij palantir-java-plugin adds a \n to the end of each java file all by itself.

Details:

copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible

 <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>${com.diffplug.spotless.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <ratchetFrom>origin/develop</ratchetFrom>
          <java>
            <palantirJavaFormat>
            </palantirJavaFormat>
            <importOrder>
              <order>,java|javax,\#
              </order>  <!-- or use <file>${project.basedir}/eclipse.importorder</file> -->
              <!-- you can use an empty string for all the imports you didn't specify explicitly, '|' to join group without blank line, and '\#` prefix for static imports. -->
            </importOrder>
          </java>
        </configuration>
      </plugin>

copy-paste the full content of any console errors emitted by mvn -e spotless:check

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< myself.clcp:myProject >---------------------
[INFO] Building myProject 0.0.129-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- spotless-maven-plugin:2.41.1:check (default-cli) @ myProject ---
[INFO] Spotless.Java is keeping 22 files clean - 1 needs changes to be clean, 0 were already clean, 21 were skipped because caching determined they were already clean
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.775 s
[INFO] Finished at: 2024-01-07T12:58:43+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.41.1:check (default-cli) on project myProject: The following files had format violations:
[ERROR]     src\test\java\myself\myProject\testing\PalantirFormatTest.java
[ERROR]         @@ -5,4 +5,4 @@
[ERROR]          public·class·PalantirFormatTest·{
[ERROR]          ····@Test
[ERROR]          ····void·t()·{}
[ERROR]         -}
[ERROR]         +}
[ERROR] Run 'mvn spotless:apply' to fix these violations.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.41.1:check (default-cli) on project myProject: The following files had format violations:
    src\test\java\myself\myProject\testing\PalantirFormatTest.java
        @@ -5,4 +5,4 @@
         public·class·PalantirFormatTest·{
         ····@Test
         ····void·t()·{}
        -}
        +}
Run 'mvn spotless:apply' to fix these violations.
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:375)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoExecutionException: The following files had format violations:
    src\test\java\myself\myProject\testing\PalantirFormatTest.java
        @@ -5,4 +5,4 @@
         public·class·PalantirFormatTest·{
         ····@Test
         ····void·t()·{}
        -}
        +}
Run 'mvn spotless:apply' to fix these violations.
    at com.diffplug.spotless.maven.SpotlessCheckMojo.process (SpotlessCheckMojo.java:80)
    at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute (AbstractSpotlessMojo.java:229)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

java file:

import org.junit.jupiter.api.Test;

public class PalantirFormatTest {
    @Test
    void t() {}
}
coolbigbear commented 3 weeks ago

+1 This behaviour is extremely annoying when using palantir intellij plugin and spotless in the CI pipeline. We get errors from not having a new line at the end of classes when the plugin doesn't add it itself.

For reference the google style plugin (google and aosp versions) do add the new line when formatting a file. The spotless check should either not fail when there is or isn't a new line or the plugin should add a new line when using the format shortcut