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

Gradle: Fail Build if not formatted (with compileJava.dependsOn('spotlessApply')) #453

Closed
vbrandl opened this issue Sep 16, 2019 · 6 comments · Fixed by #455
Closed

Gradle: Fail Build if not formatted (with compileJava.dependsOn('spotlessApply')) #453

vbrandl opened this issue Sep 16, 2019 · 6 comments · Fixed by #455

Comments

@vbrandl
Copy link

vbrandl commented Sep 16, 2019

I am using Gradle 5.2 with version 3.24.2 of the spotless plugin.

I want to format JSF XHTML files using the eclipse formatter, using the following config:

...
if (!isCI) {
  project.afterEvaluate {
    compileJava.dependsOn('spotlessApply')
  }
}
...
spotless {
  format 'xml', {
    target '**/*.xhtml'
    eclipseWtp('html')
    indentWithSpaces(2)
    endWithNewline()
    trimTrailingWhitespace()
    paddedCell()
    encoding 'UTF-8'
  }
}

The goal is to automatically format the source on every build, when not in the CI environment but fail, if in the CI environment and the source is not formatted.

Formatting on every build works due to the afterEvaluate block, but when running ./gradlew check or ./gradlew spotlessCheck on unformatted sources, either in the CI environment or with the afterEvaluete block commented out, the build does not fail.

What am I missing? I want check and spotlessCheck to fail, if the sources are not formatted...

@nedtwigg
Copy link
Member

In this case, I don't think we can help without a public repo to reproduce.

Generally speaking, when people have tried to do something.dependsOn('spotlessApply'), they are often surprised that check never fails on their machine, but then fails in CI. And that's because they commit badly-formatted files, then run a build which passes (because spotlessApply changes the files after they were commited), and then they push, and it fails on CI.

It looks like you are carefully avoiding the pitfall above. It looks to me like your code should work. If you make a public repo that reproduces this issue, I'll take a look. But my best guess is that there's some confusion, only because we have had so many issues in the past with this sort of confusion.

@nedtwigg nedtwigg changed the title Gradle: Fail Build if not formatted Gradle: Fail Build if not formatted (with compileJava.dependsOn('spotlessApply')) Sep 16, 2019
@vbrandl
Copy link
Author

vbrandl commented Sep 17, 2019

I created a minimal example to reproduce here: https://github.com/vbrandl/spotless-bad-example

Here is the unformatted file and line: https://github.com/vbrandl/spotless-bad-example/blob/master/src/main/webapp/index.xhtml#L11

Running ./gradlew build formats the file as expected:

$ ./gradlew build
Configuration on demand is an incubating feature.                                                          

> Configure project :                                                                                      
isCI: false                                                                                                

> Task :spotlessXml                                                                                        
> Task :spotlessXmlApply                                                                                   
> Task :spotlessApply                                                                                      
> Task :compileJava NO-SOURCE                                                                              
> Task :processResources NO-SOURCE                                                                         
> Task :classes UP-TO-DATE                                                                                 
> Task :war UP-TO-DATE                                                                                     
> Task :assemble UP-TO-DATE                                                                                
> Task :spotlessXmlCheck                                                                                   
> Task :spotlessCheck                                                                                      
> Task :compileTestJava NO-SOURCE                                                                          
> Task :processTestResources NO-SOURCE                                                                     
> Task :testClasses UP-TO-DATE                                                                             
> Task :test NO-SOURCE                                                                                     
> Task :check                                                                                              
> Task :build                                                                                              

BUILD SUCCESSFUL in 0s                                                                                     
2 actionable tasks: 1 executed, 1 up-to-date

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/main/webapp/index.xhtml

no changes added to commit (use "git add" and/or "git commit -a")

Now I git checkout src to get the unformatted file again and execute CI_JOB_ID=1 ./gradlew check:

$ CI_JOB_ID=1 ./gradlew check
Configuration on demand is an incubating feature.

> Configure project :
isCI: true

> Task :spotlessXml
> Task :spotlessXmlCheck
> Task :spotlessCheck
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

The build task formats index.xhtml as expected but running the build in a (fake) CI environment does not fail on the check task.

@nedtwigg
Copy link
Member

nedtwigg commented Sep 17, 2019

Awesome! I'm able to repro on my machine, thanks for the example. Surprisingly, I think this turns out to be a dup (but much smaller and therefore easier to work with) of #338

TLDR, this will be fixed if you turn off paddedCell. But this should work, and this is a great testcase to help us fix the underlying bug.

@vbrandl
Copy link
Author

vbrandl commented Sep 18, 2019

Turning off paddedCell works in the minimal example but results in an error in our production repo. So this seems like a deadlock until the underlying issue is fixed?

@nedtwigg
Copy link
Member

nedtwigg commented Sep 18, 2019

Yep. Lots of formatters have non-idempotent corner-cases. Looks like your prod repo has one of them. That's what paddedCell is for, but clearly something about it is broken. Your test repo is a perfect testcase for fixing it, and I'm happy to merge any PR's, but I won't get to investigating and implementing a fix myself until this weekend. I expect we should be able to ship a fix on Monday.

@nedtwigg nedtwigg mentioned this issue Sep 23, 2019
3 tasks
@nedtwigg
Copy link
Member

Fixed in 3.24.3. Thanks for the testcase!

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

Successfully merging a pull request may close this issue.

2 participants