How do I properly define a task in a gradle task to run in all modules?

Hi, I asked this on Stackoverflow recently but I thought it might be good to crosspost…

I have a gradle project with modules in Kotlin DSL. I want to configure a plugin task to be executed in all modules but define it only once in the root build.gradle.kts . In this case it is the detect plugin, used for linting. So in the root build.gradle.kts I have a block (simplified) like this:

allprojects {
    apply {
    tasks {
        val detect = register<io.gitlab.arturbosch.detekt.Detekt>("detect") {
            autoCorrect = true
            setSource( files(
                "*.kts", "*.gradle", "src/main/kotlin", "src/test/kotlin"

        withType<KotlinCompile> {
            kotlinOptions {
                jvmTarget = JavaVersion.VERSION_11.toString()

This works fine in the root project but the first submodule it addresses next fails with an error message:

Execution failed for task ':module1:detect'. > Provided path 'C:\[...]\project\module1\default-detekt-config.yml' does not exist!

This looks like the submodule misreads rootProject - or it gets its configuration from a default?

So what would be the correct way to achieve this goal in Gradle 7.2?