Repository: goreleaser/goreleaser
Pull request #2390 (2021-08-11 12:31:48)

fix: main check when using gomod.proxy 
we can't check for the `main` function in the actual proxied module, but we can at least check the code which, in theory, is the module being proxied.

this should avoid bad releases as I did for fork-cleaner, which was basically empty.

What happens now is that the `gomod` pipe overrides `Main` and `Dir` as before, but set the original values to `UnproxiedMain` and `UnproxiedDir`, and then the `build` pipe checks the main on the unproxied properties when they exist.

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/95teSTtcSgRqVx4aepWseYrGsrh7](https://vercel.com/goreleaser/goreleaser/95teSTtcSgRqVx4aepWseYrGsrh7)  
✅ Preview: [https://goreleaser-git-proxy-main-check-goreleaser.vercel.app](https://goreleaser-git-proxy-main-check-goreleaser.vercel.app)


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2390](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (1471bd6) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/1d90ecb8c3641677de95935afc401721e8a6f668?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (1d90ecb) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2390/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2390      +/-   ##
==========================================
+ Coverage   82.91%   82.94%   +0.02%     
==========================================
  Files          83       83              
  Lines        6059     6067       +8     
==========================================
+ Hits         5024     5032       +8     
  Misses        887      887              
  Partials      148      148              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2390/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `90.72% <ø> (ø)` | |
| [internal/builders/golang/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2390/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvYnVpbGRlcnMvZ29sYW5nL2J1aWxkLmdv) | `92.89% <100.00%> (+0.20%)` | :arrow_up: |
| [internal/pipe/gomod/gomod.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2390/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9nb21vZC9nb21vZC5nbw==) | `78.44% <100.00%> (+0.37%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [1d90ecb...1471bd6](https://codecov.io/gh/goreleaser/goreleaser/pull/2390?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).


Patch:
diff --git a/go.mod b/go.mod
index 504f86da..7a1835b3 100644
--- a/go.mod
+++ b/go.mod
@@ -21,7 +21,7 @@ require (
 	github.com/spf13/cobra v1.2.1
 	github.com/stretchr/testify v1.7.0
 	github.com/ulikunitz/xz v0.5.10
-	github.com/xanzy/go-gitlab v0.50.2
+	github.com/xanzy/go-gitlab v0.50.3
 	gocloud.dev v0.23.0
 	golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
 	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
diff --git a/go.sum b/go.sum
index dceaa797..e0574512 100644
--- a/go.sum
+++ b/go.sum
@@ -580,8 +580,8 @@ github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oW
 github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
 github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
 github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
-github.com/xanzy/go-gitlab v0.50.2 h1:Qm/um2Jryuqusc6VmN7iZYVTQVzNynzSiuMJDnCU1wE=
-github.com/xanzy/go-gitlab v0.50.2/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE=
+github.com/xanzy/go-gitlab v0.50.3 h1:M7ncgNhCN4jaFNyXxarJhCLa9Qi6fdmCxFFhMTQPZiY=
+github.com/xanzy/go-gitlab v0.50.3/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE=
 github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
 github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
 github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
diff --git a/internal/builders/golang/build.go b/internal/builders/golang/build.go
index e322d17a..706fc9a5 100644
--- a/internal/builders/golang/build.go
+++ b/internal/builders/golang/build.go
@@ -70,10 +70,8 @@ func (*Builder) WithDefaults(build config.Build) (config.Build, error) {
 
 // Build builds a golang build.
 func (*Builder) Build(ctx *context.Context, build config.Build, options api.Options) error {
-	if !ctx.Config.GoMod.Proxy {
-		if err := checkMain(build); err != nil {
-			return err
-		}
+	if err := checkMain(build); err != nil {
+		return err
 	}
 	target, err := newBuildTarget(options.Target)
 	if err != nil {
@@ -233,11 +231,19 @@ func (b buildTarget) Env() []string {
 
 func checkMain(build config.Build) error {
 	main := build.Main
+	if build.UnproxiedMain != "" {
+		main = build.UnproxiedMain
+	}
+	dir := build.Dir
+	if build.UnproxiedDir != "" {
+		dir = build.UnproxiedDir
+	}
+
 	if main == "" {
 		main = "."
 	}
-	if build.Dir != "" {
-		main = filepath.Join(build.Dir, main)
+	if dir != "" {
+		main = filepath.Join(dir, main)
 	}
 	stat, ferr := os.Stat(main)
 	if ferr != nil {
diff --git a/internal/pipe/gomod/gomod.go b/internal/pipe/gomod/gomod.go
index c2b33b99..e56e4194 100644
--- a/internal/pipe/gomod/gomod.go
+++ b/internal/pipe/gomod/gomod.go
@@ -170,6 +170,8 @@ func proxyBuild(ctx *context.Context, build *config.Build) error {
 		return newDetailedErrProxy(err, string(out))
 	}
 
+	build.UnproxiedMain = build.Main
+	build.UnproxiedDir = build.Dir
 	build.Main = mainPackage
 	build.Dir = dir
 	return nil
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 6050d7c7..0ad1a015 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -207,6 +207,8 @@ type Build struct {
 	Skip            bool           `yaml:",omitempty"`
 	GoBinary        string         `yaml:",omitempty"`
 	NoUniqueDistDir bool           `yaml:"no_unique_dist_dir,omitempty"`
+	UnproxiedMain   string         `yaml:"-"` // used by gomod.proxy
+	UnproxiedDir    string         `yaml:"-"` // used by gomod.proxy
 }
 
 type HookConfig struct {


Test patch:
diff --git a/internal/builders/golang/build_test.go b/internal/builders/golang/build_test.go
index e356e2f3..d394acd0 100644
--- a/internal/builders/golang/build_test.go
+++ b/internal/builders/golang/build_test.go
@@ -508,49 +508,71 @@ func TestRunInvalidFlags(t *testing.T) {
 }
 
 func TestRunPipeWithoutMainFunc(t *testing.T) {
-	folder := testlib.Mktmp(t)
-	writeMainWithoutMainFunc(t, folder)
-	config := config.Project{
-		Builds: []config.Build{
-			{
-				Binary: "no-main",
-				Hooks:  config.HookConfig{},
-				Targets: []string{
-					runtimeTarget,
+	newCtx := func(t *testing.T) *context.Context {
+		t.Helper()
+		folder := testlib.Mktmp(t)
+		writeMainWithoutMainFunc(t, folder)
+		config := config.Project{
+			Builds: []config.Build{
+				{
+					Binary: "no-main",
+					Hooks:  config.HookConfig{},
+					Targets: []string{
+						runtimeTarget,
+					},
 				},
 			},
-		},
+		}
+		ctx := context.New(config)
+		ctx.Git.CurrentTag = "5.6.7"
+		return ctx
 	}
-	ctx := context.New(config)
-	ctx.Git.CurrentTag = "5.6.7"
 	t.Run("empty", func(t *testing.T) {
+		ctx := newCtx(t)
 		ctx.Config.Builds[0].Main = ""
 		require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
 			Target: runtimeTarget,
 		}), `build for no-main does not contain a main function`)
 	})
 	t.Run("not main.go", func(t *testing.T) {
+		ctx := newCtx(t)
 		ctx.Config.Builds[0].Main = "foo.go"
 		require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
 			Target: runtimeTarget,
 		}), `couldn't find main file: stat foo.go: no such file or directory`)
 	})
 	t.Run("glob", func(t *testing.T) {
+		ctx := newCtx(t)
 		ctx.Config.Builds[0].Main = "."
 		require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
 			Target: runtimeTarget,
 		}), `build for no-main does not contain a main function`)
 	})
 	t.Run("fixed main.go", func(t *testing.T) {
+		ctx := newCtx(t)
 		ctx.Config.Builds[0].Main = "main.go"
 		require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
 			Target: runtimeTarget,
 		}), `build for no-main does not contain a main function`)
 	})
+	t.Run("using gomod.proxy", func(t *testing.T) {
+		ctx := newCtx(t)
+		ctx.Config.GoMod.Proxy = true
+		ctx.Config.Builds[0].Dir = "dist/proxy/test"
+		ctx.Config.Builds[0].Main = "github.com/caarlos0/test"
+		ctx.Config.Builds[0].UnproxiedDir = "."
+		ctx.Config.Builds[0].UnproxiedMain = "."
+		require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
+			Target: runtimeTarget,
+		}), `build for no-main does not contain a main function`)
+	})
 }
 
 func TestRunPipeWithProxiedRepo(t *testing.T) {
 	folder := testlib.Mktmp(t)
+	out, err := exec.Command("git", "clone", "https://github.com/goreleaser/goreleaser", "-b", "v0.161.1", "--depth=1", ".").CombinedOutput()
+	require.NoError(t, err, string(out))
+
 	proxied := filepath.Join(folder, "dist/proxy/default")
 	require.NoError(t, os.MkdirAll(proxied, 0o750))
 	require.NoError(t, os.WriteFile(
@@ -567,6 +589,7 @@ import _ "github.com/goreleaser/goreleaser"
 		[]byte("module foo\nrequire github.com/goreleaser/goreleaser v0.161.1"),
 		0o666,
 	))
+
 	cmd := exec.Command("go", "mod", "tidy")
 	cmd.Dir = proxied
 	require.NoError(t, cmd.Run())
@@ -577,9 +600,11 @@ import _ "github.com/goreleaser/goreleaser"
 		},
 		Builds: []config.Build{
 			{
-				Binary: "foo",
-				Main:   "github.com/goreleaser/goreleaser",
-				Dir:    proxied,
+				Binary:        "foo",
+				Main:          "github.com/goreleaser/goreleaser",
+				Dir:           proxied,
+				UnproxiedMain: ".",
+				UnproxiedDir:  ".",
 				Targets: []string{
 					runtimeTarget,
 				},
diff --git a/internal/pipe/gomod/gomod_test.go b/internal/pipe/gomod/gomod_test.go
index d8b9de5e..aa31a169 100644
--- a/internal/pipe/gomod/gomod_test.go
+++ b/internal/pipe/gomod/gomod_test.go
@@ -70,6 +70,7 @@ func TestGoModProxy(t *testing.T) {
 					Goos:   []string{runtime.GOOS},
 					Goarch: []string{runtime.GOARCH},
 					Main:   ".",
+					Dir:    ".",
 				},
 			},
 		})
@@ -83,7 +84,9 @@ func TestGoModProxy(t *testing.T) {
 		requireGoMod(t, mod, ctx.Git.CurrentTag)
 		requireMainGo(t, mod)
 		require.Equal(t, mod, ctx.Config.Builds[0].Main)
+		require.Equal(t, ".", ctx.Config.Builds[0].UnproxiedMain)
 		require.Equal(t, filepath.Join(dist, "proxy", "foo"), ctx.Config.Builds[0].Dir)
+		require.Equal(t, ".", ctx.Config.Builds[0].UnproxiedDir)
 		require.Equal(t, mod, ctx.ModulePath)
 	})