From 8cd325eb5a8536708c50bd90dfca2a81cd9bad4c Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 29 Mar 2024 10:27:33 -0300 Subject: [PATCH] fix: support dir in gomod (#4729) closes https://github.com/orgs/goreleaser/discussions/4728 --- internal/pipe/gomod/gomod.go | 3 +++ internal/pipe/gomod/gomod_test.go | 23 +++++++++++++++++++++ pkg/config/config.go | 1 + www/docs/customization/verifiable_builds.md | 8 ++++++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/pipe/gomod/gomod.go b/internal/pipe/gomod/gomod.go index 4fb99bfb9..dbbae8a80 100644 --- a/internal/pipe/gomod/gomod.go +++ b/internal/pipe/gomod/gomod.go @@ -36,6 +36,9 @@ func (Pipe) Run(ctx *context.Context) error { } cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...) cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...) + if dir := ctx.Config.GoMod.Dir; dir != "" { + cmd.Dir = dir + } out, err := cmd.CombinedOutput() result := strings.TrimSpace(string(out)) if strings.HasPrefix(result, goPreModulesError) { diff --git a/internal/pipe/gomod/gomod_test.go b/internal/pipe/gomod/gomod_test.go index 5587530bc..c43973a73 100644 --- a/internal/pipe/gomod/gomod_test.go +++ b/internal/pipe/gomod/gomod_test.go @@ -75,6 +75,29 @@ func TestCustomEnv(t *testing.T) { require.NoError(t, Pipe{}.Run(ctx)) } +func TestRunCustomDir(t *testing.T) { + dir := testlib.Mktmp(t) + require.NoError(t, os.MkdirAll(filepath.Join(dir, "src"), 0o755)) + require.NoError(t, os.WriteFile( + filepath.Join(dir, "src/main.go"), + []byte("package main\nfunc main() {println(0)}"), + 0o666, + )) + require.NoError(t, os.WriteFile( + filepath.Join(dir, "src/go.mod"), + []byte("module foo"), + 0o666, + )) + ctx := testctx.NewWithCfg(config.Project{ + GoMod: config.GoMod{ + Dir: filepath.Join(dir, "src"), + }, + }) + require.NoError(t, Pipe{}.Default(ctx)) + require.NoError(t, Pipe{}.Run(ctx)) + require.Equal(t, "foo", ctx.ModulePath) +} + func TestRunOutsideGoModule(t *testing.T) { dir := testlib.Mktmp(t) require.NoError(t, os.WriteFile( diff --git a/pkg/config/config.go b/pkg/config/config.go index b1c214334..06a7e0e96 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1247,6 +1247,7 @@ type GoMod struct { Env []string `yaml:"env,omitempty" json:"env,omitempty"` GoBinary string `yaml:"gobinary,omitempty" json:"gobinary,omitempty"` Mod string `yaml:"mod,omitempty" json:"mod,omitempty"` + Dir string `yaml:"dir,omitempty" json:"dir,omitempty"` } type Announce struct { diff --git a/www/docs/customization/verifiable_builds.md b/www/docs/customization/verifiable_builds.md index a2e7c9fc9..47d097a17 100644 --- a/www/docs/customization/verifiable_builds.md +++ b/www/docs/customization/verifiable_builds.md @@ -37,6 +37,12 @@ gomod: # # Default: `go`. gobinary: go1.17 + + # Directory in which the go.mod file is. + # + # Default: '' + # Since: v1.25 + dir: ./src ``` !!! tip @@ -49,7 +55,7 @@ gomod: VCS Info will not be embedded in the binary, as in practice it is not being built from the source, but from the Go Mod Proxy. -!!! warning +!!! warning If you have a `go.work` file, make sure to run `go work sync`, so the main module (`.`) is the first line inside the `use` block.