Skip to content

Commit

Permalink
feat: initial proxy build support (#2129)
Browse files Browse the repository at this point in the history
* feat: allow to use ModulePath on templates

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

* feat: initial proxy build support

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

* fix: build

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

* fix: main check

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

* fix: make it more flexible

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

* fix: small improvements

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

* fix: copy go.sum

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

* fix: root mod proxy

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

* fix: test

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

* fix: snapshots

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

* fix: lint

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

* fix: proxy main pkg

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

* fix: environment variables

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

* test: added some tests to go mod proxy feature

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

* fix: improve test

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

* fix: linte

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

* fix: goreleaser.yml

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

* fix: simplify tests

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

* test: test build

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

* fix: revert unwanted changes

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

* fix: allow to run when no mod.suym

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

* docs: example

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

* fix: not a go module on go 1.15

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

* docs: improve docs as per comments

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Mar 31, 2021
1 parent 90f2ba6 commit 8306b94
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ before:
hooks:
- go mod tidy
- ./scripts/completions.sh
gomod:
proxy: true
builds:
- env:
- CGO_ENABLED=0
Expand Down
6 changes: 4 additions & 2 deletions internal/builders/golang/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ 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 err := checkMain(build); err != nil {
return err
if !ctx.Config.GoMod.Proxy {
if err := checkMain(build); err != nil {
return err
}
}
target, err := newBuildTarget(options.Target)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions internal/builders/golang/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -548,6 +549,47 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
})
}

func TestRunPipeWithProxiedRepo(t *testing.T) {
folder := testlib.Mktmp(t)
proxied := filepath.Join(folder, "dist/proxy/default")
require.NoError(t, os.MkdirAll(proxied, 0o750))
require.NoError(t, ioutil.WriteFile(
filepath.Join(proxied, "main.go"),
[]byte("// +build: main\npackage main\nimport github.com/goreleaser/goreleaser"),
0o666,
))
require.NoError(t, ioutil.WriteFile(
filepath.Join(proxied, "go.mod"),
[]byte("module foo\nrequire github.com/goreleaser/goreleaser v0.161.1"),
0o666,
))
cmd := exec.Command("go", "mod", "download")
cmd.Dir = proxied
require.NoError(t, cmd.Run())
config := config.Project{
GoMod: config.GoMod{
Proxy: true,
},
Builds: []config.Build{
{
Binary: "foo",
Hooks: config.HookConfig{},
Main: "github.com/goreleaser/goreleaser",
Dir: proxied,
Targets: []string{
runtimeTarget,
},
GoBinary: "go",
},
},
}
ctx := context.New(config)

require.NoError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
}))
}

func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
folder := testlib.Mktmp(t)
require.NoError(t, ioutil.WriteFile(
Expand Down
162 changes: 157 additions & 5 deletions internal/pipe/gomod/gomod.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
// Package gomod provides go modules utilities, such as template variables and the ability to proxy the module from
// proxy.golang.org.
package gomod

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)

const (
go115NotAGoModuleError = "go list -m: not using modules"
go116NotAGoModuleError = "command-line-arguments"
)

// Pipe for env.
type Pipe struct{}

func (Pipe) String() string {
return "loading go mod information"
}

const (
go115NotAGoModuleError = "go list -m: not using modules"
go116NotAGoModuleError = "command-line-arguments"
)
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.GoMod.GoBinary == "" {
ctx.Config.GoMod.GoBinary = "go"
}
return nil
}

// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
out, err := exec.CommandContext(ctx, "go", "list", "-m").CombinedOutput()
out, err := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, "list", "-m").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to get module path: %w: %s", err, string(out))
}
Expand All @@ -35,5 +53,139 @@ func (Pipe) Run(ctx *context.Context) error {

ctx.ModulePath = result

if !ctx.Config.GoMod.Proxy {
return pipe.Skip("gomod.proxy is disabled")
}

if ctx.Snapshot {
return pipe.ErrSnapshotEnabled
}

return setupProxy(ctx)
}

func setupProxy(ctx *context.Context) error {
for i := range ctx.Config.Builds {
build := &ctx.Config.Builds[i]
if err := proxyBuild(ctx, build); err != nil {
return err
}
}

return nil
}

const goModTpl = `
module {{ .BuildID }}
require {{ .ModulePath }} {{ .Tag }}
`

const mainGoTpl = `
// +build main
package main
import _ "{{ .Main }}"
`

// ErrProxy happens when something goes wrong while proxying the current go module.
type ErrProxy struct {
err error
details string
}

func newErrProxy(err error) error {
return ErrProxy{
err: err,
}
}

func newDetailedErrProxy(err error, details string) error {
return ErrProxy{
err: err,
details: details,
}
}

func (e ErrProxy) Error() string {
out := fmt.Sprintf("failed to proxy module: %v", e.err)
if e.details != "" {
return fmt.Sprintf("%s: %s", out, e.details)
}
return out
}

func (e ErrProxy) Unwrap() error {
return e.err
}

func proxyBuild(ctx *context.Context, build *config.Build) error {
mainPackage := path.Join(ctx.ModulePath, build.Main)
template := tmpl.New(ctx).WithExtraFields(tmpl.Fields{
"Main": mainPackage,
"BuildID": build.ID,
})

log.Infof("proxying %s@%s to build %s", ctx.ModulePath, ctx.Git.CurrentTag, mainPackage)

mod, err := template.Apply(goModTpl)
if err != nil {
return newErrProxy(err)
}

main, err := template.Apply(mainGoTpl)
if err != nil {
return newErrProxy(err)
}

dir := filepath.Join(ctx.Config.Dist, "proxy", build.ID)

log.Debugf("creating needed files")

if err := os.MkdirAll(dir, 0o755); err != nil {
return newErrProxy(err)
}

if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(main), 0o666); err != nil {
return newErrProxy(err)
}

if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(mod), 0o666); err != nil {
return newErrProxy(err)
}

if err := copyGoSum("go.sum", filepath.Join(dir, "go.sum")); err != nil {
return newErrProxy(err)
}

log.Debugf("tidying")
cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, "mod", "tidy")
cmd.Dir = dir
cmd.Env = append(ctx.Config.GoMod.Env, os.Environ()...)
if out, err := cmd.CombinedOutput(); err != nil {
return newDetailedErrProxy(err, string(out))
}

build.Main = mainPackage
build.Dir = dir
return nil
}

func copyGoSum(src, dst string) error {
r, err := os.OpenFile(src, os.O_RDONLY, 0o666)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}

w, err := os.Create(dst)
if err != nil {
return err
}
defer w.Close()

_, err = io.Copy(w, r)
return err
}

1 comment on commit 8306b94

@vercel
Copy link

@vercel vercel bot commented on 8306b94 Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.