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

Dependency cycle not detected when using DeferAcyclicVerification() (default in fx.new()) #271

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func verifyAcyclic(c containerStore, n provider, k key) error {
return err
}

// When first called from `(c *Container) verifyAcyclic()` (`DeferAcyclicVerification()`
// option is used and this is triggered lazily in `Invoke()`) the `path` here
// is set to nil.
func detectCycles(n provider, c containerStore, path []cycleEntry, visited map[key]struct{}) error {
var err error
walkParam(n.ParamList(), paramVisitorFunc(func(param param) bool {
Expand Down Expand Up @@ -121,6 +124,7 @@ func detectCycles(n provider, c containerStore, path []cycleEntry, visited map[k
// Alternatively, if deferAcyclicVerification was set and detectCycles
// is only being called before the first Invoke, each node in the
// graph will be tested as the first element of the path, so any
// ^^^^^^^^^^^^^^^^^^^^^^^^^ WHY?
// cycle that exists is guaranteed to trip the following condition.
if path[0].Key == k {
err = errCycleDetected{Path: append(path, entry)}
Expand Down
3 changes: 3 additions & 0 deletions dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ func (c *Container) Invoke(function interface{}, opts ...InvokeOption) error {
return nil
}

// What is the motivation for introducing a function very similar to
// `verifyAcyclic(c containerStore, n provider, k key)` and why is
// the path set to nil in this case?
func (c *Container) verifyAcyclic() error {
visited := make(map[key]struct{})
for _, n := range c.nodes {
Expand Down
18 changes: 10 additions & 8 deletions dig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1797,24 +1797,26 @@ func testProvideCycleFails(t *testing.T, dryRun bool) {
})

t.Run("DeferAcyclicVerification bypasses cycle check, VerifyAcyclic catches cycle", func(t *testing.T) {
// A <- B <- C <- D
// | ^
// |_________|
// The offending node `C` is now *not* the first on the dependency `path`
// of `detectCycles()` (`D` is: we first call `detectCycles()` for the
// inovked `A` with the initial path set to nil, the following call for
// its dependency `D` will be in path[0]).
// A <-- C <- D
// | |__^ ^
// |______________|
type A struct{}
type B struct{}
type C struct{}
type D struct{}
newA := func(*C) *A { return &A{} }
newB := func(*A) *B { return &B{} }
newC := func(*B) *C { return &C{} }
newA := func(*D) *A { return &A{} }
newC := func(*C) *C { return &C{} }
newD := func(*C) *D { return &D{} }

c := New(DeferAcyclicVerification())
assert.NoError(t, c.Provide(newA))
assert.NoError(t, c.Provide(newB))
assert.NoError(t, c.Provide(newC))
assert.NoError(t, c.Provide(newD))

// Will stack overflow in this call:
err := c.Invoke(func(*A) {})
require.Error(t, err, "expected error when introducing cycle")
assert.True(t, IsCycleDetected(err))
Expand Down