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

Most upvoted comments

Weird behavior: setting the source set manually as in https://github.com/JLLeitschuh/ktlint-gradle/issues/579#issuecomment-1297402108 doesn’t work:

tasks {
    runKtlintCheckOverCommonMainSourceSet {
        setSource(
            kotlin.sourceSets.commonMain.map {
                it.kotlin.filter { file ->
                    file.toRelativeString(projectDir) != "src/commonMain/kotlin/Models.kt"
                }
            }
        )
    }
}

What does work is evaluating source in a doFirst block:

tasks {
    runKtlintCheckOverCommonMainSourceSet {
        setSource(
            kotlin.sourceSets.commonMain.map {
                it.kotlin.filter { file ->
                    file.toRelativeString(projectDir) != "src/commonMain/kotlin/Models.kt"
                }
            }
        )

        doFirst {
            source.forEach { println(it) }
        }
    }
}

Eagerly evaluating the argument to setSource, i.e. by calling toList(), 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 and sourceFiles 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.

ktlint {
  filter {
    exclude { it.file.path.contains("generated") }
  }
}

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 the FileCollection into a FileTree 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:

ktlint.filter { exclude("**/src/**/fail-*.kt") }

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:

project.afterEvaluate {
    sourceSets.main {
        java {
            srcDirs(tasks.withType<CodegenTask>())
        }
    }
}

After the change:

project.sourceSets.main {
    java {
        srcDirs(tasks.withType<CodegenTask>())
    }
}