kotlinter-gradle icon indicating copy to clipboard operation
kotlinter-gradle copied to clipboard

Kotlinter - configuring excluded files

Open Sethuraman opened this issue 2 years ago • 4 comments

This is a follow up to the previously raised issue here - https://github.com/jeremymailen/kotlinter-gradle/issues/208

The issue is it is very hard to ignore files baed on the package name. There are 2 problems with this approach:

  1. The package name being ignored could be used in the src code as well. So you might inadvertently ignore code you wrote instead of ignoring just the ones that have been generated.
  2. At times when you are generating code for something such as protobufs, we pull in packages based on what the proto files depend on. Whenever a proto on a new package is pulled in, we need to edit the exclusions list again.

It would be great to do a blanket exclusion of all files in the generated folder.

Would be grateful to have the below config working:

tasks.lintKotlinMain {
  exclude("**/generated/**")
}

Sethuraman avatar Mar 20 '22 21:03 Sethuraman

Huh, I still haven't understood the root cause (the "the way SourceTask works is matching the package part of the path" part), but I can think of a workaround other than filtering by package:

tasks.named("lintKotlinMain") {
    source = source - fileTree("$buildDir/generated")
}
tasks.named("formatKotlinMain") {
    source = source - fileTree("$buildDir/generated")
}

It worked for me with apollo generated files, maybe it'll work for your case as well? 🤞 This doesn't rely on the exclude mechanism, but removes files under /generated a step earlier

mateuszkwiecinski avatar Mar 21 '22 22:03 mateuszkwiecinski

Im facing the same issue I have my source project and outside my source poject in the build folder, im importing a lot of classed auto generated by a framework. I cant find any way to ignore those files, in ktlint gradle I do this and it works, but here it doesnt:

filter {
        exclude { element ->
            element.file.path.contains("generated/")
        }
    }

Remember this classes and folder is in the build directory, and not in the main source path

EDIT: Found a solution

tasks.formatKotlinMain {
    exclude { it.file.path.contains("generated/")}
}

tasks.lintKotlinMain {
    exclude { it.file.path.contains("generated/")}
}

ghost avatar May 23 '22 11:05 ghost

Yes the FileTreeElement is probably what you want, we do:

it.exclude { fte -> fte.file.absolutePath.contains("/build/") }

chrisjenx avatar Sep 13 '22 14:09 chrisjenx

It worked for me with apollo generated files, maybe it'll work for your case as well? crossed_fingers This doesn't rely on the exclude mechanism, but removes files under /generated a step earlier

This work nicely and doesn't trigger the disabled optimization warning.

aaulia avatar Nov 10 '22 13:11 aaulia

Ok, one style is now documented in the readme and it seems like people have ways to target the files they want. Closing for now.

jeremymailen avatar Mar 14 '23 16:03 jeremymailen

Exclude in Kotlin Gradle:

tasks.withType<LintTask> {
    exclude("generated/")
}

tasks.withType<FormatTask> {
    exclude("generated/")
}

igorwojda avatar Feb 19 '24 14:02 igorwojda