Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to exclude files from being formatted? #724

Open
Kernald opened this issue Nov 26, 2023 · 9 comments
Open

How to exclude files from being formatted? #724

Kernald opened this issue Nov 26, 2023 · 9 comments
Assignees

Comments

@Kernald
Copy link

Kernald commented Nov 26, 2023

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?

@JanCizmar
Copy link

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

@wakingrufus wakingrufus self-assigned this Dec 30, 2023
@Kernald
Copy link
Author

Kernald commented Jan 1, 2024

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

@JanCizmar
Copy link

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

@Kernald
Copy link
Author

Kernald commented Jan 3, 2024

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.

@mateot1
Copy link

mateot1 commented Jan 17, 2024

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.

@wakingrufus
Copy link
Collaborator

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

@zt64
Copy link

zt64 commented Jan 21, 2024

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

@JLLeitschuh
Copy link
Owner

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.

@adamsmd
Copy link

adamsmd commented Jun 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants