autonomousapps / autonomousapps/dependency-analysis-gradle-plugin
Incorrect implementation vs api advice when interface implementor overrides return type
- Dominant language
- Kotlin
- Stars
- 2.2k
- Forks
- 158
- Avg merge
- 16h 25m
- Merged PRs (30d)
- 46
Description
**Build scan link**
Reproduced in an internal project (cash-android), can share a build scan via internal channels.
**Plugin version**
2.19.0
**Gradle version**
8.14.3
**JDK version**
17
**(Optional) Kotlin and Kotlin Gradle Plugin (KGP) version**
2.2.0
**(Optional) Android Gradle Plugin (AGP) version**
8.11.1
**(Optional) `reason` output for bugs relating to incorrect advice**
Toggle
```
------------------------------------------------------------
You asked about the dependency ':treehouse-android:viewmodels'.
You have been advised to change this dependency to 'implementation' from 'api'.
------------------------------------------------------------
Shortest path from :treehouse-android:presenters to :treehouse-android:viewmodels for debugCompileClasspath:
:treehouse-android:presenters
\--- :treehouse-android:viewmodels
Shortest path from :treehouse-android:presenters to :treehouse-android:viewmodels for debugRuntimeClasspath:
:treehouse-android:presenters
\--- :treehouse-android:viewmodels
Shortest path from :treehouse-android:presenters to :treehouse-android:viewmodels for debugUnitTestCompileClasspath:
:treehouse-android:presenters
\--- :treehouse-android:viewmodels
Shortest path from :treehouse-android:presenters to :treehouse-android:viewmodels for debugUnitTestRuntimeClasspath:
:treehouse-android:presenters
\--- :treehouse-android:viewmodels
Shortest path from :treehouse-android:presenters to :treehouse-android:viewmodels for debugAndroidTestCompileClasspath:
:treehouse-android:presenters
\--- :treehouse-android:viewmodels
Shortest path from :treehouse-android:presenters to :treehouse-android:viewmodels for debugAndroidTestRuntimeClasspath:
:treehouse-android:presenters
\--- :treehouse-android:viewmodels
Source: debug, android_test
---------------------------
(no usages)
Source: debug, main
-------------------
* Uses 1 class: com.squareup.cash.treehouse.android.viewmodels.TreehouseUiModel (implies implementation).
Source: debug, test
-------------------
(no usages)
```
**Describe the bug**
In the case when an interface implementor overrides the return type of an overridden method, DAGP suggests that the dependency that supplies that return type should be `implementation`, which seems incorrect.
**To Reproduce**
Steps to reproduce the behavior:
1. Interface is defined in a "common" module as follows.
```kotlin
fun interface PresenterFactory {
fun create(screen: Screen, navigator: Navigator): Presenter<*, *>?
}
```
2. The interface is implemented in the "presenters" module as follows. Note that `TreehouseUiModel` is declared in the "viewmodels" module.
```kotlin
class TreehousePresenterFactory @Inject internal constructor() : PresenterFactory {
override fun create(
screen: Screen,
navigator: Navigator,
): Presenter? = TODO()
}
```
3. When "presenters" depends on "viewmodels" as `api` (since `TreehouseUiModel` appears in the ABI of "presenters"), DAGP emits the following advice:
```
Advice for :treehouse-android:presenters
Existing dependencies which should be modified to be as indicated:
implementation projects.treehouseAndroid.viewmodels (was api)
```
**Expected behavior**
DAGP should not emit the advice, since `api` is correct in this case.
**Additional context**
N/A
Contributor guide
Research direction
Start with the Kotlin interface and implementation shown in the issue, using the listed Kotlin, Gradle, and plugin versions to reproduce the advice. Trace the dependency-advice analysis for the presenters-to-viewmodels path and verify that the result preserves api when the implementation narrows the generic return type; add or update a regression test wherever this analysis is covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100