Wrong type in group is ignored without warning
Describe the bug
When you use fx.Annotate together with fx.ResultTags(`group:"name"`), but return different types in different places, it is very easy to ignore some of them. Because if you have slice of some type, it will be filled with entries of same type, but entries with different type are ignored.
We have many modules in our monorepo and some fx.Module returned different type than others. By accident, but that caused our health checks weren't run for ages. Since everything else worked, we didn't notice missing checks for very long time.
To Reproduce Here is simple example, where we create 2 clients + DB connection. All 3 produce record inside health check group but, DB one is ignored, because it returns wrong type. However no warning, is actually produced.
package main
import (
"fmt"
"go.uber.org/fx"
)
type HealthCheck interface {
Serving() string
}
type Client struct {
Addr string
}
type DB struct {
Addr string
}
func (c *Client) Serving() string {
return "Serving successfully on " + c.Addr + " address"
}
func clientFxModuleBuilder(moduleName, tagName, addr string) fx.Option {
return fx.Module(moduleName,
fx.Provide(
fx.Annotate(
func() *Client {
return &Client{Addr: addr}
},
fx.ResultTags(`name:"`+tagName+`"`),
),
fx.Annotate(
func(c *Client) HealthCheck {
return c
},
fx.ParamTags(`name:"`+tagName+`"`),
fx.ResultTags(`group:"health"`),
),
),
)
}
var c1 = clientFxModuleBuilder("c1", "c1", "service1.local")
var c2 = clientFxModuleBuilder("c2", "c2", "service2.local")
var db = fx.Module("db",
fx.Provide(
func() *DB {
return &DB{Addr: "db.local"}
},
fx.Annotate(
func(c *DB) *DB {
return c
},
fx.ResultTags(`group:"health"`),
),
),
)
var healthcheck = fx.Module("healthcheck",
fx.Invoke(
fx.Annotate(
func(clients []HealthCheck) {
for _, c := range clients {
fmt.Println(c.Serving())
}
},
fx.ParamTags(`group:"health"`),
),
),
)
func main() {
fx.New(
c1,
c2,
db,
healthcheck,
fx.Invoke(
fx.Annotate(
func(c1 *Client, c2 *Client, db *DB) {
fmt.Println(c1.Addr, c2.Addr, db.Addr)
},
fx.ParamTags(`name:"c1"`, `name:"c2"`),
),
),
).Run()
}
Expected behavior
Would be nice to receive some warning or similar notice.
Additional context
I have found that you can ask for same group with another type again. This might make that bit more complicated to spot not used values. Like
- func(clients []HealthCheck) {
+ func(clients []HealthCheck, dbs []*DB) {
for _, c := range clients {
fmt.Println(c.Serving())
}
+ for _, c := range dbs {
+ fmt.Println("DB checked:", c.Addr)
+ }
},
- fx.ParamTags(`group:"health"`),
+ fx.ParamTags(`group:"health"`, `group:"health"`),