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

Dagger factory - Package name with a sentence case segment results in the wrong import in the generated factory #150

Closed
vlad-kasatkin opened this issue Oct 24, 2020 · 0 comments · Fixed by #151
Labels
bug Something isn't working dagger factory Happens when generateDaggerFactories is set to true

Comments

@vlad-kasatkin
Copy link
Contributor

A dagger module like this

        package com.squareup.test
        
        import dagger.Module
        import dagger.Provides
        import com.uppercase.Package.Thing
        
        @Module
        object DaggerModule1 {
          @Provides fun provideThing(): Thing = Thing()
        }

generates a factory with the import to the Package, assuming that Thing is a nested class:

package com.squareup.test

import com.uppercase.Package
import dagger.internal.Factory
import dagger.internal.Preconditions
import kotlin.jvm.JvmStatic

object DaggerModule1_ProvideThingFactory : Factory<Package.Thing> {
  override fun get(): Package.Thing = provideThing()

  @JvmStatic
  fun create(): DaggerModule1_ProvideThingFactory = this

  @JvmStatic
  fun provideThing(): Package.Thing = Preconditions.checkNotNull(DaggerModule1.provideThing(),
      "Cannot return null from a non-@Nullable @Provides method")
}

This factory does not compile:

v: Configuring the compilation environment
e: /var/folders/34/3hpdfjvj7453gz767yc7b6j40000gn/T/Kotlin-Compilation1676785146484626793/build/anvil/com/squareup/test/DaggerModule1_ProvideThingFactory.kt: (5, 22): Packages cannot be imported
e: /var/folders/34/3hpdfjvj7453gz767yc7b6j40000gn/T/Kotlin-Compilation1676785146484626793/build/anvil/com/squareup/test/DaggerModule1_ProvideThingFactory.kt: (10, 60): Unresolved reference: Thing
e: /var/folders/34/3hpdfjvj7453gz767yc7b6j40000gn/T/Kotlin-Compilation1676785146484626793/build/anvil/com/squareup/test/DaggerModule1_ProvideThingFactory.kt: (11, 31): Unresolved reference: Thing
e: /var/folders/34/3hpdfjvj7453gz767yc7b6j40000gn/T/Kotlin-Compilation1676785146484626793/build/anvil/com/squareup/test/DaggerModule1_ProvideThingFactory.kt: (17, 31): Unresolved reference: Thing

Unfortunately the unconventional package is a 3rd party library that in general does not follow java conventions that are not explicitly enforced.

From the exploration, the root cause is the same as in square/kotlinpoet#978 - ClassName.bestGuess does not have enough context to make the correct guess and can only rely on conventions .

@vRallev vRallev added bug Something isn't working dagger factory Happens when generateDaggerFactories is set to true labels Oct 25, 2020
vlad-kasatkin added a commit to vlad-kasatkin/anvil that referenced this issue Oct 27, 2020
  - fall back to manual resultion when class is potentially from the sentence case package
vlad-kasatkin added a commit to vlad-kasatkin/anvil that referenced this issue Oct 27, 2020
  - fall back to manual resultion when class is potentially from the sentence case package
vlad-kasatkin added a commit to vlad-kasatkin/anvil that referenced this issue Oct 27, 2020
  - fall back to manual resultion when class is potentially from the sentence case package
vlad-kasatkin added a commit to vlad-kasatkin/anvil that referenced this issue Oct 27, 2020
  - fall back to manual resultion when class is potentially from the sentence case package
vlad-kasatkin added a commit to vlad-kasatkin/anvil that referenced this issue Oct 27, 2020
  - fall back to manual resultion when class is potentially from the sentence case package
vRallev added a commit that referenced this issue Oct 28, 2020
…se-package

[#150] Unconventional package names generate correct factories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dagger factory Happens when generateDaggerFactories is set to true
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants