Skip to content

Commit

Permalink
Run "set -e" automatically for every command
Browse files Browse the repository at this point in the history
Without this, multiline command strings won't always exit when they fail.

Closes #403
  • Loading branch information
andreynering committed Dec 27, 2020
1 parent 16fad60 commit ac8e344
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

- `set -e` is now automatically set on every command. This was done to fix an
issue where multiline string commands wouldn't really fail unless the
sentence was in the last line
([#403](https://github.com/go-task/task/issues/403)).

## v3.0.1

- Do not error if a specified dotenv file does not exist
Expand Down
13 changes: 13 additions & 0 deletions internal/execext/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ type RunCommandOptions struct {
var (
// ErrNilOptions is returned when a nil options is given
ErrNilOptions = errors.New("execext: nil options given")

setMinusE *syntax.File
)

func init() {
var err error
setMinusE, err = syntax.NewParser().Parse(strings.NewReader("set -e"), "")
if err != nil {
panic(err)
}
}

// RunCommand runs a shell command
func RunCommand(ctx context.Context, opts *RunCommandOptions) error {
if opts == nil {
Expand All @@ -54,6 +64,9 @@ func RunCommand(ctx context.Context, opts *RunCommandOptions) error {
if err != nil {
return err
}
if err = r.Run(ctx, setMinusE); err != nil {
return err
}
return r.Run(ctx, p)
}

Expand Down
16 changes: 16 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,19 @@ func TestDotenvShouldAllowMissingEnv(t *testing.T) {
}
tt.Run(t)
}

func TestExitImmediately(t *testing.T) {
const dir = "testdata/exit_immediately"

var buff bytes.Buffer
e := task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: true,
}
assert.NoError(t, e.Setup())

assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "default"}))
assert.Contains(t, buff.String(), `"this_should_fail": executable file not found in $PATH`)
}
6 changes: 6 additions & 0 deletions testdata/exit_immediately/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'

tasks:
default: |
this_should_fail
echo "This shoudn't be print"

0 comments on commit ac8e344

Please sign in to comment.