Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,10 @@ func TestIf(t *testing.T) {

// For loop with if
{name: "if-in-for-loop", task: "if-in-for-loop", verbose: true},

// Task-level if with dynamic variable
{name: "task-if-dynamic-true", task: "task-if-dynamic-true"},
{name: "task-if-dynamic-false", task: "task-if-dynamic-false", verbose: true},
}

for _, test := range tests {
Expand Down
21 changes: 15 additions & 6 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
return nil
}

// Check required vars early (before template compilation) if we can't prompt.
// This gives a clear "missing required variables" error instead of a template error.
if !e.canPrompt() {
if err := e.areTaskRequiredVarsSet(t); err != nil {
return err
}
}

t, err = e.CompiledTask(call)
if err != nil {
return err
}

// Check if condition after CompiledTask so dynamic variables are resolved
if strings.TrimSpace(t.If) != "" {
if err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: t.If,
Expand All @@ -159,7 +173,7 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
}
}

// Prompt for missing required vars (just-in-time for sequential task calls)
// Prompt for missing required vars after if check (avoid prompting if task won't run)
prompted, err := e.promptTaskVars(t, call)
if err != nil {
return err
Expand All @@ -176,11 +190,6 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
return err
}

t, err = e.CompiledTask(call)
if err != nil {
return err
}

if err := e.areTaskRequiredVarsAllowedValuesSet(t); err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions testdata/if/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,21 @@ tasks:
if: '{{ eq .ENV "dev" }}'
cmds:
- echo "should not appear"

# Task-level if with dynamic variable (condition met)
task-if-dynamic-true:
vars:
ENABLE_FEATURE:
sh: 'echo "true"'
if: '{{ eq .ENABLE_FEATURE "true" }}'
cmds:
- echo "dynamic feature enabled"

# Task-level if with dynamic variable (condition not met)
task-if-dynamic-false:
vars:
ENABLE_FEATURE:
sh: 'echo "false"'
if: '{{ eq .ENABLE_FEATURE "true" }}'
cmds:
- echo "should not appear"
2 changes: 2 additions & 0 deletions testdata/if/testdata/TestIf-task-if-dynamic-false.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
task: dynamic variable: "echo \"false\"" result: "false"
task: if condition not met - skipped: "task-if-dynamic-false"
1 change: 1 addition & 0 deletions testdata/if/testdata/TestIf-task-if-dynamic-true.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dynamic feature enabled
Loading