ktlint-gradle
ktlint-gradle copied to clipboard
How to exclude files from being formatted?
Using this configuration:
ktlint {
version.set("1.0.1")
android.set(true)
filter {
exclude { it.file.path.contains("/gen/") }
}
}
We have some files in those /gen/ folders that are generated, but also checked in Git. We don't want to format them. Running :ktlintCheck properly ignores them, but :ktlintFormat seems to ignore filters - those generated files get formatted.
Is there an option to apply filters to :ktlintFormat?
Hey. It seems more like a bug to me. Have you found any workaround?
I haven't - for now, we're reverting the changes we don't want after the :ktlintFormat call.
I've found one. I updated the .editorconfig to ignore specific paths.
How did you exclude them? I tried both a .editorconfig in those folders with the following contents:
root = true
[*]
generated_code = true
ij_formatter_enabled = false
Or the following section matching generated files in the top-level .editorconfig:
[**/gen/*]
generated_code = true
ij_formatter_enabled = false
Neither prevented :ktlintFormat to format files in those folders.
EDIT: I realized that 12.1.0 was released after this issue, so I guess I have a separate issue. For me, the exclude filter stopped working in 12.1.0 but works if I revert to 12.0.3.
EDIT: I realized that 12.1.0 was released after this issue, so I guess I have a separate issue. For me, the exclude filter stopped working in 12.1.0 but works if I revert to 12.0.3.
Interesting, I am not aware of any changes made to how filters are applied. This warrants further investigation
I'm also experiencing problems with filters. Both versions 12.1.0 and 12.0.3. Both ktlintFormat and ktlintCheck tasks ignore my filter
ktlint {
filter {
exclude("**/generated/**")
}
}
Still processes Hyperion/common/build/generated/moko/commonMain/src/dev/zt64/hyperion/MR.kt
Tried putting the filename exactly too, and that didn't work
Ultimately, we just use Gradle's built in PaternFilterable
https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html
The behavior of this filter is, admittedly, confusing, but usually it's not our bug when people have issues. It's usually that the filter being specified isn't correct.
I chose to use Gradle's built in filtering with a direct pass-through to the source set to minimize the amount of custom filtering we needed to expose, which many other plugins do, and fail to do correctly.
Unfortunately, I often don't have the answers regarding why it isn't working as expected. Ultimately, these are Gradle APIs talking to other Gradle API'S, without ktlint-gradle in the middle.
If you believe there is a bug in our implementation of how we are using PaternFilterable, please provide a minimal reproducer, and we will see what we can do to fix it.
One debugging pattern I'd suggest is printing every file passed to the exclude {} closure. That may give you an insight into what's not working.
For anyone else who finds this thread looking for a solution, the Ktlint FAQ has a recommendation: https://pinterest.github.io/ktlint/dev-snapshot/faq/#how-do-i-disable-ktlint-for-generated-code
In short, add the following to your .editorconfig, where the path is a relative path:
[some/path/to/generated/code/**/*]
ktlint = disabled
This may not work for everyone's situation, and I think it is unfortunate that I can't just use the Gradle filter, but it was the one thing that worked reliably for me, so I wanted to share in hopes it is useful to others.