kotlinter-gradle
kotlinter-gradle copied to clipboard
Kotlinter - configuring excluded files
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:
- 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.
- 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/**")
}
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
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/")}
}
Yes the FileTreeElement is probably what you want, we do:
it.exclude { fte -> fte.file.absolutePath.contains("/build/") }
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.
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.
Exclude in Kotlin Gradle:
tasks.withType<LintTask> {
exclude("generated/")
}
tasks.withType<FormatTask> {
exclude("generated/")
}