From 159211ae78e146f2c1d595410831464ba67cb915 Mon Sep 17 00:00:00 2001 From: Florent Lecoultre <104759954+fl0Lec@users.noreply.github.com> Date: Tue, 12 Dec 2023 21:41:02 +0100 Subject: [PATCH] fix: add -c flags when building go test (#4473) when building a go test without the `-c` flag like so: ```yaml builds: - command: test binary: env.test dir: ./internal/builders/golang no_main_check: true ``` will result in the error: `exit status 1: fork/exec : exec format error` adding the -c flags when not present in the flags adding unit test https://github.com/goreleaser/goreleaser/issues/4462 --------- Co-authored-by: Carlos Alexandro Becker --- internal/builders/golang/build.go | 4 ++++ internal/builders/golang/build_test.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/internal/builders/golang/build.go b/internal/builders/golang/build.go index 3cff392b4ef..9875e4d5e07 100644 --- a/internal/builders/golang/build.go +++ b/internal/builders/golang/build.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "dario.cat/mergo" @@ -283,6 +284,9 @@ func buildGoBuildLine(ctx *context.Context, build config.Build, details config.B return cmd, err } cmd = append(cmd, flags...) + if build.Command == "test" && !slices.Contains(flags, "-c") { + cmd = append(cmd, "-c") + } asmflags, err := processFlags(ctx, artifact, env, details.Asmflags, "-asmflags=") if err != nil { diff --git a/internal/builders/golang/build_test.go b/internal/builders/golang/build_test.go index c974ff94017..19f39a19771 100644 --- a/internal/builders/golang/build_test.go +++ b/internal/builders/golang/build_test.go @@ -1175,6 +1175,15 @@ func TestBuildGoBuildLine(t *testing.T) { }, strings.Fields("go test -c -o foo.test .")) }) + t.Run("build test always as c flags", func(t *testing.T) { + requireEqualCmd(t, config.Build{ + Main: ".", + GoBinary: "go", + Command: "test", + Binary: "foo.test", + }, strings.Fields("go test -c -o foo.test .")) + }) + t.Run("ldflags1", func(t *testing.T) { requireEqualCmd(t, config.Build{ Main: ".",