uber-go / dig

A reflection based dependency injection toolkit for Go.
https://go.uber.org/dig
MIT License
3.88k stars 206 forks source link

Invoke order affects private provided types #343

Closed sywhang closed 2 years ago

sywhang commented 2 years ago

@hbdf reported this issue to me.

The ordering of Invoke seems to affect the value for private-provided types.

For example:

parent := dig.New()
child := parent.Scope("child")

child.Provide(func() string {
  return "child"
}, dig.Export(false))

parent.Provide(func() string {
  return "parent"
})

parent.Invoke(func(s string) {
  fmt.Println(s)
})
child.Invoke(func(s string) {
  fmt.Println(s)
})

prints:

parent
parent

But the following code:

parent := dig.New()
child := parent.Scope("child")

child.Provide(func() string {
  return "child"
}, dig.Export(false))

parent.Provide(func() string {
  return "parent"
})

child.Invoke(func(s string) {
  fmt.Println(s)
})
parent.Invoke(func(s string) {
  fmt.Println(s)
})

prints:

child
parent

The first code snippet should've really printed:

parent
child
sywhang commented 2 years ago

Fixed by #344.