jeremymailen / kotlinter-gradle

Painless, fast ktlint plugin for Gradle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wired issues when Upgrading to Kotlin 1.8.20

hantsy opened this issue · comments

When upgrading to Kotlin 1.8.20 and Kotlin DataFramework 0.10.0(which is compatible with Kotlin 1.8.20 and KSP plugin for Kotlin 1.8.20), my original config does not work.

tasks.named<LintTask>("lintKotlinMain") {
    dependsOn("kspKotlin")
    exclude("io/etip/**/jooq/**/*.kt") // excludes Jooq generated data classes
    exclude("**/*.Generated.kt")  // exludes dataframe generated codes
    exclude("**/*\$Extensions.kt")
}

tasks.named<FormatTask>("formatKotlinMain") {
    dependsOn("kspKotlin")
    exclude("io/etip/**/jooq/**/*.kt") // excludes Jooq generated data classes
    exclude("**/*.Generated.kt") // exludes dataframe generated codes
    exclude("**/*\$Extensions.kt")
}

Now the KSP generated codes(used by DataFramework) is always scanned by lint/format, see Kotlin/dataframe#349 and Kotlin/dataframe#348

Use ./gradlew tasks --all, it includes two tasks like this:

formatKotlinGeneratedByKspKotlin
formatKotlinGeneratedByKspTestKotlin

But I have tried to configure formatKotlin to skip generated codes for these tasks, it does not work.

Now the problem is kspPlugin will fail every time see Kotlin/dataframe#349 when running git push and stop me to commit codes.

If possible to configure a global filter to exclude the paths like https://github.com/JLLeitschuh/ktlint-gradle, eg

kotlinter {
    filter {
        exclude("**/generated/**")
    }
}

If KSP now creates individual sourceSets for each body of generated code, you should be able to just disable those two tasks: formatKotlinGeneratedByKspKotlin and formatKotlinGeneratedByKspTestKotlin, which is actually a simpler setup than using excludes within a task.

If that isn't suitable, have you tried the method for excluding source paths shown here:
https://github.com/jeremymailen/kotlinter-gradle#customizing-tasks

Yes, I have tried to add the following config to all lint/format tasks. It does not work, the KSP generated codes are still linted/foramatted .

dependsOn("kspPlugin") // without this, it will fail to build with Gradle 8
// I have to convert the type `FileCollection` to `FileTree` here.
source = (source - fileTree("$buildDir/generated")).asFileTree 

BTW, I am using Gradle 8.x.
Check the original post here: Kotlin/dataframe#348

But have you tried simply doing this? It sounds like KSP has gathered generated code into separate sourceSets, which is good and easy to deal with:

tasks.named("formatKotlinGeneratedByKspKotlin").configure {
    enabled = false
}

tasks.named("formatKotlinGeneratedByKspTestKotlin").configure {
    enabled = false
}

Exception when adding this in the gradle build.

Task with name 'formatKotlinGeneratedByKspKotlin' not found in root project 'my-backend'.
	at org.gradle.api.internal.tasks.DefaultTaskCollection.createNotFoundException(DefaultTaskCollection.java:102)
	at org.gradle.api.internal.tasks.DefaultTaskCollection.createNotFoundException(DefaultTaskCollection.java:46)
	at org.gradle.api.internal.DefaultNamedDomainObjectCollection.named(DefaultNamedDomainObjectCollection.java:360)
	at org.gradle.api.internal.tasks.DefaultTaskCollection.named(DefaultTaskCollection.java:112)

Add the latest ksp plugin explicitly(in fact it is used in the Kotlin DataFrame, we have not used it directly) in our project, but still can not exclude the KSP generated codes through all methods mentioned in above comments.

tasks.whenTaskAdded {
    // println("adding task: $name")
    if (name == "lintKotlinGeneratedByKspKotlin" ||
        name == "lintKotlinGeneratedByKspTestKotlin" ||
        name == "formatKotlinGeneratedByKspKotlin" ||
        name == "formatKotlinGeneratedByKspTestKotlin"
    ) {
        enabled = false
    }
}

This code fragment skipped the tasks in the build lifecycle.

Yes, I think in the right module scope and lazy configuration, enable = false on each task should have also worked.
But overall, yes, that is the solution.

I'd still appreciate if kotlinter would have some configuration to exclude source sets it creates unwanted tasks for. Are patches accepted here?