cortinico / ktfmt-gradle

A Gradle plugin to apply ktfmt to your builds, and reformat you Kotlin source code like a glimpse ๐Ÿงน๐Ÿ˜
MIT License
154 stars 19 forks source link

Custom pre-commit task fails after update (0.18.0->0.19.0) #310

Closed pavikaa closed 3 months ago

pavikaa commented 3 months ago

๐Ÿ› Describe the bug

After updating to version 0.19.0 my custom pre-commit task fails. Plugin generated tasks ktftmFormat etc. work normally.

โš ๏ธ Current behavior

This is my custom task:

tasks.register<KtfmtFormatTask>("ktfmtPrecommitFormat") {
                group = "formatting"
                description = "Runs ktfmt on kotlin files in the project"
                source = fileTree(rootDir).apply { include("**/*.kt", "**/*.kts") }
            }

After updating to version 0.19.0 I get the following error:

Execution failed for task ':ktfmtPrecommitFormat'.
> Could not resolve all files for configuration ':ktfmt'.
   > Could not resolve com.google.guava:guava:32.0.0-jre.
     Required by:
         project : > com.facebook:ktfmt:0.51
      > The consumer was configured to find attribute 'org.gradle.usage' with value 'java-runtime'. However we cannot choose between the following variants of com.google.guava:guava:32.1.3-jre:
          - androidRuntimeElements
          - jreRuntimeElements
        All of them match the consumer attributes:
          - Variant 'androidRuntimeElements' capabilities com.google.collections:google-collections:32.1.3-jre and com.google.guava:guava:32.1.3-jre declares attribute 'org.gradle.usage' with value 'java-runtime':
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.dependency.bundling 'external' but the consumer didn't ask for it
                  - Provides org.gradle.jvm.environment 'android' but the consumer didn't ask for it
                  - Provides org.gradle.jvm.version '8' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
          - Variant 'jreRuntimeElements' capabilities com.google.collections:google-collections:32.1.3-jre and com.google.guava:guava:32.1.3-jre declares attribute 'org.gradle.usage' with value 'java-runtime':
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.dependency.bundling 'external' but the consumer didn't ask for it
                  - Provides org.gradle.jvm.environment 'standard-jvm' but the consumer didn't ask for it
                  - Provides org.gradle.jvm.version '8' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it

On the previous version (0.18.0) everything works as expected.

cortinico commented 3 months ago

Are you using Java 8? Can you try different versions of Java (like 11 and 17)?

pavikaa commented 3 months ago

Hey, thanks for your fast response. I'm using Java 17, I tried with 11 and 20 but that didn't help. The project builds fine and other tasks execute with no problem, only the one I created manually fails. Switching back to 0.18.0 works normally with Java 17

simonhauck commented 3 months ago

Does it make sense to change the structure of the plugin to be more independent of Ktfmt itself?

I know ktfmt publishes a jar with all dependencies bundled. Could it be possible to start the formatter via the command line?

Edit: I am not sure if that is the root cause of this problem and of course this has some impact on the plugin api.

pavikaa commented 3 months ago

I will close this issue because I replaced the use of a custom task with the ktfmtFormat task. I already run ktfmt on all project files, so the custom task is unnecessary for me.