Skip to content

Commit

Permalink
fix: format generated ftl.types.go file (#2927)
Browse files Browse the repository at this point in the history
Fixes #2918
  • Loading branch information
wesbillman authored Oct 1, 2024
1 parent eaf08b2 commit e018a6c
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 69 deletions.
54 changes: 12 additions & 42 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
16 changes: 7 additions & 9 deletions internal/buildengine/testdata/alpha/types.ftl.go

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

11 changes: 5 additions & 6 deletions internal/buildengine/testdata/another/types.ftl.go

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

11 changes: 5 additions & 6 deletions internal/buildengine/testdata/other/types.ftl.go

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

10 changes: 4 additions & 6 deletions internal/buildengine/testdata/type_registry_main.go

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

24 changes: 24 additions & 0 deletions smoketest/origin/types.ftl.go

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

48 changes: 48 additions & 0 deletions smoketest/relay/types.ftl.go

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

0 comments on commit e018a6c

Please sign in to comment.