1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

fix: handle build.main being a go file when using gomod.proxy (#2173)

* fix: handle build.main being a go file when using gomod.proxy

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: guess pkg

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2021-04-19 08:54:55 -03:00 committed by GitHub
parent 271bd5a635
commit a9efdcab4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View File

@ -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,

View File

@ -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) {

View File

@ -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:

View File

@ -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