1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-13 01:30:50 +02:00

fix: support dir in gomod (#4729)

closes https://github.com/orgs/goreleaser/discussions/4728
This commit is contained in:
Carlos Alexandro Becker
2024-03-29 10:27:33 -03:00
committed by GitHub
parent 46b53353fc
commit 8cd325eb5a
4 changed files with 34 additions and 1 deletions

View File

@ -36,6 +36,9 @@ func (Pipe) Run(ctx *context.Context) error {
} }
cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...) cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...)
cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...) cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...)
if dir := ctx.Config.GoMod.Dir; dir != "" {
cmd.Dir = dir
}
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
result := strings.TrimSpace(string(out)) result := strings.TrimSpace(string(out))
if strings.HasPrefix(result, goPreModulesError) { if strings.HasPrefix(result, goPreModulesError) {

View File

@ -75,6 +75,29 @@ func TestCustomEnv(t *testing.T) {
require.NoError(t, Pipe{}.Run(ctx)) 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) { func TestRunOutsideGoModule(t *testing.T) {
dir := testlib.Mktmp(t) dir := testlib.Mktmp(t)
require.NoError(t, os.WriteFile( require.NoError(t, os.WriteFile(

View File

@ -1247,6 +1247,7 @@ type GoMod struct {
Env []string `yaml:"env,omitempty" json:"env,omitempty"` Env []string `yaml:"env,omitempty" json:"env,omitempty"`
GoBinary string `yaml:"gobinary,omitempty" json:"gobinary,omitempty"` GoBinary string `yaml:"gobinary,omitempty" json:"gobinary,omitempty"`
Mod string `yaml:"mod,omitempty" json:"mod,omitempty"` Mod string `yaml:"mod,omitempty" json:"mod,omitempty"`
Dir string `yaml:"dir,omitempty" json:"dir,omitempty"`
} }
type Announce struct { type Announce struct {

View File

@ -37,6 +37,12 @@ gomod:
# #
# Default: `go`. # Default: `go`.
gobinary: go1.17 gobinary: go1.17
# Directory in which the go.mod file is.
#
# Default: ''
# Since: v1.25
dir: ./src
``` ```
!!! tip !!! tip
@ -49,7 +55,7 @@ gomod:
VCS Info will not be embedded in the binary, as in practice it is not being 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. 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 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. module (`.`) is the first line inside the `use` block.