autonomousapps / autonomousapps/dependency-analysis-gradle-plugin

fixDependencies IllegalArgumentException on unexpected format

Open
#1,386 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Kotlin
Stars
2.2k
Forks
158
Avg merge
16h 25m
Merged PRs (30d)
46

Description

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](https://github.com/user-attachments/files/18973136/Dependency.Analysis.fixDependencies.parse.crash.zip)

Contributor guide

Open the contributing guide

Research direction

Start by running ./gradlew fixDependencies --stacktrace against the attached minimum reproducer. Read DependencyExtractor.kt around findIdentifier and parseDependencyDeclaration, then follow KotlinBuildScriptDependenciesRewriter.kt where dependency declarations are handled. Done means the custom extension form no longer crashes, or the failure reports the unsupported format clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.