diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go index 2eb817c3f..064ee8e6d 100644 --- a/internal/pipe/build/build.go +++ b/internal/pipe/build/build.go @@ -3,7 +3,9 @@ package build import ( + "bytes" "fmt" + "io" "os" "os/exec" "path/filepath" @@ -11,6 +13,7 @@ import ( "github.com/apex/log" "github.com/goreleaser/goreleaser/internal/ids" + "github.com/goreleaser/goreleaser/internal/logext" "github.com/goreleaser/goreleaser/internal/semerrgroup" "github.com/goreleaser/goreleaser/internal/tmpl" builders "github.com/goreleaser/goreleaser/pkg/build" @@ -215,15 +218,18 @@ func extFor(target string, flags config.FlagArray) string { func run(ctx *context.Context, dir string, command, env []string) error { /* #nosec */ var cmd = exec.CommandContext(ctx, command[0], command[1:]...) - var log = log.WithField("env", env).WithField("cmd", command) + var entry = log.WithField("cmd", command) cmd.Env = env + var b bytes.Buffer + cmd.Stderr = io.MultiWriter(logext.NewErrWriter(entry), &b) + cmd.Stdout = io.MultiWriter(logext.NewWriter(entry), &b) if dir != "" { cmd.Dir = dir } - log.Debug("running") - if out, err := cmd.CombinedOutput(); err != nil { - log.WithError(err).Debug("failed") - return errors.Wrapf(err, "%q", string(out)) + entry.WithField("env", env).Debug("running") + if err := cmd.Run(); err != nil { + entry.WithError(err).Debug("failed") + return errors.Wrapf(err, "%q", b.String()) } return nil } diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go index bae797145..14359ee44 100644 --- a/internal/pipe/build/build_test.go +++ b/internal/pipe/build/build_test.go @@ -731,3 +731,28 @@ func TestHookInvalidShelCommand(t *testing.T) { }, })) } + +func TestRunHookFailWithLogs(t *testing.T) { + folder, back := testlib.Mktmp(t) + defer back() + var config = config.Project{ + Dist: folder, + Builds: []config.Build{ + { + Lang: "fakeFail", + Binary: "testing", + Flags: []string{"-v"}, + Hooks: config.HookConfig{ + Pre: []config.BuildHook{ + {Cmd: "sh -c 'echo foo; exit 1'"}, + }, + }, + Targets: []string{"whatever"}, + }, + }, + } + var ctx = context.New(config) + ctx.Git.CurrentTag = "2.4.5" + assert.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: \"foo\\n\": exit status 1") + assert.Empty(t, ctx.Artifacts.List()) +}