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

MissingBinding when @Provides in subcomponent to fulfill @Binds in Component #1781

Closed
davidliu opened this issue Mar 26, 2020 · 1 comment
Closed

Comments

@davidliu
Copy link

davidliu commented Mar 26, 2020

On Dagger 2.27.

If a @Binds is declared in Component, and the provision for the source of the bind is given in a subcomponent, Dagger is unable to fulfill the binding, even if it's only used from the subcomponent.

For example:

@dagger.Component(modules = BindingModule.class)
interface Component {
    OurSubcomponent subcomponent();
}

@Module
interface BindingModule {
    // note: Thing is an interface which ThingImpl extends.
    @Binds
    Thing thing(ThingImpl thingImpl);
}

@dagger.Subcomponent(modules = {ThingModule.class})
interface OurSubcomponent {
    Thing thing();
}

@Module
static class ThingModule {
    @Provides
    ThingImpl thingImpl() {
        return new ThingImpl();
    }
}

This results in the following error:

    interface Component {
    ^
      com.deviange.daggerrebind.ThingImpl is injected at
          com.deviange.daggerrebind.DaggerComponent.BindingModule.thing(thingImpl)
      com.deviange.daggerrebind.Thing is provided at
          com.deviange.daggerrebind.DaggerComponent.OurSubcomponent.thing() [com.deviange.daggerrebind.DaggerComponent.Component → com.deviange.daggerrebind.DaggerComponent.OurSubcomponent]

One way around this is to re-include the binding module in the subcomponent. However, I'm not sure if this is even intended, as the documentation seems to suggest that repeating modules will just reuse the parent Component module.

I also only chanced upon this solution through sheer luck. It'd be much more intuitive if a @Binds declared at the component level could be fulfilled at the subcomponent level without re-including the Binds.


For context: the reason why I came across this is because AssistedInject provides a convenience module that has all the bindings for the factories in a single module.

@Chang-Eric
Copy link
Member

I think this is working as intended, though I can understand why you'd want it to work this way. I think it would cause a lot of confusion since you could have an @Binds at a component level that couldn't actually be used from that component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants