https://kotlinlang.org logo
#detekt
Title
# detekt
k

Karol Ksionek

07/29/2021, 10:55 AM
Hey, I need to ask that question, as it already takes too long for me to solve this. So I’m having an android project with multiple modules and I want to execute detekt for all modules, but skipping ‘build’ and ‘resources’ directories. Hence my configuration
Copy code
detekt {
    input = files(escapedProjectDir)
    baseline = file("$escapedRootProjectDir/config/detekt/detekt-baseline.xml")
    config = files("$escapedRootProjectDir/config/detekt/detekt.yml")
    autoCorrect = false
    parallel = true
}

tasks.withType(io.gitlab.arturbosch.detekt.Detekt) {
    exclude '**/resources/**'
    exclude '**/build/**'
}
But it keeps checking build directory. What’s wrong? I’m using detekt 1.18.0-RC2
g

gammax

07/29/2021, 11:42 AM
So I couple of questions: • What is
escapedProjectDir
? • Why do you even need this custom configuration? Detekt will not scan the
build
folder by default.
k

Karol Ksionek

07/29/2021, 11:44 AM
by default it will use ‘src/main/kotlin’ and I care for all flavors, ’src/test/, not to mention the fact that my project’s source code is still in ‘src/main/java’
escpedProjectDir is valid path, pointing to project root
escapedProjectDir is /Users/ksionekk/AndroidStudioProjects/project-android/ and detekt finds an issue MaximumLineLength - [] at /Users/ksionekk/AndroidStudioProjects/project-android/auth/build/generated/source/kaptKotlin/debug/com/example/app/auth/api/authcheck/AuthCheckInfo_AuthorizedJsonAdapter.kt221
g

gammax

07/29/2021, 12:08 PM
I see your situation. Just out of curiousity: have you considered using the other tasks such as
detektDebug
,
detetkRelease
,
detetk<Flavor>
as they have the input folders already set up for you.
k

Karol Ksionek

07/29/2021, 12:11 PM
I’d like to stay with config that we had before, when using detekt 1.5.1, so task “detekt” running over all sources
g

gammax

07/29/2021, 12:13 PM
Just as a FYI, detekt-gradle-plugin is designed with a
per-project
approach (i.e. one task per every module, according to the Gradle philosophy). Having only a single task that runs over all your codebase can lead to situation like the one you’re facing. Anyway, I believe you could try to replace your
exclude **/build/**
with:
Copy code
exclude { element -> element.file.path.contains("build/") }
Please let me know if this helps 👍
k

Karol Ksionek

07/29/2021, 12:23 PM
It doesn’t work for me. I’ve changed it to
Copy code
tasks.withType(io.gitlab.arturbosch.detekt.Detekt) {
    exclude { element ->
        println("DEBUG $element")
        element.file.path.contains("build/")
    }
}
and DEBUG isn’t ever printed to the console
g

gammax

07/29/2021, 12:26 PM
tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach{
☝️ I believe you missed a
configureEach{
k

Karol Ksionek

07/29/2021, 12:36 PM
even with
configureEach
issues are being reported
and still => no “DEBUG” in logs
g

gammax

07/29/2021, 12:37 PM
Ok I see. To help you further I need you to provide some sort of reproducer. If your project is open-source that would be 👌 If it’s not, you can use this template github.com/cortinico/kotlin-android-template/ and try to recreate your setup.
k

Karol Ksionek

07/29/2021, 12:51 PM
ok, will do. BTW I’m using Gradle 6.6
g

gammax

07/29/2021, 1:58 PM
Awesome. Looking into it
👌 1
So the problem with the project you linked is that you’re applying Detekt inside
subprojects{}
and you’re running
tasks.withType…
at the top level project (that doesn’t have Detekt applied).
So if you move the
tasks.withType
inside the
subprojects{}
block it will work:
Copy code
subprojects {
    apply {
        plugin("io.gitlab.arturbosch.detekt")
        plugin("com.github.ben-manes.versions")
    }

    detekt {
        input = files(escapedProjectDir)
        config = rootProject.files("config/detekt/detekt.yml")
        parallel = true
    }

    tasks {
        withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
            exclude("**/build/**")
        }
    }
}
Is it working? @Karol Ksionek
Also as a side note: If you’re using a similar approach in your project as well, that’s extremely inefficient. Specifically you’re duplicating the detekt work a lot of times, once for every module you have. This means that if you have say 50 modules, you’r running Detekt on the whole codebase 50 times. Gradle will fail the first time if there is a violation, but in case of success it will actually run all the times.
k

Karol Ksionek

07/29/2021, 8:18 PM
Yeah, sorry, I’ve missed the fact that my detekt configuration is on main level, not in subprojects section. So I’ve prepared wrong example, but since it started to work I’ve assumed that’s it. Few minutes ago I’ve found what the real issue is… I’ve added
Copy code
tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
    println("DEBUG - Doesn't enter here")
    it.exclude("**/build/**")
}

tasks.configureEach {
    if (it.name.contains("detekt")) {
        println("DEBUG Hello, I am here and can exclude! $it ${it.class}")
        it.exclude("**/build/**")
    }
}
And
withType
doesn’t exclude build dir, but checking by name does! so looks like my issue is related to gradle itself. I have this configuration in separate file detekt.gradle - had no idea that it may have any meaning. https://github.com/gradle/gradle-native/issues/742 Well… Gradle/Groovy - surprises on each corner…. Thanks for your help though!
g

gammax

07/29/2021, 8:19 PM
External Gradle files (like a detekt.gradle) are a massive pain
26 Views