Skip to content

Unclear error message for scoped provider #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vRallev opened this issue Nov 16, 2023 · 2 comments · Fixed by #327
Closed

Unclear error message for scoped provider #320

vRallev opened this issue Nov 16, 2023 · 2 comments · Fixed by #327
Labels
bug Something isn't working
Milestone

Comments

@vRallev
Copy link

vRallev commented Nov 16, 2023

I have a component similar to this:

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class AppScope

@Component
@AppScope
abstract class AndroidAppComponent

I meant to add a new provider from a feature module like this:

typealias AppScopeCoroutineScope = CoroutineScope

interface AppScopeCoroutineScopeComponent {
    @AppScope
    @Provides
    fun provideCoroutineScope(dispatcher: IoCoroutineDispatcher): AppScopeCoroutineScope {
        return CoroutineScopeScoped(...)
    }
}

@Component
@AppScope
abstract class AndroidAppComponent : AppScopeCoroutineScopeComponent

This fails with following error:

e: [ksp] @Provides with scope: com.my.package.di.AppScope cannot be provided in an unscoped component

This message is confusing, because it's not clear which the unscoped component is. I only found out by trial and error that this provider method is triggering the error. Removing @AppScope from the provider method resolves the error, but it would result in bugs and is required to be a singleton. Changing it to the following will work:

@AppScope
interface AppScopeCoroutineScopeComponent {
    @AppScope
    @Provides
    fun provideCoroutineScope(dispatcher: IoCoroutineDispatcher): AppScopeCoroutineScope {
        return CoroutineScopeScoped(...)
    }
}

However, this creates a new error:

e: [ksp] Cannot apply scope: com.my.package.di.AppScope
e: [ksp] /path/AndroidAppComponent.kt:11: as scope: com.my.package.di.AppScope is already applied

This error is surprising, because the scope is not applied twice. I needed to change my final component to this to resolve the problem:

@Component
abstract class AndroidAppComponent : AppScopeCoroutineScopeComponent

This code finally runs, but I'm afraid that it will start failing again with another interface being added.

Is this an error on my end? How do I avoid this?

@evant
Copy link
Owner

evant commented Nov 16, 2023

I think there's a few things going on here:

  1. For the first error message, I suspect this is related to ksp error messages do not include the source information on where it applies  #302 where it's not printing out the location as it should
  2. The second error message is being triggered by a check in place making sure you don't declare conflicting scopes on the class and the class it's extending. Yeah I think it shouldn't fire if they are actually the same scope.

@evant evant added the bug Something isn't working label Nov 16, 2023
evant added a commit that referenced this issue Nov 27, 2023

Verified

This commit was signed with the committer’s verified signature.
evant Eva
Fixes #320
@evant
Copy link
Owner

evant commented Nov 27, 2023

Fixed (2), closing since we already have an issue opened for (1)

@evant evant added this to the v0.7.0 milestone Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants