Skip to content

Commit

Permalink
feat: ftl-language-go plugin (#3101)
Browse files Browse the repository at this point in the history
#2452
Separates go support into `ftl-language-go`
  • Loading branch information
matt2e authored Oct 31, 2024
1 parent 9917c4a commit 54b8be3
Show file tree
Hide file tree
Showing 51 changed files with 2,052 additions and 201 deletions.
13 changes: 12 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ build-java *args:
mvn -f jvm-runtime/ftl-runtime install {{args}}

# Builds all language plugins
build-language-plugins: build-python
build-language-plugins: build-go build-python

# Build ftl-language-go
build-go: build-zips build-protos
#!/bin/bash
shopt -s extglob

if [ "${FTL_DEBUG:-}" = "true" ]; then
go build -o "{{RELEASE}}/ftl-language-go" -tags release -gcflags=all="-N -l" -ldflags "-X github.com/TBD54566975/ftl.Version={{VERSION}} -X github.com/TBD54566975/ftl.Timestamp={{TIMESTAMP}}" "./go-runtime/cmd/ftl-language-go"
else
mk "{{RELEASE}}/ftl-language-go" : !(build|integration) -- go build -o "{{RELEASE}}/ftl-language-go" -tags release -ldflags "-X github.com/TBD54566975/ftl.Version={{VERSION}} -X github.com/TBD54566975/ftl.Timestamp={{TIMESTAMP}}" "./go-runtime/cmd/ftl-language-go"
fi

# Build ftl-language-python
build-python: build-zips build-protos
Expand Down
23 changes: 23 additions & 0 deletions go-runtime/cmd/ftl-language-go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"context"
"os"

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/language/languagepbconnect"
"github.com/TBD54566975/ftl/common/plugin"
"github.com/TBD54566975/ftl/go-runtime/goplugin"
)

func main() {
plugin.Start(context.Background(),
os.Getenv("FTL_NAME"),
createService,
languagepbconnect.LanguageServiceName,
languagepbconnect.NewLanguageServiceHandler)
}

func createService(ctx context.Context, config any) (context.Context, *goplugin.Service, error) {
svc := goplugin.New()
return ctx, svc, nil
}
10 changes: 5 additions & 5 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ func buildDir(moduleDir string) string {
}

// Build the given module.
func Build(ctx context.Context, projectRootDir, stubsRoot string, config moduleconfig.AbsModuleConfig, sch *schema.Schema, filesTransaction ModifyFilesTransaction, buildEnv []string, devMode bool) (moduleSch *schema.Module, buildErrors []builderrors.Error, err error) {
func Build(ctx context.Context, projectRootDir, stubsRoot string, config moduleconfig.AbsModuleConfig, sch *schema.Schema, filesTransaction ModifyFilesTransaction, devMode bool) (moduleSch optional.Option[*schema.Module], buildErrors []builderrors.Error, err error) {
if err := filesTransaction.Begin(); err != nil {
return nil, nil, fmt.Errorf("could not start a file transaction: %w", err)
return moduleSch, nil, fmt.Errorf("could not start a file transaction: %w", err)
}
defer func() {
if terr := filesTransaction.End(); terr != nil {
Expand All @@ -262,7 +262,7 @@ func Build(ctx context.Context, projectRootDir, stubsRoot string, config modulec

replacements, goModVersion, err := updateGoModule(filepath.Join(config.Dir, "go.mod"))
if err != nil {
return nil, nil, err
return moduleSch, nil, err
}

goVersion := runtime.Version()[2:]
Expand Down Expand Up @@ -366,7 +366,7 @@ func Build(ctx context.Context, projectRootDir, stubsRoot string, config modulec
}
// We have seen lots of upstream HTTP/2 failures that make CI unstable.
// Disable HTTP/2 for now during the build. This can probably be removed later
buildEnv = append(buildEnv, "GODEBUG=http2client=0")
buildEnv := []string{"GODEBUG=http2client=0"}
err = exec.CommandWithEnv(ctx, log.Debug, mainDir, buildEnv, "go", args...).RunStderrError(ctx)
if err != nil {
return moduleSch, nil, fmt.Errorf("failed to compile: %w", err)
Expand All @@ -381,7 +381,7 @@ func Build(ctx context.Context, projectRootDir, stubsRoot string, config modulec
if err != nil {
return moduleSch, nil, fmt.Errorf("failed to write launch script: %w", err)
}
return result.Module, result.Errors, nil
return optional.Some(result.Module), result.Errors, nil
}

type mainModuleContextBuilder struct {
Expand Down
6 changes: 3 additions & 3 deletions go-runtime/compile/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func GenerateStubs(ctx context.Context, dir string, moduleSch *schema.Module, co
return nil
}

func SyncGeneratedStubReferences(ctx context.Context, config moduleconfig.ModuleConfig, stubsDir string, stubbedModules []string) error {
func SyncGeneratedStubReferences(ctx context.Context, config moduleconfig.AbsModuleConfig, stubsDir string, stubbedModules []string) error {
sharedModulePaths := []string{}
for _, mod := range stubbedModules {
if mod == config.Module {
Expand All @@ -93,13 +93,13 @@ func SyncGeneratedStubReferences(ctx context.Context, config moduleconfig.Module
sharedModulePaths = append(sharedModulePaths, filepath.Join(stubsDir, mod))
}

_, goModVersion, err := updateGoModule(filepath.Join(config.Abs().Dir, "go.mod"))
_, goModVersion, err := updateGoModule(filepath.Join(config.Dir, "go.mod"))
if err != nil {
return err
}

funcs := maps.Clone(scaffoldFuncs)
if err := internal.ScaffoldZip(mainWorkTemplateFiles(), config.Abs().Dir, MainWorkContext{
if err := internal.ScaffoldZip(mainWorkTemplateFiles(), config.Dir, MainWorkContext{
GoVersion: goModVersion,
SharedModulesPaths: sharedModulePaths,
}, scaffolder.Exclude("^go.mod$"), scaffolder.Functions(funcs)); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build integration

package languageplugin
package goplugin

import (
"os"
Expand All @@ -14,7 +14,7 @@ import (
func TestGoBuildClearsBuildDir(t *testing.T) {
file := "./another/.ftl/test-clear-build.tmp"
in.Run(t,
in.WithTestDataDir("../testdata"),
in.WithTestDataDir("testdata"),
in.CopyModule("another"),
in.Build("another"),
in.WriteFile(file, []byte{1}),
Expand All @@ -26,7 +26,7 @@ func TestGoBuildClearsBuildDir(t *testing.T) {

func TestExternalType(t *testing.T) {
in.Run(t,
in.WithTestDataDir("../testdata"),
in.WithTestDataDir("testdata"),
in.CopyModule("external"),
in.ExpectError(in.Build("external"),
`unsupported type "time.Month" for field "Month"`,
Expand All @@ -43,7 +43,7 @@ func TestGeneratedTypeRegistry(t *testing.T) {
file := "other/.ftl/go/main/main.go"

in.Run(t,
in.WithTestDataDir("../testdata"),
in.WithTestDataDir("testdata"),
// Deploy dependency
in.CopyModule("another"),
in.Deploy("another"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package languageplugin
package goplugin

import (
"errors"
"fmt"
"go/parser"
"go/token"
Expand All @@ -9,8 +10,6 @@ import (
"strings"

"golang.org/x/mod/modfile"

"github.com/TBD54566975/ftl/internal/errors"
)

func replacementWatches(moduleDir, deployDir string) ([]string, error) {
Expand Down
Loading

0 comments on commit 54b8be3

Please sign in to comment.