Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlinter - configuring excluded files #242

Closed
Sethuraman opened this issue Mar 20, 2022 · 7 comments
Closed

Kotlinter - configuring excluded files #242

Sethuraman opened this issue Mar 20, 2022 · 7 comments

Comments

@Sethuraman
Copy link

This is a follow up to the previously raised issue here - #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/**")
}
@mateuszkwiecinski
Copy link
Contributor

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

@ghost
Copy link

ghost commented May 23, 2022

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/")}
}

@chrisjenx
Copy link

chrisjenx commented Sep 13, 2022

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

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

@aaulia
Copy link

aaulia commented Nov 10, 2022

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.

jeremymailen added a commit that referenced this issue Mar 14, 2023
- Fix issue #316 by upgrading kotlinter gradle's released version to ktlint 0.48.2.
- Remove configuration for `disabledRules` and `experimentalRules` to fully delegate this to .editorconfig
- Fix issue with resolution of rule enable / disable statements in .editorconfig
- Address issue #242 by providing readme example of excluding source files at the project tree level
- Address issue #314 by including sources in published artifacts
- Fix issue #313 by exporting `ReporterType`
- Address issue #315 by logging format log statements at level `WARNING` so they can be hidden
- Credit @mateuszkwiecinski as one of the developers of the plugin
jeremymailen added a commit that referenced this issue Mar 14, 2023
- Fix issue #316 by upgrading kotlinter gradle's released version to ktlint 0.48.2.
- Remove configuration for `disabledRules` and `experimentalRules` to fully delegate this to .editorconfig
- Fix issue with resolution of rule enable / disable statements in .editorconfig
- Address issue #242 by providing readme example of excluding source files at the project tree level
- Address issue #314 by including sources in published artifacts
- Fix issue #313 by exporting `ReporterType`
- Address issue #315 by logging format log statements at level `WARNING` so they can be hidden
- Credit @mateuszkwiecinski as one of the developers of the plugin
@jeremymailen
Copy link
Owner

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.

@igorwojda
Copy link

igorwojda commented Feb 19, 2024

Exclude in Kotlin Gradle:

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

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

@TrueFiskibility
Copy link

Even more DRY:

import org.jmailen.gradle.kotlinter.tasks.ConfigurableKtLintTask

withType<ConfigurableKtLintTask> {
    exclude("generated/")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants