spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Allow `targetExclude` to be passed a directory (`Directory`, `DirectoryProperty`, `Provider<Directory>`, etc.)

Open bric3 opened this issue 2 months ago • 2 comments

One could be tempted to exclude a directory via targetExclude, e.g. project.layout.buildDirectory

spotless {
  scala {
    toggleOffOn()
    targetExclude(project.layout.buildDirectory)
    scalafmt('2.7.5').configFile(configPath + '/enforcement/spotless-scalafmt.conf')
  }
}

However this doesn't work as it's not handled specifically. So when the task is configured, it uses the dir as a regular "file", and the operation here does nothing (target and targetExclude are FileCollection)

https://github.com/diffplug/spotless/blob/d3a160d8d9f3576cca751344e91d14b0d33beb2e/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java#L1078

The alternative is to expand this to a FileTree before passing it to targetExclude. In this case the operation above correctly removes the files nested under the directory.

-    targetExclude(project.layout.buildDirectory)
+    targetExclude(project.layout.buildDirectory.asFileTree)

However, I believe that spotless could do that automatically, if the type is known to represent a directory.


On another note I tried to pass a Provider<String> (targetExclude(genDirectory.map { it.asFile.path + "/**/*.scala" })), but this was interpreted as a single file (because the value is passed to project.files(...)).

bric3 avatar Oct 08 '25 13:10 bric3

seems like a good idea, PR's welcome

nedtwigg avatar Oct 08 '25 15:10 nedtwigg

Having not fully understood the issue, I’m wondering if it’s related to normalizing the directories as well.

I’m also assuming this approach would only handle one level of directories. If a directory contains subdirectories, those wouldn’t be handled correctly, and the same issue would likely occur.

  • https://github.com/diffplug/spotless/pull/2691

Pankraz76 avatar Oct 18 '25 12:10 Pankraz76