From 40323813c4d72f145447f9ce605c70d7fcc5dbdf Mon Sep 17 00:00:00 2001 From: "Kasper J. Hermansen" Date: Sat, 28 Oct 2023 13:13:43 +0200 Subject: [PATCH] fix: golang actions now work in go workspaces Previously if a go.work (workspaces) exist, the build fails to compile, this is because we cannot do a proper build without the dependencies being shared between all the modules. This sets GOWORK=off for only the internal builds Signed-off-by: Kasper J. Hermansen --- examples/moon-base/actions/dev.go | 1 - pkg/executors/golang/codegen/compilation.go | 8 +++++++- pkg/executors/golang/compile/compile.go | 8 ++++---- pkg/executors/golang/executer/prepare.go | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/moon-base/actions/dev.go b/examples/moon-base/actions/dev.go index 6a8b7db..865bb24 100644 --- a/examples/moon-base/actions/dev.go +++ b/examples/moon-base/actions/dev.go @@ -2,7 +2,6 @@ package main import ( "context" - _ "github.com/lunarway/shuttle" ) func Dev(ctx context.Context) error { diff --git a/pkg/executors/golang/codegen/compilation.go b/pkg/executors/golang/codegen/compilation.go index a40d80a..d54f80a 100644 --- a/pkg/executors/golang/codegen/compilation.go +++ b/pkg/executors/golang/codegen/compilation.go @@ -2,6 +2,8 @@ package codegen import ( "context" + "log" + "os" "os/exec" "path" @@ -10,11 +12,15 @@ import ( func CompileBinary(ctx context.Context, ui *ui.UI, shuttlelocaldir string) (string, error) { cmd := exec.Command("go", "build") + cmd.Env = os.Environ() + // We need to set workspaces off, as we don't want users to have to add the golang modules to their go.work + cmd.Env = append(cmd.Env, "GOWORK=off") + cmd.Dir = path.Join(shuttlelocaldir, "tmp") output, err := cmd.CombinedOutput() if err != nil { - ui.Verboseln("compile-binary output: %s", string(output)) + log.Printf("compile-binary output: %s", string(output)) return "", err } diff --git a/pkg/executors/golang/compile/compile.go b/pkg/executors/golang/compile/compile.go index 5afd82e..3d38513 100644 --- a/pkg/executors/golang/compile/compile.go +++ b/pkg/executors/golang/compile/compile.go @@ -115,20 +115,20 @@ func compile(ctx context.Context, ui *ui.UI, actions *discover.ActionsDiscovered if goInstalled() { if err = codegen.Format(ctx, ui, shuttlelocaldir); err != nil { - return "", err + return "", fmt.Errorf("go fmt failed: %w", err) } if err = codegen.ModTidy(ctx, ui, shuttlelocaldir); err != nil { - return "", err + return "", fmt.Errorf("go mod tidy failed: %w", err) } binarypath, err = codegen.CompileBinary(ctx, ui, shuttlelocaldir) if err != nil { - return "", err + return "", fmt.Errorf("go build failed: %w", err) } } else { binarypath, err = compileWithDagger(ctx, ui, shuttlelocaldir) if err != nil { - return "", err + return "", fmt.Errorf("failed to compile with dagger: %w", err) } } diff --git a/pkg/executors/golang/executer/prepare.go b/pkg/executors/golang/executer/prepare.go index 3d930b7..7a342e3 100644 --- a/pkg/executors/golang/executer/prepare.go +++ b/pkg/executors/golang/executer/prepare.go @@ -21,7 +21,7 @@ func prepare( disc, err := discover.Discover(ctx, path, c) if err != nil { - return nil, fmt.Errorf("failed to fiscover actions: %v", err) + return nil, fmt.Errorf("failed to discover actions: %v", err) } binaries, err := compile.Compile(ctx, ui, disc)