JLLeitschuh / ktlint-gradle

A ktlint gradle plugin
MIT License
1.48k stars 163 forks source link

Cannot apply to buildSrc #743

Open pkubowicz opened 10 months ago

pkubowicz commented 10 months ago

When I add the plugin to buildSrc/build.gradle.kts:

plugins {
    `kotlin-dsl`
    id("org.jlleitschuh.gradle.ktlint") version "12.0.3"
}

configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
    version.set("1.1.1")
    filter {
        include("**/*.kts")
        exclude { element -> element.file.path.contains("/generated-sources/") }
    }
}

it fails on classes generated by Gradle, even though I explicitly instruct the plugin not to include them.

buildSrc/build/generated-sources/kotlin-dsl-external-plugin-spec-builders/kotlin/gradle/kotlin/dsl/plugins/_bf0f0d1de34ae2a280c4b3e0ab359712/PluginSpecBuilders.kt:32:9 Package name must not contain underscore (cannot be auto-corrected) (standard:package-name)
JLLeitschuh commented 10 months ago

Can you share a build scan?

Also, a minor reproducer would be nice.

@wakingrufus we could probably automatically filter out this directory when the kotlin-dsl plugin is applied. Thoughts?

pkubowicz commented 10 months ago

The scan is here: https://scans.gradle.com/s/enzehbyek47qs

Attached a simple reproducer. gradle-kotlin-buildsrc.zip

wakingrufus commented 10 months ago

Can you share a build scan?

Also, a minor reproducer would be nice.

@wakingrufus we could probably automatically filter out this directory when the kotlin-dsl plugin is applied. Thoughts?

We could. I don't see this problem in regular projects using the kotlin-dsl plugin, so what is different about buildSrc that causes this?

xenomachina commented 7 months ago

tl;dr: It looks like something is getting cached in buildSrc/.gradle/buildOutputCleanup/ that's preventing changes to excludes from being honored.


I ran into this same problem: ktlint was reporting errors in the Kotlin code that Gradle generates in buildSrc/. I added an exclude like this...

ktlint {
    filter {
        exclude {
            it.file.relativeTo(projectDir).startsWith(File("build"))
        }
    }
}

...and it continued reporting these errors. Eventually I used git clean to delete all ignored files from my repo, and then it started working.

I was able to narrow the problem down to buildSrc/.gradle/buildOutputCleanup/ by conducting the following series of tests:

"WORKS" and "FAILS" indicate the outcome of running ./gradlew :buildSrc:ktlintCheck after the corresponding operation.