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

iamthevoid

05/27/2021, 10:02 AM
I use git submodule inside project, both submodule and module has configured detekt. Can i somehow pass baseline and config path depends on module, that detect check at the time?
g

gammax

05/27/2021, 11:07 AM
Yes, you can just reference the file with
..
or so.
i

iamthevoid

05/27/2021, 11:12 AM
Hm, it should work if detekt configured for each separate module, but what if it configured for
allProjects
?
g

gammax

05/27/2021, 11:13 AM
Sorry I’m not sure I follow you 🤔 Can you explain better what’s your setup?
i

iamthevoid

05/27/2021, 11:31 AM
I have next tree
Copy code
project
|-lib
  |-git-submodule-with-own-detect-configuration
  	|-detekt-config-files
  	|-sample-app
	|-modulesFolder
	  |-module1
	  |-module2
	  |-etc
  |-detekt-config-files
  |-modulesFolder1
    |-module1
    |-module2
    |-etc
  |-modulesFolder2
    |-module1
    |-module2
    |-etc
  |-etc
detect-config-files is
baseline.xml
and
config.xml
. They stored in root project ang git-submodule-project with same relative path. Detekt configured in each of these projects in
allProjects
section of root
build.gradle.kts
like a:
Copy code
fun Project.configureDetekt() {

    apply(plugin = Dependencies.Detekt.plugin)

    dependencies { detektPlugins(Dependencies.Detekt.detektFormatting) }

    configureEachTask<Detekt> {
        jvmTarget = Versions.jvmTarget
    }

    configure<DetektExtension> {
        toolVersion = Versions.detekt
        baseline = rootDirFile("config/detekt/baseline.xml")
        input.setFrom(
            "src/main/kotlin",
            "src/main/java",
            "src/commonMain/kotlin",
            "src/androidMain/kotlin",
            "src/iosMain/kotlin"
        )
        config = rootDirFiles("config/detekt/detekt.yml")
        parallel = true
        autoCorrect = true
        reports {
            xml {
                enabled = true
                destination = rootDirFile("build/report/detekt.xml")
            }
            txt {
                enabled = true
                destination = rootDirFile("build/report/detekt.txt")
            }
        }
    }
}
When i run detekt on root project with
Copy code
./gradlew detekt
it use root project
build.gradle.kts
. And as
configure<DetektExtension>
configures for plugin, but not for each task. I am getting the situation when submodule modules checking with root project rules. I want to separate configs of root project and submodule project and somehow force to Detekt use concrete rules. It can be possible if
config
and
baseline
path can be modified from
configureEachTask<Detekt>
block. But in task
Detekt
both
config
and
baseline
properties declared as
val
Let me format it quicly
done
Thing is the modules lay on different depth relative to root, so i cant just use something like
../..
in root
build.gradle.kts
but must specify it for each separate module
But it can be better if path to config can be configured for each detekt task. Then next logick will be possible
Copy code
configureEachTask<Detekt> {
   val isSubmoduleTask = {somehow by task name or path}
   val configRoot = if (isSubmoduleTask) "submodule/path/" else "."
   config = "$configRoot/config/detekt.yml"
   baseline = "$configRoot/config/baseline.xml"
}
g

gammax

05/27/2021, 11:44 AM
so i cant just use something like 
../..
 in root 
build.gradle.kts
 but must specify it for each separate module
With Gradle you can always use
rootProject.files
and that will give you a path starting from the root project of your Gradle multi-project setup.
i

iamthevoid

05/27/2021, 11:59 AM
So. Now i don’t understand how it will help me to pass relative path to two different configs for different modules.
Even if i separate paths for modules and pass it in
configure<DetektExtensions>
this
configure
will be called for plugin, but not for each task
So only last call of
configure<DetektExtension>
will be applied
If place this
configure
in
allProjects
block. Right?
So if i want to separate checks for different modules i must apply plugin and configure it for each module, not for
allProjects
Right?
g

gammax

05/27/2021, 12:16 PM
Sorry I don’t really understand what’s your question 🤔 I thought you wanted to share baseline and config between all of your modules, right? Seems like that’s not the case?
i

iamthevoid

05/27/2021, 12:19 PM
At this point i should sorry for my bad english )
I want use two baselines and two configs, one for base project modules, second for submodule modules. I want they to select dynamically when
./gradlew detekt
called for base project
And want to place all configuration in one place
g

gammax

05/27/2021, 12:23 PM
Ok I understand. So that’s IMHO far from optimal 😕 Potentially you can use
allProjects{}
and do an
if-then-else
check inside for the project name. Not elegant, but is probably the best solution for you right now.
i

iamthevoid

05/27/2021, 1:09 PM
I was at this point at start of this conversation 🙂 Somehow it is not works. My configuration looks like
Copy code
allprojects {

    .................

    configureDetekt()
}
And fun
Copy code
fun Project.configureDetekt() {

    apply(plugin = Dependencies.Detekt.plugin)

    dependencies { detektPlugins(Dependencies.Detekt.detektFormatting) }

    configureEachTask<Detekt> {
        jvmTarget = Versions.jvmTarget
    }
    println("$name: ${rootProject.name}")
    configure<DetektExtension> {
        val isFromSubmodule = with(Dependencies.Modules.DesignSystem) {
            listOf(shared, switches, typography, buttons).contains(path)
        }
        val configRoot = if (isFromSubmodule) "lib/design-system" else "."
        toolVersion = Versions.detekt
        baseline = rootDirFile("$configRoot/config/detekt/baseline.xml")
        input.setFrom(
            "src/main/kotlin",
            "src/main/java",
            "src/commonMain/kotlin",
            "src/androidMain/kotlin",
            "src/iosMain/kotlin"
        )
        config = rootDirFiles("$configRoot/config/detekt/detekt.yml")
        parallel = true
        autoCorrect = true
        reports {
            xml {
                enabled = true
                destination = rootDirFile("build/report/detekt.xml")
            }
            txt {
                enabled = true
                destination = rootDirFile("build/report/detekt.txt")
            }
        }
    }
}
Here i assume that
configure
applied for each project and it is (i checked with
println
). But when i run
./gradlew detekt
i see detekt fails on submodule check, so i assume that detekt use base project config for submodule check
g

gammax

05/27/2021, 2:20 PM
I’m unsure what’s going on but your problem is not related to Detekt but to Gradle. I actually suggest you don’t use
allProjects{}
and refactor your build logic a bit. Perhaps using a precompiled script plugin or just copy-n-pasting your build logic around.
i

iamthevoid

05/27/2021, 2:22 PM
Ok, anyway thanks a lot for the given time!
g

gammax

05/27/2021, 2:22 PM
You’re welcome 🙏
24 Views