Repository: goreleaser/goreleaser
Pull request #2173 (2021-04-18 23:22:36)

fix: handle build.main being a go file when using gomod.proxy
refs https://github.com/go-task/task/pull/462#issuecomment-821886239

having `build.main` point to a `main.go` file, for example, instead of package would break things. 

trying to guess the package by removing the last element of the path if it ends with `.go`, also throwing a warning when that happens.

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/3qtDhixDsnMxE68JbZbf4JZ9gun8](https://vercel.com/goreleaser/goreleaser/3qtDhixDsnMxE68JbZbf4JZ9gun8)  
✅ Preview: *Failed*


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

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2173/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/2173?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2173      +/-   ##
==========================================
+ Coverage   82.64%   82.83%   +0.19%     
==========================================
  Files          75       75              
  Lines        4113     4118       +5     
==========================================
+ Hits         3399     3411      +12     
+ Misses        580      573       -7     
  Partials      134      134              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2173?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/pipe/gomod/gomod.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2173/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9nb21vZC9nb21vZC5nbw==) | `88.31% <100.00%> (+10.53%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2173?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/2173?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [e4e242f...c37866d](https://codecov.io/gh/goreleaser/goreleaser/pull/2173?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/internal/pipe/gomod/gomod.go b/internal/pipe/gomod/gomod.go
index ac1a3417..5562afa3 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/www/docs/customization/build.md b/www/docs/customization/build.md
index 22f147db..caa10756 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 d3d6715c..e2dcb58a 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


Test patch:
diff --git a/internal/pipe/gomod/gomod_test.go b/internal/pipe/gomod/gomod_test.go
index dfc12de1..d8b9de5e 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) {