diff --git a/go-runtime/compile/build.go b/go-runtime/compile/build.go index f36f15706..4c3d0bef7 100644 --- a/go-runtime/compile/build.go +++ b/go-runtime/compile/build.go @@ -87,7 +87,7 @@ func (c mainModuleContext) generateMainImports() []string { for _, e := range c.MainCtx.ExternalTypes { imports.Add(e.importStatement()) } - return formatGoImports(imports.ToSlice()) + return imports.ToSlice() } func (c mainModuleContext) generateTypesImports(mainModuleImport string) []string { @@ -118,7 +118,7 @@ func (c mainModuleContext) generateTypesImports(mainModuleImport string) []strin } filteredImports = append(filteredImports, im) } - return formatGoImports(filteredImports) + return filteredImports } func typeImports(t goSchemaType) []string { @@ -355,17 +355,26 @@ func Build(ctx context.Context, projectRootDir, moduleDir string, sch *schema.Sc logger.Debugf("Tidying go.mod files") wg, wgctx := errgroup.WithContext(ctx) + + ftlTypesFilename := "types.ftl.go" wg.Go(func() error { if err := exec.Command(wgctx, log.Debug, moduleDir, "go", "mod", "tidy").RunBuffered(wgctx); err != nil { return fmt.Errorf("%s: failed to tidy go.mod: %w", moduleDir, err) } - return filesTransaction.ModifiedFiles(filepath.Join(moduleDir, "go.mod"), filepath.Join(moduleDir, "go.sum")) + + if err := exec.Command(wgctx, log.Debug, moduleDir, "go", "fmt", ftlTypesFilename).RunBuffered(wgctx); err != nil { + return fmt.Errorf("%s: failed to format module dir: %w", moduleDir, err) + } + return filesTransaction.ModifiedFiles(filepath.Join(moduleDir, "go.mod"), filepath.Join(moduleDir, "go.sum"), filepath.Join(moduleDir, ftlTypesFilename)) }) mainDir := filepath.Join(buildDir, "go", "main") wg.Go(func() error { if err := exec.Command(wgctx, log.Debug, mainDir, "go", "mod", "tidy").RunBuffered(wgctx); err != nil { return fmt.Errorf("%s: failed to tidy go.mod: %w", mainDir, err) } + if err := exec.Command(wgctx, log.Debug, mainDir, "go", "fmt", "./...").RunBuffered(wgctx); err != nil { + return fmt.Errorf("%s: failed to format main dir: %w", mainDir, err) + } return filesTransaction.ModifiedFiles(filepath.Join(mainDir, "go.mod"), filepath.Join(moduleDir, "go.sum")) }) if err := wg.Wait(); err != nil { @@ -1319,42 +1328,3 @@ func addImports(existingImports map[string]string, newTypes ...nativeType) map[s } return imports } - -func formatGoImports(imports []string) []string { - getPriority := func(path string) int { - // ftl import - if strings.HasPrefix(path, "\"ftl/") { - return 2 - } - // aliased ftl import - if parts := strings.SplitAfter(path, " "); len(parts) == 2 && strings.HasPrefix(parts[1], "\"ftl/") { - return 2 - } - // stdlib imports don't contain a dot (e.g., "fmt", "strings") - if !strings.Contains(path, ".") { - return 0 - } - return 1 - } - - slices.SortFunc(imports, func(i, j string) int { - priorityI := getPriority(i) - priorityJ := getPriority(j) - if priorityI != priorityJ { - return priorityI - priorityJ - } - return strings.Compare(i, j) - }) - - // add newlines between import groupings - previousPriority := -1 - for i, imp := range imports { - currentPriority := getPriority(imp) - if currentPriority != previousPriority && previousPriority != -1 { - imports[i-1] = fmt.Sprintf("%s\n", imports[i-1]) - } - previousPriority = currentPriority - } - - return imports -} diff --git a/internal/buildengine/testdata/alpha/types.ftl.go b/internal/buildengine/testdata/alpha/types.ftl.go index 8d59e7f67..8a98b8c46 100644 --- a/internal/buildengine/testdata/alpha/types.ftl.go +++ b/internal/buildengine/testdata/alpha/types.ftl.go @@ -2,12 +2,10 @@ package alpha import ( - "context" - - "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" - "github.com/TBD54566975/ftl/go-runtime/server" - - ftlother "ftl/other" + "context" + ftlother "ftl/other" + "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" + "github.com/TBD54566975/ftl/go-runtime/server" ) type EchoClient func(context.Context, EchoRequest) (EchoResponse, error) @@ -15,8 +13,8 @@ type EchoClient func(context.Context, EchoRequest) (EchoResponse, error) func init() { reflection.Register( reflection.ProvideResourcesForVerb( - Echo, - server.VerbClient[ftlother.EchoClient, ftlother.EchoRequest, ftlother.EchoResponse](), + Echo, + server.VerbClient[ftlother.EchoClient, ftlother.EchoRequest, ftlother.EchoResponse](), ), ) -} \ No newline at end of file +} diff --git a/internal/buildengine/testdata/another/types.ftl.go b/internal/buildengine/testdata/another/types.ftl.go index 832122b81..4652ba9af 100644 --- a/internal/buildengine/testdata/another/types.ftl.go +++ b/internal/buildengine/testdata/another/types.ftl.go @@ -2,10 +2,9 @@ package another import ( - "context" - - "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" - lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata" + "context" + "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" + lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata" ) type EchoClient func(context.Context, EchoRequest) (EchoResponse, error) @@ -22,7 +21,7 @@ func init() { ), reflection.ExternalType(*new(lib.AnotherNonFTLType)), reflection.ProvideResourcesForVerb( - Echo, + Echo, ), ) -} \ No newline at end of file +} diff --git a/internal/buildengine/testdata/other/types.ftl.go b/internal/buildengine/testdata/other/types.ftl.go index 1269b3711..9e30e8e6c 100644 --- a/internal/buildengine/testdata/other/types.ftl.go +++ b/internal/buildengine/testdata/other/types.ftl.go @@ -2,10 +2,9 @@ package other import ( - "context" - - "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" - lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata" + "context" + "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" + lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata" ) type EchoClient func(context.Context, EchoRequest) (EchoResponse, error) @@ -32,7 +31,7 @@ func init() { reflection.ExternalType(*new(lib.NonFTLType)), reflection.ExternalType(*new(lib.AnotherNonFTLType)), reflection.ProvideResourcesForVerb( - Echo, + Echo, ), ) -} \ No newline at end of file +} diff --git a/internal/buildengine/testdata/type_registry_main.go b/internal/buildengine/testdata/type_registry_main.go index 4e8f898c3..2283fb995 100644 --- a/internal/buildengine/testdata/type_registry_main.go +++ b/internal/buildengine/testdata/type_registry_main.go @@ -3,15 +3,13 @@ package main import ( "context" - + ftlanother "ftl/another" + ftlother "ftl/other" "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect" "github.com/TBD54566975/ftl/common/plugin" "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" - "github.com/TBD54566975/ftl/go-runtime/server" lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata" - - ftlanother "ftl/another" - ftlother "ftl/other" + "github.com/TBD54566975/ftl/go-runtime/server" ) func init() { @@ -40,7 +38,7 @@ func init() { reflection.ExternalType(*new(lib.NonFTLType)), reflection.ExternalType(*new(lib.AnotherNonFTLType)), reflection.ProvideResourcesForVerb( - ftlother.Echo, + ftlother.Echo, ), ) } diff --git a/smoketest/origin/types.ftl.go b/smoketest/origin/types.ftl.go new file mode 100644 index 000000000..51de3d963 --- /dev/null +++ b/smoketest/origin/types.ftl.go @@ -0,0 +1,24 @@ +// Code generated by FTL. DO NOT EDIT. +package origin + +import ( + "context" + ftlbuiltin "ftl/builtin" + "github.com/TBD54566975/ftl/go-runtime/ftl" + "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" +) + +type GetNonceClient func(context.Context, GetNonceRequest) (GetNonceResponse, error) + +type PostAgentClient func(context.Context, ftlbuiltin.HttpRequest[Agent, ftl.Unit, ftl.Unit]) (ftlbuiltin.HttpResponse[PostAgentResponse, PostAgentErrorResponse], error) + +func init() { + reflection.Register( + reflection.ProvideResourcesForVerb( + GetNonce, + ), + reflection.ProvideResourcesForVerb( + PostAgent, + ), + ) +} diff --git a/smoketest/relay/types.ftl.go b/smoketest/relay/types.ftl.go new file mode 100644 index 000000000..b963c390a --- /dev/null +++ b/smoketest/relay/types.ftl.go @@ -0,0 +1,48 @@ +// Code generated by FTL. DO NOT EDIT. +package relay + +import ( + "context" + ftlorigin "ftl/origin" + "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" +) + +type BriefedClient func(context.Context, ftlorigin.Agent) error + +type ConsumeAgentBroadcastClient func(context.Context, ftlorigin.Agent) error + +type DeployedClient func(context.Context, AgentDeployment) error + +type GetLogFileClient func(context.Context, GetLogFileRequest) (GetLogFileResponse, error) + +type MissionResultClient func(context.Context, MissionResultRequest) (MissionResultResponse, error) + +type SucceededClient func(context.Context, MissionSuccess) error + +type TerminatedClient func(context.Context, AgentTerminated) error + +func init() { + reflection.Register( + reflection.ProvideResourcesForVerb( + Briefed, + ), + reflection.ProvideResourcesForVerb( + ConsumeAgentBroadcast, + ), + reflection.ProvideResourcesForVerb( + Deployed, + ), + reflection.ProvideResourcesForVerb( + GetLogFile, + ), + reflection.ProvideResourcesForVerb( + MissionResult, + ), + reflection.ProvideResourcesForVerb( + Succeeded, + ), + reflection.ProvideResourcesForVerb( + Terminated, + ), + ) +}