diff --git a/internal/pipe/gomod/gomod.go b/internal/pipe/gomod/gomod.go index ac1a34178..5562afa3d 100644 --- a/internal/pipe/gomod/gomod.go +++ b/internal/pipe/gomod/gomod.go @@ -120,6 +120,11 @@ func (e ErrProxy) Unwrap() error { func proxyBuild(ctx *context.Context, build *config.Build) error { mainPackage := path.Join(ctx.ModulePath, build.Main) + if strings.HasSuffix(build.Main, ".go") { + pkg := path.Dir(build.Main) + log.Warnf("guessing package of '%s' to be '%s', if this is incorrect, setup 'build.%s.main' to be the correct package", build.Main, pkg, build.ID) + mainPackage = path.Join(ctx.ModulePath, pkg) + } template := tmpl.New(ctx).WithExtraFields(tmpl.Fields{ "Main": mainPackage, "BuildID": build.ID, diff --git a/internal/pipe/gomod/gomod_test.go b/internal/pipe/gomod/gomod_test.go index dfc12de1d..d8b9de5ed 100644 --- a/internal/pipe/gomod/gomod_test.go +++ b/internal/pipe/gomod/gomod_test.go @@ -184,6 +184,37 @@ func TestGoModProxy(t *testing.T) { }) } }) + + t.Run("goreleaser with main.go", func(t *testing.T) { + dir := testlib.Mktmp(t) + dist := filepath.Join(dir, "dist") + ctx := context.New(config.Project{ + Dist: dist, + GoMod: config.GoMod{ + Proxy: true, + }, + Builds: []config.Build{ + { + ID: "foo", + Goos: []string{runtime.GOOS}, + Goarch: []string{runtime.GOARCH}, + Main: "main.go", + }, + }, + }) + ctx.Git.CurrentTag = "v0.161.1" + + mod := "github.com/goreleaser/goreleaser" + + fakeGoModAndSum(t, mod) + require.NoError(t, Pipe{}.Default(ctx)) + require.NoError(t, Pipe{}.Run(ctx)) + requireGoMod(t, mod, ctx.Git.CurrentTag) + requireMainGo(t, mod) + require.Equal(t, mod, ctx.Config.Builds[0].Main) + require.Equal(t, filepath.Join(dist, "proxy", "foo"), ctx.Config.Builds[0].Dir) + require.Equal(t, mod, ctx.ModulePath) + }) } func requireGoMod(tb testing.TB, module, version string) { diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index 22f147dbe..caa107568 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -24,8 +24,10 @@ builds: dir: go # Path to main.go file or main package. + # Notice: when used with `gomod.proxy`, this must be a package. + # # Default is `.`. - main: ./cmd/main.go + main: ./cmd/my-app # Binary name. # Can be a path (e.g. `bin/app`) to wrap the binary in a directory. @@ -134,7 +136,7 @@ Here is an example with multiple binaries: ```yaml # .goreleaser.yml builds: - - main: ./cmd/cli/cli.go + - main: ./cmd/cli id: "cli" binary: cli goos: @@ -142,7 +144,7 @@ builds: - darwin - windows - - main: ./cmd/worker/worker.go + - main: ./cmd/worker id: "worker" binary: worker goos: @@ -150,7 +152,7 @@ builds: - darwin - windows - - main: ./cmd/tracker/tracker.go + - main: ./cmd/tracker id: "tracker" binary: tracker goos: diff --git a/www/docs/customization/gomod.md b/www/docs/customization/gomod.md index d3d6715ce..e2dcb58a4 100644 --- a/www/docs/customization/gomod.md +++ b/www/docs/customization/gomod.md @@ -15,6 +15,7 @@ Configuration options available are described bellow. gomod: # Proxy a module from proxy.golang.org, making the builds verifiable. # This will only be effective if running against a tag. Snapshots will ignore this setting. + # PS: for this to work you `build.main` must be a package, not a `.go` file. # # Default is false. proxy: true