Skip to content

Commit

Permalink
Entirely move blocking logic from build to module loading
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseli committed Apr 10, 2024
1 parent 121d2d8 commit 50c1540
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 56 deletions.
36 changes: 0 additions & 36 deletions buildengine/build_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/alecthomas/assert/v2"

"github.com/TBD54566975/ftl"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
)
Expand Down Expand Up @@ -198,38 +197,3 @@ func TestExternalType(t *testing.T) {
err = os.RemoveAll(buildDir)
assert.NoError(t, err, "Error removing build directory")
}

func TestBuildChecksMinFTLVersion(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
moduleDir := "testdata/projects/yetanother"
buildDir := "_ftl"

ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, log.Config{}))
module, err := LoadModule(moduleDir)
assert.NoError(t, err)

tests := []struct {
v string
wantErr bool
}{
{"dev", false},
{"0.129.2", false},
//{"0.129.1", true},
}

for _, test := range tests {
sch := &schema.Schema{}
ftl.Version = test.v
err = Build(ctx, sch, module)
if test.wantErr {
assert.Error(t, err, "when version=%v, we expected an error but did not receive one", test.v)
} else {
assert.NoError(t, err, "when version=%v, we received an unexpected error: %w", test.v, err)
}
}

err = os.RemoveAll(buildDir)
assert.NoError(t, err, "Error removing build directory")
}
2 changes: 1 addition & 1 deletion buildengine/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestDiscoverModules(t *testing.T) {
Language: "go",
Realm: "home",
Module: "yetanother",
MinFTLVersion: "1.2.3",
MinFTLVersion: "0.129.2",
Deploy: []string{"main"},
DeployDir: "_ftl",
Schema: "schema.pb",
Expand Down
2 changes: 1 addition & 1 deletion buildengine/testdata/projects/yetanother/ftl.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module = "yetanother"
language = "go"
min-ftl-version = "0.129.1"
min-ftl-version = "0.129.2"
6 changes: 6 additions & 0 deletions common/moduleconfig/moduleconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"

"github.com/BurntSushi/toml"

"github.com/TBD54566975/ftl"
)

// ModuleGoConfig is language-specific configuration for Go modules.
Expand Down Expand Up @@ -108,6 +110,10 @@ func setConfigDefaults(moduleDir string, config *ModuleConfig) error {
return fmt.Errorf("watch files must be relative to the module directory")
}
}
if !ftl.IsVersionAtLeastMin(ftl.Version, config.MinFTLVersion) {
return fmt.Errorf("FTL version '%v' predates the minimum version '%v'", ftl.Version, config.MinFTLVersion)
}

return nil
}

Expand Down
29 changes: 29 additions & 0 deletions common/moduleconfig/moduleconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package moduleconfig

import (
"testing"

"github.com/alecthomas/assert/v2"

"github.com/TBD54566975/ftl"
)

func TestLoadModuleConfig(t *testing.T) {
tests := []struct {
v string
wantErr bool
}{
{"dev", false},
{"1.2.4", false},
{"0.0.4", true},
}
for _, test := range tests {
ftl.Version = test.v
_, err := LoadModuleConfig("testdata/projects/sample")
if test.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}
}
3 changes: 3 additions & 0 deletions common/moduleconfig/testdata/projects/sample/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module = "sample"
language = "go"
min-ftl-version = "0.129.2"
2 changes: 2 additions & 0 deletions common/moduleconfig/testdata/projects/sample/ftl.toml~
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "sample"
language = "go"
43 changes: 43 additions & 0 deletions common/moduleconfig/testdata/projects/sample/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module ftl/sample

go 1.22.2

require github.com/TBD54566975/ftl v0.165.0

require (
connectrpc.com/connect v1.16.0 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/TBD54566975/scaffolder v0.8.0 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/kong v0.9.0 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/types v0.13.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
142 changes: 142 additions & 0 deletions common/moduleconfig/testdata/projects/sample/go.sum

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

21 changes: 21 additions & 0 deletions common/moduleconfig/testdata/projects/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sample

import (
"context"
"fmt"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
)

type EchoRequest struct {
Name ftl.Option[string] `json:"name"`
}

type EchoResponse struct {
Message string `json:"message"`
}

//ftl:export
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
9 changes: 0 additions & 9 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ func Build(ctx context.Context, moduleDir string, sch *schema.Schema) error {
}
logger := log.FromContext(ctx)

meetsMinVerReq, err := ftl.IsVersionAtLeastMin(ftl.Version, config.MinFTLVersion)
if err != nil {
return fmt.Errorf("failed to compare FTL version against minimum version: %w", err)
}
if !meetsMinVerReq {
return fmt.Errorf("FTL version '%v' predates the minimum version '%v'", ftl.Version, config.MinFTLVersion)
}

funcs := maps.Clone(scaffoldFuncs)

logger.Debugf("Generating external modules")
Expand Down Expand Up @@ -199,7 +191,6 @@ func GenerateStubsForExternalLibrary(ctx context.Context, dir string, schema *sc
Schema: schema,
Replacements: replacements,
})

}

func generateExternalModules(context ExternalModuleContext) error {
Expand Down
13 changes: 4 additions & 9 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"golang.org/x/mod/semver"
)

const VersionNumberParts int = 3

// IsRelease returns true if the version is a release version.
func IsRelease(v string) bool {
return regexp.MustCompile(`^\d+\.\d+\.\d+$`).MatchString(v)
Expand All @@ -17,19 +15,16 @@ func IsRelease(v string) bool {
// - minVersion is not defined (i.e. is emptystring)
// - v or minVersion is not a release version
// - v > minVersion when both v and minVersion are release versions
func IsVersionAtLeastMin(v string, minVersion string) (bool, error) {
func IsVersionAtLeastMin(v string, minVersion string) bool {
if minVersion == "" {
return true, nil
return true
}
if !IsRelease(v) || !IsRelease(minVersion) {
return true, nil
return true
}
return semver.Compare("v"+v, "v"+minVersion) >= 0, nil
return semver.Compare("v"+v, "v"+minVersion) >= 0
}

// VersionIsMock is set by tests and used to block evaluation of versions that look like release versions but are not real.
var VersionIsMock = false

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

Expand Down

0 comments on commit 50c1540

Please sign in to comment.