dagger: Dagger generates code with wrong parameter names and fails to compile

We recently updated Dagger from 2.37 to 2.42. Dagger time to time generates not compiling code. We have a component that uses other component.

Our simplified component;

@Subcomponent
interface ParentComponent {

    fun inject(foo: Foo)

    fun plus(module: FooModule): FooComponent
}

Generated code;

    @Override
    public FooComponent plus(FooModule module) {
      Preconditions.checkNotNull(arg0);
      return new FooComponentImpl(applicationComponentImpl, parentComponentImp, arg0);
    }

Generated code has module parameter but code expects arg0 name. This fails to compile. If i change something in generated code and compile again, old generated code invalided and dagger generate correct code. But issue appears again, I couldnt find a common use case.

As workaround we changed module parameter name in ParentComponent to arg0 and it seems to prevents the issue up until now.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18

Commits related to this issue

Most upvoted comments

Sorry, I was out last week.

Just got some time to look into this and should have a fix submitted soon.

Seeing this as well, but interestingly, only in a test component that inherits from another component;

Our (simplified) usage in an Android app looks something like this:

Application component pretty standard android here; written in Kotlin

@Singleton
@Component(modules = [ApplicationModule::class])
interface AppComponent {
    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }

    fun plusActivityComponent(activityModule: ActivityModule): ActivityComponent
...
}

TestComponent extends AppComponent for use in some connected android tests and is written in Kotlin

@Singleton
@Component(modules = [ApplicationModule::class])
interface TestComponent : AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): TestComponent
    }
...
}

ActivityComponent a pretty standard subcomponent written in Kotlin

@Subcomponent(modules = arrayOf(ActivityModule::class))
@ActivityScoped
interface ActivityComponent {
...
}

Interestingly when I roll back to Dagger 2.41 the function is declared with a parameter name of arg0 in DaggerTestComponent:

  @Override
  public ActivityComponent plusActivityComponent(ActivityModule arg0) {
    Preconditions.checkNotNull(arg0);
    return new ActivityComponentImpl(testComponent, arg0);
  }

but in Dagger 2.42 the function parameter name is not replaced with arg0, but it’s uses remain arg0.

    @Override
    public ActivityComponent plusActivityComponent(ActivityModule activityModule) {
      Preconditions.checkNotNull(arg0);
      return new ActivityComponentImpl(testComponentImpl, arg0);
    }

Interestingly, this suggests a workaround: Anytime you’re doing something like this with a module, just name it arg0 🤷‍♀️

I’m having the same issue after updating from 2.40.1 to 2.42. I’ve noticed the problem occurs in multi-module project with components from non-main modules.

Снимок экрана 2022-06-16 в 13 01 00

Using HEAD-SNAPSHOT artifacts causes multiple other issues with dagger modules from non-main modules.

Снимок экрана 2022-06-16 в 12 55 26

Version 2.41 does not have this issue.

Kotlin version 1.6.21/1.6.10.

Exactly like in the comment above, changing argument name in the component’s method to arg0 has helped.