From 16588c54ee5504c09e103800495d4050fbb2365e Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Mon, 25 Mar 2024 14:48:04 +1100 Subject: [PATCH] fix: external_module.go with sinks/sources build failures (#1132) Fixes #1095 Sink & Source verbs would have a schema like ``` verb returnsUser(Unit) two.UserResponse ``` but the scaffolded `external_module.go` version of it would be: ``` func ReturnsUser(ctx context.Context) (UserResponse, error) ``` When generating imports, we were finding `schema.Unit` in the verb which triggered an inclusion of ftl This caused the exernal module to fail to build because ftl was not being used by the generated code. This fix now skips any unit in the schema if the parent is a verb --- backend/schema/visit.go | 6 ++++++ buildengine/build_go_test.go | 1 - go-runtime/compile/schema_test.go | 2 +- go-runtime/compile/testdata/two/two.go | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/backend/schema/visit.go b/backend/schema/visit.go index c3430329ca..c3aa4ab292 100644 --- a/backend/schema/visit.go +++ b/backend/schema/visit.go @@ -19,6 +19,12 @@ func VisitExcludingMetadataChildren(n Node, visit func(n Node, next func() error return visit(n, func() error { if _, ok := n.(Metadata); !ok { for _, child := range n.schemaChildren() { + _, isParentVerb := n.(*Verb) + _, isChildUnit := child.(*Unit) + if isParentVerb && isChildUnit { + // Skip visiting children of a verb that are units as the scaffolded code will not inclue them + continue + } if err := VisitExcludingMetadataChildren(child, visit); err != nil { return err } diff --git a/buildengine/build_go_test.go b/buildengine/build_go_test.go index 13007f4b21..ecc380dec7 100644 --- a/buildengine/build_go_test.go +++ b/buildengine/build_go_test.go @@ -67,7 +67,6 @@ package other import ( "context" - "github.com/TBD54566975/ftl/go-runtime/ftl" ) var _ = context.Background diff --git a/go-runtime/compile/schema_test.go b/go-runtime/compile/schema_test.go index 2719e4c431..78edb16888 100644 --- a/go-runtime/compile/schema_test.go +++ b/go-runtime/compile/schema_test.go @@ -148,7 +148,7 @@ func TestExtractModuleSchemaTwo(t *testing.T) { verb callsTwo(two.Payload) two.Payload +calls two.two - verb returnsUser(Unit) two.UserResponse? + verb returnsUser(Unit) two.UserResponse verb two(two.Payload) two.Payload } diff --git a/go-runtime/compile/testdata/two/two.go b/go-runtime/compile/testdata/two/two.go index c823117ca8..c33fa3d644 100644 --- a/go-runtime/compile/testdata/two/two.go +++ b/go-runtime/compile/testdata/two/two.go @@ -42,10 +42,10 @@ func CallsTwo(ctx context.Context, req Payload[string]) (Payload[string], error) } //ftl:export -func ReturnsUser(ctx context.Context) (ftl.Option[UserResponse], error) { - return ftl.Some[UserResponse](UserResponse{ +func ReturnsUser(ctx context.Context) (UserResponse, error) { + return UserResponse{ User: User{ Name: "John Doe", }, - }), nil + }, nil }