spotless
spotless copied to clipboard
Spotless fails to format .gitignore
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
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
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.
I considered 3 files:
.gitignore: KOa.gitignore: OK.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:

I get me to:
- https://github.com/gradle/gradle/issues/11176
- 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)
Wow! Amazing debugging and workaround @blacelle!