8000 Anvil does not generate dagger factory in some KSP multi-round cases · Issue #87 · ZacSweers/anvil · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Anvil does not generate dagger factory in some KSP multi-round cases #87

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

Open
IlyaGulya opened this issue Jan 14, 2025 · 3 comments
Open

Comments

@IlyaGulya
Copy link
IlyaGulya commented Jan 14, 2025

Here's reproducer project: https://github.com/IlyaGulya/anvil-ksp-reproducer
If you run the ./gradlew :app:run it will fail with following compilation error:

> Task :feature-list:compileKotlin FAILED
e: file:///Users/ilyagulya/Projects/Community/anvil-reproducer/feature-list/build/generated/ksp/main/kotlin/com/example/myapplication/list/DefaultListComponent_AssistedFactory_Impl.kt:21:32 Unresolved reference 'DefaultListComponent_Factory'.
e: file:///Users/ilyagulya/Projects/Community/anvil-reproducer/feature-list/build/generated/ksp/main/kotlin/com/example/myapplication/list/DefaultListComponent_AssistedFactory_Impl.kt:23:104 Not enough information to infer type argument for 'V'.
e: file:///Users/ilyagulya/Projects/Community/anvil-reproducer/feature-list/build/generated/ksp/main/kotlin/com/example/myapplication/list/DefaultListComponent_AssistedFactory_Impl.kt:27:40 Unresolved reference 'DefaultListComponent_Factory'.
e: file:///Users/ilyagulya/Projects/Community/anvil-reproducer/feature-list/build/generated/ksp/main/kotlin/com/example/myapplication/list/DefaultListComponent_AssistedFactory_Impl.kt:30:55 Unresolved reference 'DefaultListComponent_Factory'.

Looking into the generated sources, there's indeed no DefaultListComponent_Factory:
image

If I turn off the anvil.generateDaggerFactories and use ksp(libs.dagger.compiler) - everything starts working fine.

If I remove the generated class dep: ListComponentDependency from the DefaultListComponent constructor:

@ContributesAssistedFactory(AppScope::class, ListComponent.Factory::class)
class DefaultListComponent @AssistedInject constructor(
//    dep: ListComponentDependency,
    test: Test,
    @Assisted private val onItemSelected: (id: String) -> Unit,
) : ListComponent {

    override fun onItemClicked(id: String) {
        onItemSelected(id)
    }
}
class Test @Inject constructor()

Everything also works fine.

Seems that issue only present when we depend on the classes generated by another KSP processor.

@ZacSweers
Copy link
Owner

Can you create a minimally reproducible sample? Ideally a self-contained program. The one you linked is fairly nontrivial and I'm not really sure where to begin with it

@IlyaGulya
Copy link
Author

Sure, I will try to shrink it even further

@IlyaGulya
Copy link
Author
IlyaGulya commented Jan 14, 2025

@ZacSweers I've updated the reproducer.
What's happening:
We have interface ListComponent which is annotated with @MyAnnotation.
@MyAnnotation triggers generation of ListComponentDependency using processor defined in :my-processor
DefaultListComponent class depends on ListComponentDependency in its constructor.
We have DefaultListComponent.Factory annotated with @AssistedFactory.
What I expect:
Everything compiles fine and dagger assisted factory being generated by Anvil, as well as Dagger factory for DefaultListComponent.

What happens instead:
Anvil generates the assisted factory which depends on dagger factory:

public class DefaultListComponent_Factory_Impl(
  private val delegateFactory: DefaultListComponent_Factory,
) : DefaultListComponent.Factory {
  override fun invoke(onItemSelected: Function1<String, Unit>): DefaultListComponent =
      delegateFactory.get(onItemSelected)

  public companion object {
    @JvmStatic
    public fun create(delegateFactory: DefaultListComponent_Factory):
        InjectProvider<DefaultListComponent.Factory> =
        InstanceFactory.create(DefaultListComponent_Factory_Impl(delegateFactory))

    @JvmStatic
    public fun createFactoryProvider(delegateFactory: DefaultListComponent_Factory):
        InternalProvider<DefaultListComponent.Factory> =
        InstanceFactory.create(DefaultListComponent_Factory_Impl(delegateFactory))
  }
}

But it does not generate the DefaultListComponent_Factory itself, thus, failing to compile.

Removing the generated dep: ListComponentDependency from constructor and replacing it by some non-generated dependency fixes the issue.

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
0