spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Spotless fails to format .gitignore

Open es80 opened this issue 3 years ago • 2 comments

Hello :wave:,

I was trying out Spotless for Gradle today and found that if failed to format my .gitignore. A minimal example would use the following build.gradle:

plugins {
  id "com.diffplug.spotless" version "6.3.0"
}

spotless {
  format 'misc', {
    target '.gitignore'
    trimTrailingWhitespace()
    indentWithTabs()
    endWithNewline()
  }
}

Then introduce any formatting violation in .gitignore and the spotlessCheck, spotlessApply, spotlessMiscCheck etc. don't pick it up. Any other filename appears to work fine.

Spotless 6.3.0 Gradle 7.4.1 Java 11 and 17 Linux and macOS

es80 avatar Mar 11 '22 19:03 es80

Strange, I would expect this to work. We do some fancy stuff when parsing the target to make sure that **/* doesn't format stuff inside the .git directory for example, but that shouldn't affect the example you're describing. I'm not sure what's going on...

https://github.com/diffplug/spotless/blob/1ce515b061f0f2a0bfa0d807dec1783cf91b151f/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java#L244-L258

nedtwigg avatar Mar 11 '22 21:03 nedtwigg

This issue happened to me as well. It does not fail when when the filename starts with .git I've tested renaming to .agitignore and gitignore and both failed as expected.

Mavbraz avatar Jul 26 '22 01:07 Mavbraz

I considered 3 files:

  1. .gitignore: KO
  2. a.gitignore: OK
  3. .gitignorea: OK

and the template: target '*.gitignore*'

While debugging with ./gradlew spotlessApply --debug --no-build-cache --rerun-tasks -Dorg.gradle.jvmargs='-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=5005,suspend=y', I see:

image

I get me to:

  1. https://github.com/gradle/gradle/issues/11176
  2. https://github.com/gradle/gradle/issues/13427

Hence, you can fix this issue by adding:

org.apache.tools.ant.DirectoryScanner.removeDefaultExclude("**/.gitignore")

in some settings.gradle file (confirmed in my reproduction scenario)

blacelle avatar Mar 09 '23 06:03 blacelle

Wow! Amazing debugging and workaround @blacelle!

nedtwigg avatar Mar 10 '23 05:03 nedtwigg