amzn / kotlin-inject-anvil

Extensions for the kotlin-inject dependency injection framework
Apache License 2.0
274 stars 8 forks source link

Build fails with unhelpful error message when there are component methods with the same name #57

Closed matejdro closed 1 month ago

matejdro commented 1 month ago

Attempting to compile following code:

class MyScope

@Component
@MergeComponent(MyScope::class)
abstract class DemoComponent: DemoComponentMerged {
   abstract fun provideAClassThatUsesBoth(): AClassThatUsesBoth
}

@ContributesTo(MyScope::class)
interface FirstComponent {
   @Provides
   fun provideSomething(long: Long): String = ""

   @Provides
   fun provideLong(): Long = 3L
}

@ContributesTo(MyScope::class)
interface SecondComponent {
   @Provides
   fun provideSomething(long: Long): Int = 0
}

@Inject
class AClassThatUsesBoth(text: String, number: Int)

Will just fail with the unhelpful InjectDemoComponent.kt:9:12 Type mismatch: inferred type is Int but String was expected error.

Issue stems from the fact that final merged component contains two methods that are both named provideSomething but they return different types. With Anvil allowing decentralized components across the projects, this is a case that could easily happen, where two developers unwittingly create a component with the same method name at two different places in the codebase.

vRallev commented 1 month ago

This error is either coming from kotlin-inject (I couldn't find the message though) or the Kotlin compiler itself. There isn't anything this project could do about it. We could check for contributed components and their methods, but then we'd need to rebuild everything kotlin-inject itself does internally. And even then it wouldn't be reliable, because the interfaces could be added manually and not through kotlin-inject-anvil to the final component.