From 4a510999646d7b6c794fd99a0b43bf4fbd16897e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 4 Oct 2022 13:01:18 -0300 Subject: [PATCH] fix: gomod.env not being used (#3434) Fixes #3426 Signed-off-by: Carlos A Becker --- internal/pipe/gomod/gomod.go | 4 +++- internal/pipe/gomod/gomod_test.go | 13 +++++++++++++ www/docs/customization/build.md | 3 ++- www/docs/customization/verifiable_builds.md | 8 +++++--- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/pipe/gomod/gomod.go b/internal/pipe/gomod/gomod.go index 3803c729b15..723a88730a0 100644 --- a/internal/pipe/gomod/gomod.go +++ b/internal/pipe/gomod/gomod.go @@ -33,7 +33,9 @@ func (Pipe) Run(ctx *context.Context) error { if ctx.Config.GoMod.Mod != "" { flags = append(flags, "-mod="+ctx.Config.GoMod.Mod) } - out, err := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...).CombinedOutput() + cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...) + cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...) + out, err := cmd.CombinedOutput() result := strings.TrimSpace(string(out)) if result == go115NotAGoModuleError || result == go116NotAGoModuleError { return pipe.Skip("not a go module") diff --git a/internal/pipe/gomod/gomod_test.go b/internal/pipe/gomod/gomod_test.go index 9e711fc27ad..0bf2e2d2333 100644 --- a/internal/pipe/gomod/gomod_test.go +++ b/internal/pipe/gomod/gomod_test.go @@ -29,6 +29,19 @@ func TestRunCustomMod(t *testing.T) { require.Equal(t, "github.com/goreleaser/goreleaser", ctx.ModulePath) } +func TestCustomEnv(t *testing.T) { + bin := filepath.Join(t.TempDir(), "go.bin") + require.NoError(t, os.WriteFile(bin, []byte("#!/bin/sh\nenv | grep -qw FOO=bar"), 0o755)) + ctx := context.New(config.Project{ + GoMod: config.GoMod{ + GoBinary: bin, + Env: []string{"FOO=bar"}, + }, + }) + require.NoError(t, Pipe{}.Default(ctx)) + require.NoError(t, Pipe{}.Run(ctx)) +} + func TestRunOutsideGoModule(t *testing.T) { dir := testlib.Mktmp(t) require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\nfunc main() {println(0)}"), 0o666)) diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index bcc43d49bcb..90e489f2107 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -60,7 +60,8 @@ builds: - feature # Custom environment variables to be set during the builds. - # Default is empty. + # + # Default: `os.Environ()` merged with what you set the root `env` section. env: - CGO_ENABLED=0 diff --git a/www/docs/customization/verifiable_builds.md b/www/docs/customization/verifiable_builds.md index ec77bcf015e..2a7fd559a16 100644 --- a/www/docs/customization/verifiable_builds.md +++ b/www/docs/customization/verifiable_builds.md @@ -18,12 +18,13 @@ gomod: # this setting. # Notice: for this to work your `build.main` must be a package, not a `.go` file. # - # Default is false. + # Default: false. proxy: true # If proxy is true, use these environment variables when running `go mod` # commands (namely, `go mod tidy`). - # Defaults to `os.Environ()`. + # + # Default: `os.Environ()` merged with what you set the root `env` section. env: - GOPROXY=https://proxy.golang.org,direct - GOSUMDB=sum.golang.org @@ -36,7 +37,8 @@ gomod: mod: mod # Which Go binary to use. - # Defaults to `go`. + # + # Default: `go`. gobinary: go1.17 ```