google / dagger

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

Unable to inject @JvmInline value classes #2930

Open drinkthestars opened 3 years ago

drinkthestars commented 3 years ago

Injecting a @JvmInline value class fails in the following manner:

Code:

@JvmInline value class Foo(val value: Int)

@Module
@InstallIn(SingletonComponent::class)
object FooModule {
    @Provides
    fun provideFoo() = Foo(value = 123)
}

class FooBar @Inject constructor(
    private val foo: Foo
) { /** ... **/ }

class UsesFooBar @Inject constructor(
    private val fooBar: FooBar
) { /** Uses fooBar **/ }

Fails with:

[Dagger/MissingBinding] FooBar cannot be provided without an @Inject constructor or an @Provides-annotated method.
  public abstract static class SingletonC implements FooApplication_GeneratedInjector

Not sure if this is a bug, or if it's just impossible to support with kapt (i.e. needs something like KSP) 🤔. Thanks!

drinkthestars commented 3 years ago

could be related to https://github.com/google/dagger/issues/2855

Chang-Eric commented 3 years ago

I think this will likely be something that needs KSP as we probably will just see Foo as an Integer after kapt. Added some labels but we can revisit in the future when KSP is done.

drinkthestars commented 3 years ago

Sounds good, thank you!

reddaly commented 1 year ago

I think this will likely be something that needs KSP as we probably will just see Foo as an Integer after kapt. Added some labels but we can revisit in the future when KSP is done.

Is there a bug I can track related to "when KSP is done?"

Chang-Eric commented 1 year ago

@reddaly https://github.com/google/dagger/issues/2349

bj0 commented 1 year ago

The alpha version of KSP support was released with dagger 1.48, so I tested this again:

@JvmInline
value class PluginContext(val context: Context)

@Module
abstract class UiModule {

...

    companion object {
        @Provides
        @PluginScope
        fun providePluginContext(ctx: Context): PluginContext {
            return PluginContext(ctx)
        }
   }
}  

Just adding this provider (no requesters yet) ksp throws an error:

e: [ksp] java.lang.IllegalArgumentException: not a valid name: UiModule_Companion_ProvidePluginContext-wwunPCQFactory
Chang-Eric commented 1 year ago

To clarify, KSP is a prerequisite to fixing this, but is not itself the solution.

dawidhyzy commented 1 year ago

@Chang-Eric is this fixed with the current KSP implementation?

Chang-Eric commented 1 year ago

It is not fixed, as mentioned above. To clarify in case it is causing confusion, the current behavior is mixed, some cases work, some don't. This is because this is the behavior that existed due to kapt.