Implicit dependency among tasks.. but the tasks do not exist

When I run my build I get this message:

* What went wrong:
A problem was found with the configuration of task ':kspExtensiveTestKotlin' (type 'KspTaskJvm').
  - Gradle detected a problem with the following location: '/Users/ftomassetti/repos/kolasu-java-langmodule/build/generated-src/antlr/extensiveTest'.
    
    Reason: Task ':kspExtensiveTestKotlin' uses this output of task ':generateExtensiveTestGrammarSource' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
    
    Possible solutions:
      1. Declare task ':generateExtensiveTestGrammarSource' as an input of ':kspExtensiveTestKotlin'.
      2. Declare an explicit dependency on ':generateExtensiveTestGrammarSource' from ':kspExtensiveTestKotlin' using Task#dependsOn.
      3. Declare an explicit dependency on ':generateExtensiveTestGrammarSource' from ':kspExtensiveTestKotlin' using Task#mustRunAfter.
    
    For more information, please refer to https://docs.gradle.org/8.2.1/userguide/validation_problems.html#implicit_dependency in the Gradle documentation.

So I add the dependency:

tasks.named("kspExtensiveTestKotlin") {
    mustRunAfter("generateExtensiveTestGrammarSource")
}

and I get:

Task with name 'kspExtensiveTestKotlin' not found in root project 'kolasu-java-langmodule'.

So I imagine somehow the task is created dynamically while running the build and cause this issue. I have not idea how to get rid of this. Any suggestion?

It seems KSP is not compatible with the antlr plugin, but you use both.

And the “task not found” seems to be because the KSP tasks are somehow lazily created and not yet available.

You can work-around the first problem, avoiding the second problem with

tasks.configureEach {
    if (name == "kspKotlin") {
        mustRunAfter(tasks.generateGrammarSource)
    }
}

But ultimately this is a bug in the Kotlin Gradle plugin.

https://youtrack.jetbrains.com/issue/KT-60815/

Thank you, this is super useful!

1 Like

Argh, stupid me.
It is not a problem of the Kotlin Plugin.
It is a bug in the ANTLR plugin: ANTLR plugin's generated source dir creates implicit task dependencies · Issue #19555 · gradle/gradle · GitHub
And my work-around posted there resolves the problem properly for all source-needing tasks:

sourceSets.configureEach {
    val generateGrammarSource = tasks.named(getTaskName("generate", "GrammarSource"))
    java.srcDir(generateGrammarSource.map { files() })
}