diff --git a/task.go b/task.go index b183014271..e1fbe5d460 100644 --- a/task.go +++ b/task.go @@ -176,6 +176,10 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error { return nil } + if err := e.areTaskRequiredVarsSet(t, call); err != nil { + return err + } + t, err = e.CompiledTask(call) if err != nil { return err @@ -202,10 +206,6 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error { return err } - if err := e.areTaskRequiredVarsSet(t, call); err != nil { - return err - } - preCondMet, err := e.areTaskPreconditionsMet(ctx, t) if err != nil { return err diff --git a/task_test.go b/task_test.go index 3cb02010ae..f8af6399d4 100644 --- a/task_test.go +++ b/task_test.go @@ -199,6 +199,10 @@ func TestRequires(t *testing.T) { vars.Set("foo", ast.Var{Value: "one"}) require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "validation-var", Vars: vars})) buff.Reset() + + require.NoError(t, e.Setup()) + require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "require-before-compile"}), "task: Task \"require-before-compile\" cancelled because it is missing required variables: MY_VAR") + buff.Reset() } func TestSpecialVars(t *testing.T) { diff --git a/testdata/requires/Taskfile.yml b/testdata/requires/Taskfile.yml index 22733ca3d5..83d4110f1b 100644 --- a/testdata/requires/Taskfile.yml +++ b/testdata/requires/Taskfile.yml @@ -16,3 +16,12 @@ tasks: vars: - name: foo enum: ['one', 'two'] + + + require-before-compile: + requires: + vars: [ MY_VAR ] + cmd: | + {{range .MY_VAR | splitList " " }} + echo {{.}} + {{end}}