cortinico / ktfmt-gradle

A Gradle plugin to apply ktfmt to your builds, and reformat you Kotlin source code like a glimpse 🧹🐘
MIT License
156 stars 19 forks source link

`./gradlew build` fails with implicit dependency errors #157

Open pramod-knidal opened 1 year ago

pramod-knidal commented 1 year ago

πŸ› Describe the bug

./gradlew build command fails with the following error

* What went wrong:
Some problems were found with the configuration of task 'app:ktfmtFormatMain' (type 'KtfmtFormatTask').
  - Gradle detected a problem with the following location: '<project-dir>/src/main/java'.

    Reason: Task 'app:ktfmtCheckMain' uses this output of task 'app:ktfmtFormatMain' 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 'app:ktfmtFormatMain' as an input of 'app:ktfmtCheckMain'.
      2. Declare an explicit dependency on 'app:ktfmtFormatMain' from 'app:ktfmtCheckMain' using Task#dependsOn.
      3. Declare an explicit dependency on 'app:ktfmtFormatMain' from 'app:ktfmtCheckMain' using Task#mustRunAfter.

    Please refer to https://docs.gradle.org/8.1.1/userguide/validation_problems.html#implicit_dependency for more details about this problem.
  - Gradle detected a problem with the following location: '<project-dir>/src/main/kotlin'.

    Reason: Task 'app:ktfmtCheckMain' uses this output of task 'app:ktfmtFormatMain' 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 'app:ktfmtFormatMain' as an input of 'app:ktfmtCheckMain'.
      2. Declare an explicit dependency on 'app:ktfmtFormatMain' from 'app:ktfmtCheckMain' using Task#dependsOn.
      3. Declare an explicit dependency on 'app:ktfmtFormatMain' from 'app:ktfmtCheckMain' using Task#mustRunAfter.

    Please refer to https://docs.gradle.org/8.1.1/userguide/validation_problems.html#implicit_dependency for more details about this problem.

⚠️ Current behavior

./gradlew build fails with error

βœ… Expected behavior

./gradlew build should not throw any warnings/errors related to ktfmt-gradle plugin.

πŸ’£ Steps to reproduce

  1. Include ktfmt plugin 0.12.0 in an android library project id 'com.ncorti.ktfmt.gradle' version '0.12.0'
  2. run ./gradlew build from terminal

πŸ“± Tech info

Notes

I found this issue when I was trying to generate a release build using 'maven-publish' which threw errors similar to the one above but it was asking me to include the implicit dependency between 'sourceReleaseJartask andktfmtFormatRelease`.

A workaround for now would be appreciated.

WhosNickDoglio commented 1 year ago

I think the error message is giving you a workaround? You need to make the relationship between tasks more explicit and could use dependsOn or mustRunAfter to do that.

Adding the following should work.

tasks.named("ktfmtCheckMain") {
    dependsOn("ktfmtFormatMain")
}
pramod-knidal commented 1 year ago

I think the error message is giving you a workaround? You need to make the relationship between tasks more explicit and could use dependsOn or mustRunAfter to do that.

Adding the following should work.

tasks.named("ktfmtCheckMain") {
    dependsOn("ktfmtFormatMain")
}

Yeah. Tried several things similar to the one you noted. It was throwing an error saying ktfmtCheckMain task not found. In gradle, the ordering matters. So, tried putting that logic in several places until it worked. After it worked, there was another similar error saying sourcesReleaseJar should depend on ktfmtFormatMain. After I fixed that, there was another similar one.

I thought having these dependencies explicitly defined in the plugin code would be a good solution. So, posted an issue here.

cortinico commented 1 year ago

@pramodshri-tgsys Thanks for the report

I thought having these dependencies explicitly defined in the plugin code would be a good solution. So, posted an issue here.

The two tasks should not depend on each other. Could you provide a reproducer where you're having this behavior as I can't reproduce it in my testing?

pramod-knidal commented 1 year ago

@cortinico I don't have a reproducer. It was happening in a proprietary project when I tried to publish an aar for an android library project using gradle plugin for maven publish. It happens only when you try to publish a release aar.

You note that the two tasks should not depend on each other. However, adding the code that @WhosNickDoglio has put in his comment will get you past the error... to a new error...