codingwell / scala-guice

Scala extensions for Google Guice
Apache License 2.0
341 stars 44 forks source link

injectMembers silently fails to inject members on Scala 2.13.6 #99

Closed ahrussell closed 2 years ago

ahrussell commented 3 years ago

On versions 4.2.5, 4.2.9, and 5.0.1 (the ones I tested), Injector.injectMembers fails on Scala 2.13.6. Minimal repro:

import com.google.inject.{Guice, Inject, Injector}

class A {}

object O {
  @Inject var a: A = _
}

val injector: Injector =
  Guice.createInjector()

injector.injectMembers(O)
println(O.a) // prints null

You can play around with this example (changing the Scala version from 2.12.13 to 2.13.6) here.

joel-apollo commented 3 years ago

After debugging we found out it's related to this: https://users.scala-lang.org/t/fields-in-object-is-now-static-in-2-13/5873 and a change in Scala 2.13 that makes objects have static fields

MichealZub commented 2 years ago

I have found this would help: snippet

tsuckow commented 2 years ago

I'm glad you seem to have found workarounds. It seems it was amazing this worked at all since object implies a static object though for implementation reasons in scala wasn't apparently actually static. Google-Guice also seems to explicitly not endorse injecting into static fields.

With that in mind since this seems to be a Scala language vs Google-Guice issue and not directly related to Scala-Guice I'm going to close this ticket. If anyone has more tips or tricks they want to share feel free to continue the conversation.