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

A generic type is not recognized as `api` when exposed as return type from a suspend function

Open timoloewe opened this issue 6 months ago • 3 comments

Plugin version

2.17.0

Gradle version

8.12

JDK version

21

Kotlin and Kotlin Gradle Plugin (KGP) version

2.1.20

Describe the bug

If a generic type is only exposed as a return type from a suspend function, the plugin does not recognize the type as being part of the public API and thus advises to change the dependency from api to implementation.

We have a network module that defines a generic ApiResult<T> type:

sealed class ApiResult<T> { ... }

In other modules, we depend on it and expose it as a return type on a public interface like this:

interface FooService {
    suspend fun getFoo(): ApiResult<Foo>
} 

The plugin then generates an incorrect advice to change the dependency from api to implementation:

Existing dependencies which should be modified to be as indicated:
  implementation(project(":network:api")) (was api)

If the type in question is not a generic type or if the function is not a suspend function, the advice is not given.

interface FooService {
    // Wrong advice is given:
    suspend fun getFoo(): ApiResult<Foo>

    // Not suspending, no advice given:
    fun getFoo(): ApiResult<Foo> 

    // Return type not generic, no advice given:
    suspend fun getFoo(): ApiResult.Error 
}

To Reproduce Steps to reproduce the behavior:

Here is a reproducer project: dagp-suspend-repro.zip

Running buildHealth on this project generates the incorrect advice.

Expected behavior

The plugin should not advise changing the dependency to implementation since the type is exposed in the public API no matter if the function is suspending or not.

timoloewe avatar May 12 '25 16:05 timoloewe

Thanks for the report and reproducer.

autonomousapps avatar May 15 '25 23:05 autonomousapps

Dupe of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/1324?

eygraber avatar May 20 '25 18:05 eygraber