autonomousapps / dependency-analysis-gradle-plugin

Gradle plugin for JVM projects written in Java, Kotlin, Groovy, or Scala; and Android projects written in Java or Kotlin. Provides advice for managing dependencies and other applied plugins
Apache License 2.0
1.82k stars 120 forks source link

fixDependencies task added dependency incorrectly #1313

Closed zepurplez closed 12 hours ago

zepurplez commented 4 days ago

Plugin version 2.4.2

Gradle version 8.11

JDK version 17

(Optional) Kotlin and Kotlin Gradle Plugin (KGP) version 1.9.24

Describe the bug There is a kotlin project with java-test-fixture plugin. src/main has only one class with arguments from library(jodatime for example) and stub implementation in src/testFixtures.

build.gradle.kts

plugins {
    kotlin("jvm") version "1.9.24"
    `java-test-fixtures`
    id("com.autonomousapps.dependency-analysis") version "2.4.2"
}

dependencies {
    api("joda-time:joda-time:2.12.5") {
        artifact {
            classifier = "no-tzdb"
        }
    }
}

src/main

interface SomeHandler {
    fun handle(date: DateTime)
}

src/testFixtures

class StubSomeHandler : SomeHandler {
    override fun handle(date: DateTime) = TODO()
}

So when you try to run a command ./gradlew :lib:fixDependencies it will add new dependency to testFixturesApi configuration and it will be looks like this:

dependencies {
    api("joda-time:joda-time:2.12.5") {
        artifact {
            classifier = "no-tzdb"
        }
      testFixturesApi("joda-time:joda-time:2.12.5")
}
}

Expected behavior

dependencies {
    api("joda-time:joda-time:2.12.5") {
        artifact {
            classifier = "no-tzdb"
        }
    }
    testFixturesApi("joda-time:joda-time:2.12.5")
}
zepurplez commented 3 days ago

There is another way to specify a dependency with classifier or artifactType

dependencies {
    api(variantOf(libs.jodaTime) { classifier("no-tzdb") })
}

In this case, running ./gradlew :lib:fixDependencies won't give any advice to adding the dependency to the testFixtures configuration and print the next message:

> Task :lib:fixDependencies
Fixing dependencies for /Users/r.zaremba/Developer/sample-proj/lib/build.gradle.kts.
line 8:18 extraneous input 'libs.jodaTime' expecting ')'
line 9:1 extraneous input '<EOF>' expecting {'}', ID}
Can't fix dependencies for ':lib': 1: extraneous input 'libs.jodaTime' expecting ')'; 8:18, 2: extraneous input '<EOF>' expecting {'}', ID}; 9:1
autonomousapps commented 14 hours ago

Thanks for the report! Can you please try again with v2.5.0? it was just released yesterday, and it has improved support for parsing and rewriting .gradle.kts files.

zepurplez commented 12 hours ago

In version 2.5.0 it works as expected in both cases. Thank you!

autonomousapps commented 12 hours ago

Great, thanks for confirming! I'll close this issue then.