uber-go / fx

A dependency injection based application framework for Go.
https://uber-go.github.io/fx/
MIT License
5.65k stars 287 forks source link

fx.Private yields incorrect fx.Dotgraph visualization #1014

Open rossb83 opened 1 year ago

rossb83 commented 1 year ago

Describe the bug The fx.Dotgraph displayed when visualizing the dependency graph is incorrect when using the new fx.Private variable.

To Reproduce For the following code it prints as expected "foo\nbar" meaning ModuleB has a dependency on ModuleA and ModuleC has a dependency on AnotherModuleA.

package main

import (
    "fmt"
    "go.uber.org/fx"
)

type A struct{ text string }
type B struct{ a *A }
type C struct{ a *A }

func NewA() *A           { return &A{"foo"} }
func AnotherNewA() *A    { return &A{"bar"} }
func NewB(a *A) *B       { return &B{a: a} }
func NewC(a *A, b *B) *C { return &C{a: a} }

var ModuleA = fx.Provide(NewA)
var AnotherModuleA = fx.Provide(fx.Annotate(AnotherNewA, fx.ResultTags(`name:"anotherA"`)))
var ModuleB = fx.Provide(NewB)
var ModuleC = fx.Provide(NewC)

func main() {
    fx.New(
        ModuleA,
        ModuleB,
        AnotherModuleA,
        fx.Module(
            "submodule",
            fx.Provide(
                fx.Annotate(func(a *A) *A {
                    return a
                }, fx.ParamTags(`name:"anotherA"`)), fx.Private),
            ModuleC,
        ),
        fx.Invoke(func(b *B, c *C, g fx.DotGraph) {
            fmt.Println(b.a.text)
            fmt.Println(c.a.text)
        }),
    )
}

Yet the fx.DotGraph printed looks like this where the incorrect edge is highlighted in red. graphviz (3)

Expected behavior My expectation is the dotgraph look like this again the corrected edge is highlighted in red. graphviz (4)

Additional Context While this bug is a subtle point, the visualization helps a lot when using new features like fx.Private to determine usage is correctly understood and seeing an incorrect visualization creates confusion and makes the user think they are the ones doing something wrong.

sywhang commented 1 year ago

@JacobOaks to take a look

JacobOaks commented 1 year ago

Hey there,

Thanks for reporting this. There are actually a couple issues with the graph visualization in addition to this at the moment. For example, decorators aren't represented, and neither are modules. I've reproduced your bug and filed an internal ticket to update the graph visualization code and fix these issues, ref: GO-1797.