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

Build fails when declaring two Lazy dependencies. #425

Open
AdriaBosch opened this issue Aug 17, 2024 · 1 comment
Open

Build fails when declaring two Lazy dependencies. #425

AdriaBosch opened this issue Aug 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@AdriaBosch
Copy link

I acknowledge that the following graph is not ideal, but I believe it should compile without the need for any workarounds.

@Scope
annotation class Singleton

@Singleton
@Inject
class Interceptor(val client1: Lazy<Client1>, val client2: Lazy<Client2>)

@Inject
class Client1(val interceptor: Interceptor)

@Inject
class Client2(val interceptor: Interceptor)

@Inject
class Repository(val client1: Client1, val client2: Client2)

@Singleton
@Component
interface MyComponent {

    val repository: Repository
}

I was able to compile by declaring the Interceptor dependency within the component.

@Singleton
@Component
interface MyComponent {

    val interceptor: Interceptor
    val dep: Dependency
}

or by removing the @Singleton annotation from Interceptor.

@AdriaBosch AdriaBosch changed the title The presence of two Lazy dependencies prevents it from compiling. Build fails when declaring two Lazy dependencies. Aug 17, 2024
@evant evant added bug Something isn't working labels Sep 11, 2024
@evant
Copy link
Owner

evant commented Sep 12, 2024

Ok I think I tracked down the cause of the issue. The expression for a given type is cached so it doesn't have to be recalculated when used in different places. However, when there is a a cycle that caching includes the reference to the lateinit variable, but not the lateinit variable itself. So when it's reused that variable doesn't exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants