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

[1.20.0 regression] Task detektMain does not exclude build directory anymore #4743

Closed
leinardi opened this issue Apr 21, 2022 · 8 comments
Closed
Labels

Comments

@leinardi
Copy link

leinardi commented Apr 21, 2022

Expected Behavior

All the content of .*/build/.* should be automatically ignored by the task detektMain.

Observed Behavior

detektMain is checking for violations inside the <module>/build/generated/ directory.

Steps to Reproduce

  1. Clone this repo and checkout the detekt branch: https://github.com/leinardi/Forlago/tree/detekt
  2. run ./gradlew detektMain
> Task :modules:core-network:detektDebug FAILED
style - 25min debt
        UtilityClassWithPublicConstructor - [The class GraphQLString only contains utility functions. Consider defining it as(…)] at /home/rleinardi/Workspace/My/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLString.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLBoolean only contains utility functions. Consider defining it a(…)] at /home/rleinardi/Workspace/My/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLBoolean.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLID only contains utility functions. Consider defining it as an (…)] at /home/rleinardi/Workspace/My/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLID.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLFloat only contains utility functions. Consider defining it as (…)] at /home/rleinardi/Workspace/My/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLFloat.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLInt only contains utility functions. Consider defining it as an(…)] at /home/rleinardi/Workspace/My/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLInt.kt:10:1
> IDLE
Overall debt: 25min

Context

Your Environment

@leinardi leinardi added the bug label Apr 21, 2022
@BraisGabin
Copy link
Member

Do you mean 1.20.0? Or 0.20.0?

@leinardi leinardi changed the title [0.20.0 regression] Task detektMain does not exclude build directory anymore [1.20.0 regression] Task detektMain does not exclude build directory anymore Apr 21, 2022
@leinardi
Copy link
Author

Sorry it was a typo: I mean 1.20.0, the latest stable release.

@leinardi
Copy link
Author

Is there a known workaround for this issue? How can I manually exclude the build folder from the detektMain task?

@cortinico
Copy link
Member

Thanks for sharing your project.
That seems to be a bug, though I wasn't able to point out the root cause 🤔

My gut feeling was that this would have worked:

tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
    exclude("**/build/**")
    include("**/*.kt")
}
tasks.withType(io.gitlab.arturbosch.detekt.DetektCreateBaselineTask).configureEach {
    exclude("**/build/**")
    include("**/*.kt")
}

Sadly this didn't worked for me.

There seems to be some problems on how the source input is populated.

The problem here is also that your project has a non trivial setup, so it's making harder to understand where the cause could. Having a minimal reproducer would help investigating.

@leinardi
Copy link
Author

Ciao @cortinico, I've just pushed an update to the branch with the minimal reproducer that you asked for.

Now there is only one empty app module (no Java/Kotlin code at all) and an empty core-network module (no Java/Kotlin code at all) that is just generating some code using Apollo Graphql plugin.

Detekt still fails because it validates the generated code:

> Task :modules:core-network:detektDebug FAILED
style - 25min debt
        UtilityClassWithPublicConstructor - [The class GraphQLInt only contains utility functions. Consider defining it as an(…)] at /home/leinardi/Workspace/github/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLInt.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLBoolean only contains utility functions. Consider defining it a(…)] at /home/leinardi/Workspace/github/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLBoolean.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLID only contains utility functions. Consider defining it as an (…)] at /home/leinardi/Workspace/github/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLID.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLFloat only contains utility functions. Consider defining it as (…)] at /home/leinardi/Workspace/github/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLFloat.kt:10:1
        UtilityClassWithPublicConstructor - [The class GraphQLString only contains utility functions. Consider defining it as(…)] at /home/leinardi/Workspace/github/Forlago/modules/core-network/build/generated/source/apollo/service/com/leinardi/forlago/core/network/graphql/type/GraphQLString.kt:10:1

Overall debt: 25min

All the Detekt Gradle plugin config is here: https://github.com/leinardi/Forlago/blob/1a802fa1fce78fab1d918b033de5266768d09358/modules/core-network/build.gradle#L110

The YML config is still here: https://github.com/leinardi/Forlago/blob/detekt/config/detekt/detekt.yml

Please let me know if I can help in some other way.

@cortinico
Copy link
Member

cortinico commented Apr 24, 2022

Thanks for minimal reproduce. That helped me understantd what's going on.

So I don't think this is a regression, but more like a side effect of how Detekt, AGP and Apollo interact each other (cc @martinbonnin for visibility).

The "offending" change would be this one:

We now populate the Detekt Source input using the srcDirs.java and srcDirs.kotlin. Those are the folders that are currently populated for your module:

/Users/.../Forlago/modules/core-network/src/main/kotlin
/Users/.../Forlago/modules/core-network/src/main/java
/Users/.../Forlago/modules/core-network/build/generated/source/apollo/service
/Users/.../Forlago/modules/core-network/src/debug/java
/Users/.../Forlago/modules/core-network/src/debug/kotlin

I believe the Apollo Gradle Plugin is populating the Kotiln source set in some form.

On Detekt, we then allow users to exclude/include folders using the snippets I mentioned above.
The problem is that exclude/include are processed by Gradle from the root of the folder, so the exclude("**/build/**") is essentially ignored.

The current solution for you would be to either override the task source, so you won't get the autopopulated one:

tasks.withType(io.gitlab.arturbosch.detekt.Detekt) {
    source("src/main/kotlin", "src/main/java")
}

or you can exclude a package path if it's well known and restricted to code-generation only (which btw it's a good practice).

tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
    exclude("com/leinardi/forlago/core/network/graphql/**/*.kt")
}

Closing as there is nothing we can do on our end, unless we want to hardcode some known paths (i.e. build/) inside the Gradle Plugin/CLI and filter them. But this will make the tool not configurable.

@leinardi
Copy link
Author

leinardi commented May 2, 2022

Sorry for the late reply, it has been a busy week at work. Thank you for suggesting a couple of fixes, I went with the exclusion of the code generated package since I also that was the better solution.

@matejdro
Copy link
Contributor

matejdro commented Jun 29, 2023

This seems to be a better way of doing this:

tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
   exclude {
      it.file.absolutePath.contains("build/")
   }
}

It filters build folder directly, so it is not dependent on specific package (for example, in our case, we had same package in both regular sources and generated source).

I'm wondering though, why do some generators (such as Moshi and Anvil) get properly excluded out of the box, while others (such as Apollo or Sqldelight) do not? Do those generators register their sources in a different way?

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

No branches or pull requests

4 participants