Commit 95cbe83
authored
Provide Fx types before user types (#1191)
Consider the following example:
```go
func opts() fx.Option {
return fx.Options(
fx.WithLogger(func(fx.Lifecycle) fxevent.Logger {
return &fxevent.ConsoleLogger{ W: os.Stdout }
}),
fx.Provide(func() string { return "" }),
fx.Provide(func() string { return "" }),
)
}
func main() {
fx.New(opts()).Run()
}
```
The relevant issue to surface to the user is that they are double
providing the same type. However, the actual error message is:
```
[Fx] ERROR Failed to start: the following errors occurred:
- fx.Provide(main.opts.func3()) from:
main.opts
/home/user/go/src/scratch/fx_provide_order/main.go:17
main.main
/home/user/go/src/scratch/fx_provide_order/main.go:22
runtime.main
/opt/go/root/src/runtime/proc.go:271
Failed: cannot provide function "main".opts.func3
(/home/user/go/src/scratch/fx_provide_order/main.go:17): cannot provide
string from [0]: already provided by "main".opts.func2
(/home/user/go/src/scratch/fx_provide_order/main.go:16)
- could not build arguments for function
"go.uber.org/fx".(*module).constructCustomLogger.func2
/home/user/go-repos/pkg/mod/go.uber.org/[email protected]/module.go:292:
failed to build fxevent.Logger:
missing dependencies for function "main".opts.func1
/home/user/go/src/scratch/fx_provide_order/main.go:11:
missing type:
- fx.Lifecycle (did you mean to Provide it?)
```
Which contains an additional error related to how the custom logger
could not be built.
This is because Fx will try to continue to build the custom logger in
the face of DI failure, theoretically to report issues through the right
channels. But after an error occurs when providing anything, [Fx refuses
to provide any more
types](https://github.com/uber-go/fx/blob/master/module.go#L184) -
leading to a subsequent error when trying to build this custom logger
that depends on the `fx.Lifecycle` type.
This is a common issue that can be misleading for new engineers
debugging their fx apps.
I couldn't find any particular reason why user-provided provides are
registered before these Fx types, so this PR switches this ordering so
that custom loggers can still be built if they rely on the Fx types,
which cleans up the error message:
```
[Fx] ERROR Failed to start: fx.Provide(main.opts.func3())
from:
main.opts
/home/user/go/src/scratch/fx_provide_order/main.go:17
main.main
/home/user/go/src/scratch/fx_provide_order/main.go:22
runtime.main
/opt/go/root/src/runtime/proc.go:271
Failed: cannot provide function "main".opts.func3
(/home/user/go/src/scratch/fx_provide_order/main.go:17): cannot provide
string from [0]: already provided by "main".opts.func2
(/home/user/go/src/scratch/fx_provide_order/main.go:16)
```1 parent 9814dd3 commit 95cbe83
3 files changed
+24
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
480 | 480 | | |
481 | 481 | | |
482 | 482 | | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
487 | 486 | | |
488 | 487 | | |
489 | 488 | | |
| |||
492 | 491 | | |
493 | 492 | | |
494 | 493 | | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
495 | 498 | | |
496 | 499 | | |
497 | 500 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
117 | 123 | | |
118 | 124 | | |
119 | 125 | | |
| |||
575 | 581 | | |
576 | 582 | | |
577 | 583 | | |
578 | | - | |
| 584 | + | |
579 | 585 | | |
580 | 586 | | |
581 | 587 | | |
| |||
605 | 611 | | |
606 | 612 | | |
607 | 613 | | |
608 | | - | |
| 614 | + | |
609 | 615 | | |
610 | 616 | | |
611 | 617 | | |
| |||
1166 | 1172 | | |
1167 | 1173 | | |
1168 | 1174 | | |
1169 | | - | |
1170 | | - | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
1171 | 1178 | | |
1172 | 1179 | | |
1173 | 1180 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
264 | | - | |
| 264 | + | |
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
272 | | - | |
| 272 | + | |
273 | 273 | | |
274 | 274 | | |
275 | 275 | | |
| |||
660 | 660 | | |
661 | 661 | | |
662 | 662 | | |
663 | | - | |
| 663 | + | |
664 | 664 | | |
665 | 665 | | |
666 | 666 | | |
| |||
678 | 678 | | |
679 | 679 | | |
680 | 680 | | |
681 | | - | |
| 681 | + | |
682 | 682 | | |
683 | 683 | | |
684 | 684 | | |
| |||
690 | 690 | | |
691 | 691 | | |
692 | 692 | | |
693 | | - | |
| 693 | + | |
694 | 694 | | |
695 | 695 | | |
696 | 696 | | |
| |||
0 commit comments