fix: evaluate task-level if condition after resolving dynamic variables

This commit is contained in:
Valentin Maerten
2026-01-25 19:40:29 +01:00
committed by Andrey Nering
parent 026c899d90
commit 5a78808caa
5 changed files with 37 additions and 11 deletions

View File

@@ -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 {

23
task.go
View File

@@ -148,17 +148,6 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
return nil
}
if strings.TrimSpace(t.If) != "" {
if err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: t.If,
Dir: t.Dir,
Env: env.Get(t),
}); err != nil {
e.Logger.VerboseOutf(logger.Yellow, "task: if condition not met - skipped: %q\n", call.Task)
return nil
}
}
// Prompt for missing required vars (just-in-time for sequential task calls)
prompted, err := e.promptTaskVars(t, call)
if err != nil {
@@ -185,6 +174,18 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
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,
Dir: t.Dir,
Env: env.Get(t),
}); err != nil {
e.Logger.VerboseOutf(logger.Yellow, "task: if condition not met - skipped: %q\n", call.Task)
return nil
}
}
if !e.Watch && atomic.AddInt32(e.taskCallCount[t.Task], 1) >= MaximumTaskCall {
return &errors.TaskCalledTooManyTimesError{
TaskName: t.Task,

View File

@@ -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"

View File

@@ -0,0 +1,2 @@
task: dynamic variable: "echo \"false\"" result: "false"
task: if condition not met - skipped: "task-if-dynamic-false"

View File

@@ -0,0 +1 @@
dynamic feature enabled