Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check if go.mod has replace directives #4398

Merged
merged 2 commits into from Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions internal/pipe/gomod/gomod_proxy.go
Expand Up @@ -10,14 +10,64 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/logext"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)

// ErrReplaceWithProxy happens when the configuration has gomod.proxy enabled,
// and the go.mod file contains replace directives.
//
// Replaces does not work with proxying, nor with go installs,
// and are made for development only.
var ErrReplaceWithProxy = errors.New("cannot use the go.mod replace directive with go mod proxy enabled")

type CheckGoModPipe struct{}

func (CheckGoModPipe) String() string { return "checking go.mod" }
func (CheckGoModPipe) Skip(ctx *context.Context) bool {
return ctx.ModulePath == "" || !ctx.Config.GoMod.Proxy
}

var replaceRe = regexp.MustCompile("^replace .* => .*$")

// Run the ReplaceCheckPipe.
func (CheckGoModPipe) Run(ctx *context.Context) error {
for i := range ctx.Config.Builds {
build := &ctx.Config.Builds[i]
path := filepath.Join(build.UnproxiedDir, "go.mod")
mod, err := os.ReadFile(path)
if err != nil {
log.Errorf("could not check %q", path)
return nil
}
for _, line := range strings.Split(string(mod), "\n") {
if !replaceRe.MatchString(line) {
continue
}
log.Warnf(
"your %[2]s file has %[1]s directive in it, and go mod proxying is enabled - "+
"this does not work, and you need to either disable it or remove the %[1]s directive",
logext.Keyword("replace"),
logext.Keyword("go.mod"),
)
log.Warnf("the offending line is %s", logext.Keyword(strings.TrimSpace(line)))
if ctx.Snapshot {
// only warn on snapshots
break
}
return ErrReplaceWithProxy
}
}

return nil
}

// ProxyPipe for gomod proxy.
type ProxyPipe struct{}

Expand Down
86 changes: 85 additions & 1 deletion internal/pipe/gomod/gomod_proxy_test.go
Expand Up @@ -3,6 +3,7 @@ package gomod
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
Expand All @@ -15,6 +16,84 @@ import (
"github.com/stretchr/testify/require"
)

func TestString(t *testing.T) {
require.NotEmpty(t, CheckGoModPipe{}.String())
require.NotEmpty(t, ProxyPipe{}.String())
}

func TestCheckGoMod(t *testing.T) {
t.Run("replace on snapshot", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
GoBinary: "go",
},
Builds: []config.Build{
{
ID: "foo",
Goos: []string{runtime.GOOS},
Goarch: []string{runtime.GOARCH},
Main: ".",
Dir: ".",
},
},
}, testctx.Snapshot, withGoReleaserModulePath)

fakeGoModAndSum(t, ctx.ModulePath)
require.NoError(t, exec.Command("go", "mod", "edit", "-replace", "foo=../bar").Run())
require.NoError(t, CheckGoModPipe{}.Run(ctx))
})
t.Run("no go mod", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
GoBinary: "go",
},
Builds: []config.Build{
{
ID: "foo",
Goos: []string{runtime.GOOS},
Goarch: []string{runtime.GOARCH},
Main: ".",
Dir: ".",
},
},
}, withGoReleaserModulePath)

require.NoError(t, CheckGoModPipe{}.Run(ctx))
})
t.Run("replace", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
GoBinary: "go",
},
Builds: []config.Build{
{
ID: "foo",
Goos: []string{runtime.GOOS},
Goarch: []string{runtime.GOARCH},
Main: ".",
Dir: ".",
},
},
}, withGoReleaserModulePath)

fakeGoModAndSum(t, ctx.ModulePath)
require.NoError(t, exec.Command("go", "mod", "edit", "-replace", "foo=../bar").Run())
require.ErrorIs(t, CheckGoModPipe{}.Run(ctx), ErrReplaceWithProxy)
})
}

func TestGoModProxy(t *testing.T) {
t.Run("goreleaser", func(t *testing.T) {
dir := testlib.Mktmp(t)
Expand Down Expand Up @@ -167,7 +246,9 @@ func TestProxyDescription(t *testing.T) {

func TestSkip(t *testing.T) {
t.Run("skip false gomod.proxy", func(t *testing.T) {
require.True(t, ProxyPipe{}.Skip(testctx.New()))
ctx := testctx.New()
require.True(t, ProxyPipe{}.Skip(ctx))
require.True(t, CheckGoModPipe{}.Skip(ctx))
})

t.Run("skip snapshot", func(t *testing.T) {
Expand All @@ -177,6 +258,7 @@ func TestSkip(t *testing.T) {
},
}, withGoReleaserModulePath, testctx.Snapshot)
require.True(t, ProxyPipe{}.Skip(ctx))
require.False(t, CheckGoModPipe{}.Skip(ctx))
})

t.Run("skip not a go module", func(t *testing.T) {
Expand All @@ -186,6 +268,7 @@ func TestSkip(t *testing.T) {
},
}, func(ctx *context.Context) { ctx.ModulePath = "" })
require.True(t, ProxyPipe{}.Skip(ctx))
require.True(t, CheckGoModPipe{}.Skip(ctx))
})

t.Run("dont skip", func(t *testing.T) {
Expand All @@ -195,6 +278,7 @@ func TestSkip(t *testing.T) {
},
}, withGoReleaserModulePath)
require.False(t, ProxyPipe{}.Skip(ctx))
require.False(t, CheckGoModPipe{}.Skip(ctx))
})
}

Expand Down
2 changes: 2 additions & 0 deletions internal/pipeline/pipeline.go
Expand Up @@ -70,6 +70,8 @@ var BuildPipeline = []Piper{
// run prebuild stuff
prebuild.Pipe{},
// proxy gomod if needed
gomod.CheckGoModPipe{},
// proxy gomod if needed
gomod.ProxyPipe{},
// writes the actual config (with defaults et al set) to dist
effectiveconfig.Pipe{},
Expand Down