Skip to content

Commit

Permalink
fixes #1745
Browse files Browse the repository at this point in the history
buildGraph does not check for cycles as it walks dependency chains; this change detects a member of the cycle and reports an error identifying that member
  • Loading branch information
jonathanj-square committed Jun 13, 2024
1 parent dba835b commit bdfa4ef
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 16 deletions.
3 changes: 3 additions & 0 deletions buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (e *Engine) Graph(projects ...ProjectKey) (map[string][]string, error) {

func (e *Engine) buildGraph(key string, out map[string][]string) error {
var deps []string
if _, ok := out[key]; ok {
return fmt.Errorf("module %q contains a cyclical dependency", key)
}
if meta, ok := e.projectMetas.Load(ProjectKey(key)); ok {
deps = meta.project.Config().Dependencies
} else if sch, ok := e.controllerSchema.Load(key); ok {
Expand Down
14 changes: 14 additions & 0 deletions buildengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ func TestEngine(t *testing.T) {
err = engine.Build(ctx)
assert.NoError(t, err)
}

func TestCycleDetection(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil, []string{"testdata/projects/depcycle1", "testdata/projects/depcycle2"}, nil)
assert.NoError(t, err)

defer engine.Close()

_, err = engine.Graph()
assert.Contains(t, err.Error(), "contains a cyclical dependency")
}
18 changes: 18 additions & 0 deletions buildengine/testdata/projects/depcycle1/depcycle1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package depcycle1

import (
"context"
"fmt"
"ftl/depcycle2"
)

type Request struct{}
type Response struct {
Message string
}

//ftl:verb export
func Cycle1(ctx context.Context, req Request) (Response, error) {
var resp depcycle2.Response
return Response{Message: fmt.Sprintf("cycle1 %s", resp)}, nil
}
2 changes: 2 additions & 0 deletions buildengine/testdata/projects/depcycle1/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "depcycle1"
language = "go"
5 changes: 5 additions & 0 deletions buildengine/testdata/projects/depcycle1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ftl/depcycle1

go 1.22.2

replace github.com/TBD54566975/ftl => ./../../../../..
18 changes: 18 additions & 0 deletions buildengine/testdata/projects/depcycle2/depcycle2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package depcycle2

import (
"context"
"fmt"
"ftl/depcycle1"
)

type Request struct{}
type Response struct {
Message string
}

//ftl:verb export
func Cycle2(ctx context.Context, req Request) (Response, error) {
var resp depcycle1.Response
return Response{Message: fmt.Sprintf("cycle2 %s", resp)}, nil
}
2 changes: 2 additions & 0 deletions buildengine/testdata/projects/depcycle2/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "depcycle2"
language = "go"
5 changes: 5 additions & 0 deletions buildengine/testdata/projects/depcycle2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ftl/depcycle2

go 1.22.2

replace github.com/TBD54566975/ftl => ./../../../../..
16 changes: 8 additions & 8 deletions go-runtime/ftl/ftltest/testdata/go/pubsub/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ require (
github.com/alecthomas/repr v0.4.0 // indirect
github.com/alecthomas/types v0.16.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/amacneil/dbmate/v2 v2.16.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.27.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.7 // indirect
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.29.1 // indirect
github.com/amacneil/dbmate/v2 v2.17.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.27.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.9 // indirect
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.29.3 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
Expand All @@ -47,18 +47,18 @@ require (
github.com/puzpuzpuz/xsync/v3 v3.1.0 // indirect
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
github.com/zalando/go-keyring v0.2.5 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

replace github.com/TBD54566975/ftl => ./../../../../../..
8 changes: 8 additions & 0 deletions go-runtime/ftl/ftltest/testdata/go/pubsub/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions go-runtime/ftl/ftltest/testdata/go/subscriber/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ require (
github.com/alecthomas/repr v0.4.0 // indirect
github.com/alecthomas/types v0.16.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/amacneil/dbmate/v2 v2.16.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.27.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.7 // indirect
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.29.1 // indirect
github.com/amacneil/dbmate/v2 v2.17.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.27.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.9 // indirect
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.29.3 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
Expand All @@ -46,18 +46,18 @@ require (
github.com/puzpuzpuz/xsync/v3 v3.1.0 // indirect
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
github.com/zalando/go-keyring v0.2.5 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

replace github.com/TBD54566975/ftl => ./../../../../../..
8 changes: 8 additions & 0 deletions go-runtime/ftl/ftltest/testdata/go/subscriber/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bdfa4ef

Please sign in to comment.