Skip to content

Commit

Permalink
fix: update ftl Go module when building
Browse files Browse the repository at this point in the history
This updates the go.mod file at the root of a Go FTL module.
  • Loading branch information
alecthomas committed Jan 16, 2024
1 parent 7e5b073 commit 5674236
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 63 deletions.
45 changes: 45 additions & 0 deletions FTL.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"folders": [
{
"path": "."
},
{
"path": "go-runtime"
},
{
"path": "kotlin-runtime"
},
{
"path": "examples/time"
},
{
"path": "examples/echo"
},
{
"path": "examples/online-boutique"
},
{
"path": "backend"
},
{
"path": "cmd/ftl",
"name": "cmd/ftl"
}
],
"settings": {
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/_ftl": true,
"**/node_modules": true,
"**/go.work": true,
"**/go.work.sum": true,
".hermit": true,
"**/*.zip": true,
}
}
}
6 changes: 2 additions & 4 deletions cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/bmatcuk/doublestar/v4"

"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/protos/xyz/block/ftl/v1/ftlv1connect"
Expand Down Expand Up @@ -246,9 +245,8 @@ func initGitIgnore(ctx context.Context, dir string) []string {
if err == nil {
ignore = append(ignore, loadGitIgnore(home)...)
}
gitRootBytes, err := exec.Capture(ctx, dir, "git", "rev-parse", "--show-toplevel")
if err == nil {
gitRoot := strings.TrimSpace(string(gitRootBytes))
gitRoot := gitRoot(ctx, dir)
if gitRoot != "" {
for current := dir; strings.HasPrefix(current, gitRoot); current = path.Dir(current) {
ignore = append(ignore, loadGitIgnore(current)...)
}
Expand Down
20 changes: 20 additions & 0 deletions cmd/ftl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"
"runtime"
"strings"
"syscall"

"connectrpc.com/connect"
Expand All @@ -14,6 +15,7 @@ import (

"github.com/TBD54566975/ftl"
_ "github.com/TBD54566975/ftl/backend/common/automaxprocs" // Set GOMAXPROCS to match Linux container CPU quota.
"github.com/TBD54566975/ftl/backend/common/exec"
"github.com/TBD54566975/ftl/backend/common/log"
"github.com/TBD54566975/ftl/backend/common/rpc"
"github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/ftlv1connect"
Expand Down Expand Up @@ -98,3 +100,21 @@ func makeDialer[Client rpc.Pingable](newClient func(connect.HTTPClient, string,
return rpc.Dial(newClient, cli.Endpoint.String(), log.Error), nil
}
}

// gitRoot returns the root of the git repository containing dir, or empty string if dir is not in a git repository.
//
// If dir is empty, the current working directory is used.
func gitRoot(ctx context.Context, dir string) string {
if dir == "" {
var err error
dir, err = os.Getwd()
if err != nil {
return ""
}
}
gitRootBytes, err := exec.Capture(ctx, dir, "git", "rev-parse", "--show-toplevel")
if err != nil {
return ""
}
return strings.TrimSpace(string(gitRootBytes))
}
15 changes: 13 additions & 2 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/mod/modfile"
"google.golang.org/protobuf/proto"

"github.com/TBD54566975/ftl"
"github.com/TBD54566975/scaffolder"

"github.com/TBD54566975/ftl/backend/common/exec"
Expand Down Expand Up @@ -50,7 +51,7 @@ 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"))
goModVersion, err := updateGoModule(filepath.Join(moduleDir, "go.mod"))
if err != nil {
return err
}
Expand Down Expand Up @@ -197,7 +198,8 @@ func genType(module *schema.Module, t schema.Type) string {
panic(fmt.Sprintf("unsupported type %T", t))
}

func readGoModVersion(goModPath string) (string, error) {
// Update go.mod file to include the FTL version.
func updateGoModule(goModPath string) (version string, err error) {
goModBytes, err := os.ReadFile(goModPath)
if err != nil {
return "", fmt.Errorf("failed to read %s: %w", goModPath, err)
Expand All @@ -206,5 +208,14 @@ func readGoModVersion(goModPath string) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to parse %s: %w", goModPath, err)
}
if ftl.IsRelease(ftl.Version) {
if err := goModfile.AddRequire("github.com/TBD54566975/ftl", ftl.Version); err != nil {
return "", fmt.Errorf("failed to add github.com/TBD54566975/ftl to %s: %w", goModPath, err)
}
goModBytes = modfile.Format(goModfile.Syntax)
if err := os.WriteFile(goModPath, goModBytes, 0600); err != nil {
return "", fmt.Errorf("failed to update %s: %w", goModPath, err)
}
}
return goModfile.Go.Version, nil
}
57 changes: 0 additions & 57 deletions go-runtime/sdk/kvstore/kvstore.go

This file was deleted.

7 changes: 7 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package ftl

import "strings"

// IsRelease returns true if the version is a release version.
func IsRelease(v string) bool {
return v != "dev" && !strings.HasSuffix(v, "-dirty")
}

// Version of FTL binary (set by linker).
var Version = "dev"

Expand Down

0 comments on commit 5674236

Please sign in to comment.