Skip to content

Commit

Permalink
fix: use Go version from the FTL module's go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Jan 15, 2024
1 parent 7223c9a commit 6ce364c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/echo/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ftl/echo

go 1.21.6
go 1.21.5

require github.com/TBD54566975/ftl v0.95.0

Expand Down
2 changes: 1 addition & 1 deletion examples/time/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ftl/time

go 1.21.6
go 1.21.5
4 changes: 3 additions & 1 deletion go-runtime/compile/build-template/_ftl/go/main/go.mod.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module main
module main

go {{ .GoVersion }}
41 changes: 35 additions & 6 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ import (
"reflect"
"strings"

"github.com/TBD54566975/scaffolder"
"github.com/iancoleman/strcase"
"golang.org/x/mod/modfile"
"google.golang.org/protobuf/proto"

"github.com/TBD54566975/scaffolder"

"github.com/TBD54566975/ftl/backend/common/exec"
"github.com/TBD54566975/ftl/backend/common/log"
"github.com/TBD54566975/ftl/backend/common/moduleconfig"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal"
)

type buildContext struct {
type externalModuleContext struct {
ModuleDir string
*schema.Schema
Main string
GoVersion string
Main string
}

type mainModuleContext struct {
GoVersion string
*schema.Module
}

func (b buildContext) NonMainModules() []*schema.Module {
func (b externalModuleContext) NonMainModules() []*schema.Module {
modules := make([]*schema.Module, 0, len(b.Modules)-1)
for _, module := range b.Modules {
if module.Name == b.Main {
Expand All @@ -42,6 +50,11 @@ const buildDirName = "_ftl"

// Build the given module.
func Build(ctx context.Context, moduleDir string, sch *schema.Schema) error {
goModVersion, err := readGoModVersion(filepath.Join(moduleDir, "go.mod"))
if err != nil {
return err
}

config, err := moduleconfig.LoadConfig(moduleDir)
if err != nil {
return fmt.Errorf("failed to load module config: %w", err)
Expand All @@ -56,8 +69,9 @@ func Build(ctx context.Context, moduleDir string, sch *schema.Schema) error {
_ = os.RemoveAll(filepath.Join(buildDir, "go", "modules"))

logger.Infof("Generating external modules")
if err := internal.ScaffoldZip(externalModuleTemplateFiles(), moduleDir, buildContext{
if err := internal.ScaffoldZip(externalModuleTemplateFiles(), moduleDir, externalModuleContext{
ModuleDir: moduleDir,
GoVersion: goModVersion,
Schema: sch,
Main: config.Module,
}, scaffolder.Exclude("^go.mod$"), scaffolder.Functions(funcs)); err != nil {
Expand All @@ -79,7 +93,10 @@ func Build(ctx context.Context, moduleDir string, sch *schema.Schema) error {
}

logger.Infof("Generating main module")
if err := internal.ScaffoldZip(buildTemplateFiles(), moduleDir, main, scaffolder.Exclude("^go.mod$"), scaffolder.Functions(funcs)); err != nil {
if err := internal.ScaffoldZip(buildTemplateFiles(), moduleDir, mainModuleContext{
GoVersion: goModVersion,
Module: main,
}, scaffolder.Exclude("^go.mod$"), scaffolder.Functions(funcs)); err != nil {
return err
}

Expand Down Expand Up @@ -179,3 +196,15 @@ func genType(module *schema.Module, t schema.Type) string {
}
panic(fmt.Sprintf("unsupported type %T", t))
}

func readGoModVersion(goModPath string) (string, error) {
goModBytes, err := os.ReadFile(goModPath)
if err != nil {
return "", fmt.Errorf("failed to read %s: %w", goModPath, err)
}
goModfile, err := modfile.Parse(goModPath, goModBytes, nil)
if err != nil {
return "", fmt.Errorf("failed to parse %s: %w", goModPath, err)
}
return goModfile.Go.Version, nil
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ftl

go 1.21.6
go {{ .GoVersion }}

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

2 changes: 1 addition & 1 deletion go-runtime/compile/external-module-template/go.work.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.21.6
go {{ .GoVersion }}

use (
.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ftl/{{ .Name }}

go 1.21.6
go {{ .GoVersion }}

require github.com/TBD54566975/ftl latest

0 comments on commit 6ce364c

Please sign in to comment.