Skip to content

Commit ddb708f

Browse files
authored
Stop Decorator Running When Dry Run Set (#371)
Currently, decorators still run even when dry run is set in the container (see Fx issue: uber-go/fx#1018) This changes decorators to use the scope's invoker when decorating, causing it to fill-in zero-valued results instead of actually running the function when the dry run option is set.
1 parent cdbd7eb commit ddb708f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

decorate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (n *decoratorNode) Call(s containerStore) (err error) {
128128
}
129129
}
130130

131-
results := reflect.ValueOf(n.dcor).Call(args)
131+
results := s.invoker()(reflect.ValueOf(n.dcor), args)
132132
if err := n.results.ExtractList(n.s, true /* decorated */, results); err != nil {
133133
return err
134134
}

dig_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,22 @@ func TestDryModeSuccess(t *testing.T) {
20502050
c.RequireProvide(provides)
20512051
c.RequireInvoke(invokes)
20522052
})
2053+
t.Run("does not call decorators", func(t *testing.T) {
2054+
type type1 struct{}
2055+
provides := func() *type1 {
2056+
t.Fatal("must not be called")
2057+
return &type1{}
2058+
}
2059+
decorates := func(*type1) *type1 {
2060+
t.Fatal("must not be called")
2061+
return &type1{}
2062+
}
2063+
invokes := func(*type1) {}
2064+
c := digtest.New(t, dig.DryRun(true))
2065+
c.RequireProvide(provides)
2066+
c.RequireDecorate(decorates)
2067+
c.RequireInvoke(invokes)
2068+
})
20532069
}
20542070

20552071
func TestProvideCycleFails(t *testing.T) {

0 commit comments

Comments
 (0)