dependency-analysis-gradle-plugin icon indicating copy to clipboard operation
dependency-analysis-gradle-plugin copied to clipboard

fixDependencies IllegalArgumentException on unexpected format

Open adecker89 opened this issue 8 months ago • 1 comments
trafficstars

Hello,

When attempting to upgrade from version 1.32.0 to 2.10.1 I started getting an IllegalArgumentException when calling fixDependencies.

Caused by: java.lang.IllegalArgumentException: List has more than one element.
        at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:618)
        at com.autonomousapps.internal.cash.grammar.kotlindsl.utils.DependencyExtractor.findIdentifier(DependencyExtractor.kt:377)
        at com.autonomousapps.internal.cash.grammar.kotlindsl.utils.DependencyExtractor.parseDependencyDeclaration(DependencyExtractor.kt:167)
        at com.autonomousapps.internal.cash.grammar.kotlindsl.utils.DependencyExtractor.collectDependencies(DependencyExtractor.kt:65)
        at com.autonomousapps.internal.parse.KotlinBuildScriptDependenciesRewriter.handleDependencies(KotlinBuildScriptDependenciesRewriter.kt:118)
        at com.autonomousapps.internal.parse.KotlinBuildScriptDependenciesRewriter.enterNamedBlock(KotlinBuildScriptDependenciesRewriter.kt:76)
        at com.autonomousapps.internal.squareup.cash.grammar.KotlinParser$NamedBlockContext.enterRule(KotlinParser.java:1491)
        at com.autonomousapps.internal.antlr.v4.runtime.tree.ParseTreeWalker.enterRule(ParseTreeWalker.java:50)
        at com.autonomousapps.internal.antlr.v4.runtime.tree.ParseTreeWalker.walk(ParseTreeWalker.java:33)
        at com.autonomousapps.internal.antlr.v4.runtime.tree.ParseTreeWalker.walk(ParseTreeWalker.java:36)
        at com.autonomousapps.internal.antlr.v4.runtime.tree.ParseTreeWalker.walk(ParseTreeWalker.java:36)
        at com.autonomousapps.internal.cash.grammar.kotlindsl.parse.Parser.listener(Parser.kt:96)
        at com.autonomousapps.internal.parse.KotlinBuildScriptDependenciesRewriter$Companion.of(KotlinBuildScriptDependenciesRewriter.kt:168)
        at com.autonomousapps.internal.parse.BuildScriptDependenciesRewriter$Companion.of(BuildScriptDependenciesRewriter.kt:22)
        at com.autonomousapps.tasks.RewriteTask.action(RewriteTask.kt:64)

I figured out that this is related to some custom extension functions that I was adding to the dependency block.

dependencies {
    implementation(project(":foo").withFeature("bar"))
}

fun ProjectDependency.withFeature(feature: String): ProjectDependency = apply {
    capabilities { requireFeature(feature) }
}

So it looks like the dependency parser might be more strict now in the newer version.

I managed to find a fairly easy workaround for my specific use case as I was typing this up

    implementation(project(":foo")) { withFeature("bar") }

but it could still be good to have better error messages or improved compatibility of DependencyExtractor.

I've attached a minimum reproducer. Running ./gradlew fixDependencies --stacktrace should reproduce

Thanks!

Dependency Analysis fixDependencies parse crash.zip

adecker89 avatar Feb 25 '25 22:02 adecker89

Thanks for the report! fixDependencies is a best-effort to provide a way to auto-remediate issues discovered by the plugin. Unfortunately, since build scripts have access to a Full Programming Language, it is extremely difficult to account for all possible ways users can declare dependencies. I agree that a better error message would, at the very least, be a worthy improvement, but I can't make any promises about improving the parsing to work with custom functions like this.

autonomousapps avatar Mar 06 '25 18:03 autonomousapps