ktlint-gradle: Exclude configuration is ignored
On plugin version 10.2.1
the following configuration is ignored:
ktlint {
filter {
exclude("**/generated/**")
}
}
It works fine on version 10.2.0
.
Also tried with
ktlint {
filter {
exclude { element -> element.file.path.contains("generated/") }
}
}
And it yielded the same result.
Might be a regression from #266
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 23
- Comments: 17 (3 by maintainers)
Commits related to this issue
- fix file filtering (fixes #579) — committed to JLLeitschuh/ktlint-gradle by wakingrufus 9 months ago
Weird behavior: setting the source set manually as in https://github.com/JLLeitschuh/ktlint-gradle/issues/579#issuecomment-1297402108 doesn’t work:
What does work is evaluating
source
in adoFirst
block:Eagerly evaluating the argument to
setSource
, i.e. by callingtoList()
, doesn’t work.It seems to me that the intermediate
source
collection should be removed, as the filter should be applied as late as possible andsourceFiles
should be evaluated lazily in a task action instead of hoping Gradle does the right thing.you can put logger statements inside of filter, and log
it
to help. This is working for me, official fix is needed though, at least the docs if nothing more.There are several issues about filtered file tree on gradle < 7.5 version
Although it is difficult to identify the exact cause of the issue, updating the Gradle version(7.5) will solve the problem.
Thanks. I’ll look into this
+1’d. Working with a file pattern is not working for me, too. I gave fixing this bug a shot, but working with Gradle’s APIs gave me a headache. I think this is the offending line:
https://github.com/JLLeitschuh/ktlint-gradle/blob/1b5b4e7c328e89a8486a0d1776b05d5d943513f8/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt#L113
Because we cannot pass
getAsFileTree()
a root directory for the file tree, filtering by relative path, e.g.,**/generated/**
, does not work. We would need to work out a way to convert theFileCollection
into aFileTree
with a root, for example the project directory, which will be the base for relative path patterns to match against.Lastly, we may defend against regression by making the exclusion pattern in
KtlintPluginTest#ignoreExcludedSources
more complicated, for example as in:I had a similar issue in my project. In my case I was configuring src dirs after project evaluation, so the sources were getting added after the filter was applied. At least that was my hypothesis, I didn’t dig into the sources to confirm it. But what helped solving it - is to configure src dirs lazily at config time.
E.g.:
Before the change:
After the change: