diff --git a/CHANGELOG.md b/CHANGELOG.md index 21434137..ecc11ef5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/execext/exec.go b/internal/execext/exec.go index dae3eb6c..6ac9b1ad 100644 --- a/internal/execext/exec.go +++ b/internal/execext/exec.go @@ -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 { @@ -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) } diff --git a/task_test.go b/task_test.go index 31710445..f86d2741 100644 --- a/task_test.go +++ b/task_test.go @@ -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`) +} diff --git a/testdata/exit_immediately/Taskfile.yml b/testdata/exit_immediately/Taskfile.yml new file mode 100644 index 00000000..247165c9 --- /dev/null +++ b/testdata/exit_immediately/Taskfile.yml @@ -0,0 +1,6 @@ +version: '3' + +tasks: + default: | + this_should_fail + echo "This shoudn't be print"