Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task spotlessJava not triggering when only formatting code #127

Closed
carcaret opened this issue Jul 28, 2017 · 9 comments
Closed

Task spotlessJava not triggering when only formatting code #127

carcaret opened this issue Jul 28, 2017 · 9 comments
Labels

Comments

@carcaret
Copy link

I'm not sure if this has been discussed before or it's intended behavior.

I've just added the plugin to an existing project for code formatting. When running a new build, I've noticed that this configuration is not triggering the spotlessJava task, so the build is successful even though the code is badly formatted:

spotless {
    java {
        eclipseFormatFile 'format/eclipse-java-google-style.xml'
    }
}

However, adding for instance a call to removeUnusedImports() before seems to trigger spotlessJava task successfully and the build therefore fails as intended since the code is not formatted:

spotless {
    java {
        removeUnusedImports()
        eclipseFormatFile 'format/eclipse-java-google-style.xml'
    }
}

Shouldn't the first configuration trigger the spotlessJava task and make the build fail?

I'm using Gradle v3.4.1 and Java 1.8.0_131-b11

@carcaret
Copy link
Author

carcaret commented Jul 28, 2017

I've seen that eclipseFormatFile is deprecated. I've tried with the eclipse() method and still happens in a similar way.
These configurations triggers the spotlessJava task:

spotless {
    java {
        eclipse().configFile 'format/eclipse-java-google-style.xml'
    }
}

spotless {
    java {
        removeUnusedImports()
        eclipse().configFile 'format/eclipse-java-google-style.xml'
    }
}

While this one doesn't:

spotless {
    java {
        eclipse().configFile 'format/eclipse-java-google-style.xml'
        removeUnusedImports()
    }
}

@jbduncan
Copy link
Member

jbduncan commented Jul 28, 2017

Hi @carcaret, glad to see that you managed to resolve your problem.

On a kind of related note, I highly encourage you to use the googleJavaFormat() option instead of eclipse().configFile 'format/eclipse-java-google-style.xml', as the Eclipse formatting file for Google Java Style is merely an approximation; googleJavaFormat() uses https://github.com/google/google-java-format in the background, which is a custom formatter made by Google themselves for conforming to Google Java Style as closely as possible.

@nedtwigg nedtwigg added the bug label Jul 28, 2017
@nedtwigg
Copy link
Member

Any chance we can get a link to the project you're using it on?

fvgh referenced this issue Jul 28, 2017
Replaced importOrder array argument by varargs. Fixes #125.
@fvgh
Copy link
Member

fvgh commented Jul 28, 2017

@carcaret : I am responsible for the code change #109, declaring eclipseFormatFile deprecated.
Since I just made a similar change for another method, I just want to ensure that I haven't missed something. I would appreciate if you could help to clarify the issue.
I tried the following set-up:

buildscript {
    repositories { mavenCentral() }
    dependencies {  classpath 'com.diffplug.spotless:spotless-plugin-gradle:3.4.1' }
}
apply plugin: 'com.diffplug.gradle.spotless'
apply plugin: 'java'
spotless {
    java { eclipseFormatFile 'format/eclipse-java-google-style.xml' }
}

I got:

.../testbed_spotless$ gradle build
'eclipseFormatFile [version] <file>' is deprecated.
Use 'eclipse([version]).configFile(<file>)' instead.
For details see https://github.com/diffplug/spotless/tree/master/plugin-gradle#applying-to-java-source

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:spotlessJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':spotlessJava'.
> The following files had format violations:
      src/main/java/testbed_spotless/test.java
          @@ -1,3 +1,4 @@
           package·testbed_spotless;
           
          -class·Test·{};
          +class·Test·{
          +};
  Run 'gradlew spotlessApply' to fix these violations.

I am using a slightly different gradle and java version, but that should not have cause the issue.

Anything else you can think of, which is different in your set-up?
Thanks in advance for your time and effort!

@carcaret
Copy link
Author

I´ve tried to reproduce the issue at home in a simple project with no success.
On the other hand, I might be able to share the original build.gradle file with you. Let me get back to you on Monday.

@fvgh
Copy link
Member

fvgh commented Jul 29, 2017 via email

@carcaret
Copy link
Author

I've been trying with no success to reproduce the error using the original build.gradle in a different project. Actually, copying exactly the same project to a new folder and launching gradle clean build is working fine regardless the order.
I guess the issue had something to do with my file system and something maybe corrupted or in an unstable state. Guess you can close the issue, sorry for the trouble.

@fvgh
Copy link
Member

fvgh commented Jul 31, 2017 via email

@nedtwigg
Copy link
Member

We do some tricky stuff to make up-to-date checking work. spotlessCheck and spotlessApply are zero-work marker tasks that configure spotless, which does the actual work. It mighta seemed like the task you asked for wasn't running, when it actually was. Regardless, all good :) Reopen if you're able to reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants