ktlint-gradle icon indicating copy to clipboard operation
ktlint-gradle copied to clipboard

How to exclude files from being formatted?

Open Kernald opened this issue 2 years ago • 10 comments

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?

Kernald avatar Nov 26 '23 22:11 Kernald

Hey. It seems more like a bug to me. Have you found any workaround?

JanCizmar avatar Dec 30 '23 15:12 JanCizmar

I haven't - for now, we're reverting the changes we don't want after the :ktlintFormat call.

Kernald avatar Jan 01 '24 23:01 Kernald

I've found one. I updated the .editorconfig to ignore specific paths.

JanCizmar avatar Jan 02 '24 07:01 JanCizmar

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.

Kernald avatar Jan 03 '24 00:01 Kernald

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.

mateot1 avatar Jan 17 '24 18:01 mateot1

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

wakingrufus avatar Jan 18 '24 22:01 wakingrufus

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

zt64 avatar Jan 21 '24 08:01 zt64

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.

JLLeitschuh avatar Jan 22 '24 01:01 JLLeitschuh

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.

adamsmd avatar Jun 01 '24 06:06 adamsmd