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

Wrong type in group is ignored without warning #1250

Open
arxeiss opened this issue Dec 4, 2024 · 0 comments
Open

Wrong type in group is ignored without warning #1250

arxeiss opened this issue Dec 4, 2024 · 0 comments

Comments

@arxeiss
Copy link

arxeiss commented Dec 4, 2024

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"`),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant