1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00
Marcin Białoń bb175015c7
feat: skip gomod pipe when Go does not support modules (#4280)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

The gomod pipe will skip if the Go version does not support modules.

<!-- Why is this change being made? -->

I tried to use goreleaser to build a project with Go v1.8.7 and it
failed on `loading go mod information` step. The Go version used doesn't
support `go list -m`.

The build failed with the following error:
```
  • loading go mod information
  ⨯ release failed after 0s                          error=failed to get module path: exit status 2: flag provided but not defined: -m
usage: list [-e] [-f format] [-json] [build flags] [packages]

List lists the packages named by the import paths, one per line.
...
```

<!-- # Provide links to any relevant tickets, URLs or other resources
-->

...
2023-09-07 15:01:57 -03:00

54 lines
1.3 KiB
Go

package gomod
import (
"fmt"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/pkg/context"
)
const (
goPreModulesError = "flag provided but not defined: -m"
go115NotAGoModuleError = "go list -m: not using modules"
go116NotAGoModuleError = "command-line-arguments"
)
// Pipe for gomod.
type Pipe struct{}
func (Pipe) String() string { return "loading go mod information" }
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.GoMod.GoBinary == "" {
ctx.Config.GoMod.GoBinary = "go"
}
return nil
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
flags := []string{"list", "-m"}
if ctx.Config.GoMod.Mod != "" {
flags = append(flags, "-mod="+ctx.Config.GoMod.Mod)
}
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 strings.HasPrefix(result, goPreModulesError) {
return pipe.Skip("go version does not support modules")
}
if result == go115NotAGoModuleError || result == go116NotAGoModuleError {
return pipe.Skip("not a go module")
}
if err != nil {
return fmt.Errorf("failed to get module path: %w: %s", err, string(out))
}
ctx.ModulePath = result
return nil
}