Skip to content
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

Annotation targets for qualifier annotations break kotlin-inject #447

Open
vRallev opened this issue Oct 25, 2024 · 9 comments
Open

Annotation targets for qualifier annotations break kotlin-inject #447

vRallev opened this issue Oct 25, 2024 · 9 comments
Labels
blocked by 3rd-party bug Something isn't working

Comments

@vRallev
Copy link
Contributor

vRallev commented Oct 25, 2024

The following code compiles fine:

@Qualifier
annotation class QualifiedString

@Component
abstract class MainComponent {

    @Provides
    @QualifiedString
    fun provideString(): String = ""

    @QualifiedString
    abstract val otherString: String
}

But if I add a target to the qualifier annotation, even with an exhaustive list, the code fails to compile with a confusing error.

@Qualifier
@Target(
    CLASS,
    ANNOTATION_CLASS,
    TYPE_PARAMETER,
    PROPERTY,
    FIELD,
    LOCAL_VARIABLE,
    VALUE_PARAMETER,
    CONSTRUCTOR,
    FUNCTION,
    PROPERTY_GETTER,
    PROPERTY_SETTER,
    TYPE,
    FILE,
    TYPEALIAS,
)
annotation class QualifiedString

Error:

e: [ksp] Cannot find an @Inject constructor or provider for: String
/kotlin-inject-anvil/sample/lib/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidApplicationIdProvider.kt:84: otherString: String
@vRallev
Copy link
Contributor Author

vRallev commented Oct 25, 2024

Oh, screw this, it's related to KSP2. With KSP1 ksp.useKSP2=false it works fine.

vRallev added a commit to amzn/kotlin-inject-anvil that referenced this issue Oct 25, 2024
Disable KSP2 again as there are still subtle bugs such as: evant/kotlin-inject#447
@evant
Copy link
Owner

evant commented Oct 25, 2024

I wonder if that makes them no longer compare equal, may be related to google/ksp#2091

@vRallev
Copy link
Contributor Author

vRallev commented Oct 25, 2024

Happy to forward this as new issue to them, but could you please point me to where qualifier annotations are resolved and how you compare them?

@evant
Copy link
Owner

evant commented Oct 25, 2024

sure, comparisons are done for looking for an equal TypeKey which in turn uses the equals implementation for annotations here

@evant
Copy link
Owner

evant commented Oct 25, 2024

Also note I put 'blocked by 3rd-party' on this but if there's a reasonable adjustment to our implementation that works I'm happy to accept it

@vRallev
Copy link
Contributor Author

vRallev commented Nov 8, 2024

This almost slipped through the cracks: google/ksp#2196

@eygraber
Copy link
Contributor

I can get that test case to succeed with KSP2 by doing abstract val otherString: @QualifiedString String.

@Qualifier
@Target(
    CLASS,
    ANNOTATION_CLASS,
    TYPE_PARAMETER,
    PROPERTY,
    FIELD,
    LOCAL_VARIABLE,
    VALUE_PARAMETER,
    CONSTRUCTOR,
    FUNCTION,
    PROPERTY_GETTER,
    PROPERTY_SETTER,
    TYPE,
    FILE,
    TYPEALIAS,
)
annotation class QualifiedString

@Component
abstract class MainComponent {

    @Provides
    @QualifiedString
    fun provideString(): String = ""

    abstract val otherString: @QualifiedString String
}

@eygraber
Copy link
Contributor

This looks related to something I saw in one of my projects using KSP1:

@Qualifier
@Target(
  PROPERTY,
  PROPERTY_GETTER,
  FUNCTION,
  VALUE_PARAMETER,
  TYPE,
)
annotation class ActivityContext

class Context

@Component
abstract class MainComponent(
  @get:Provides val context: @ActivityContext Context
) {
    @ActivityContext
    abstract val c: Context
}

If I do @get:Provides @ActivityContext val context: Context it doesn't work.

@vRallev vRallev mentioned this issue Nov 21, 2024
@Ppeepost4489
Copy link

The following code compiles fine:

@Qualifier
annotation class QualifiedString

@Component
abstract class MainComponent {

    @Provides
    @QualifiedString
    fun provideString(): String = ""

    @QualifiedString
    abstract val otherString: String
}

But if I add a target to the qualifier annotation, even with an exhaustive list, the code fails to compile with a confusing error.

@Qualifier
@Target(
    CLASS,
    ANNOTATION_CLASS,
    TYPE_PARAMETER,
    PROPERTY,
    FIELD,
    LOCAL_VARIABLE,
    VALUE_PARAMETER,
    CONSTRUCTOR,
    FUNCTION,
    PROPERTY_GETTER,
    PROPERTY_SETTER,
    TYPE,
    FILE,
    TYPEALIAS,
)
annotation class QualifiedString

Error:

e: [ksp] Cannot find an @Inject constructor or provider for: String
/kotlin-inject-anvil/sample/lib/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidApplicationIdProvider.kt:84: otherString: String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked by 3rd-party bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants