google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

[KSP] Dagger-KSP does not see typealiased dagger annotations #4356

Open ZacSweers opened 4 months ago

ZacSweers commented 4 months ago

Edit When I originally wrote this, I thought it was related to the use of expect/actual in multiplatform. Upon further testing, it appears to be just related to the use of a typealiased annotation in any context

I have a multiplatform project where I use Dagger in jvm/android. To accomplish this in a way that still allows for use of standard injection annotations in common sources, I use expect/actual + typealiases to actualize them in jvm/android compilations to the real dagger annotations.

// commonMain
@Target(CONSTRUCTOR, FUNCTION, PROPERTY_SETTER) expect annotation class Inject()

@Target(CONSTRUCTOR) expect annotation class AssistedInject()

@Target(VALUE_PARAMETER) expect annotation class Assisted(val value: String = "")

@Target(CLASS) expect annotation class AssistedFactory()
// androidMain/jvmMain
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import javax.inject.Inject

actual typealias Inject = Inject

actual typealias AssistedInject = AssistedInject

actual typealias Assisted = Assisted

actual typealias AssistedFactory = AssistedFactory

However, it appears that dagger-ksp then misses these types because it doesn't resolve the aliased type of the annotations. I'm not sure if this is a dagger issue or a KSP issue (i.e. is KSP not correctly resolving that the annotated symbol is actually an alias to the intended type).

Two alternative solutions:

  1. Allow specifying custom annotations to replace dagger's expected ones. This solution is what the kotlin parcelize plugin used to support K2/KMP parceling. That is to say, expose compiler options to remap/add mappings of the annotations dagger looks for, such that I could say "hey my.custom.AssistedFactory should also be treated as @AssistedFactory".
  2. Repackage dagger's annotations into a multiplatform project such that they can be used natively in a KMP project.
ZacSweers commented 4 months ago

I've searched the KSP issue tracker and found a previous issue that, amusingly, I filed months ago and forgot about https://github.com/google/ksp/issues/1676. I'm going to consolidate this over to that.

My solution in that issue is similar to the first alternative I listed above

ZacSweers commented 4 months ago

Reopening as, according to the KSP folks, this is actually the responsibility of the symbol processor to resolve the underlying aliased type.

https://github.com/google/ksp/issues/1676#issuecomment-2231956440

danysantiago commented 4 months ago

I filled https://issuetracker.google.com/355006385 in XProcessing which is where most type aliases are resolved for Dagger. We already resolve type alias in various places but looks like we never needed to for annotations, so this seems something that just might be fixed with changes in XProcessing.