Skip to content

Issue with EXIT_CODE variable not available in deferred tasks referencing another task #1966

@NikitaCOEUR

Description

@NikitaCOEUR

Description

I am trying to configure a task that is used in a defer based on the EXIT_CODE generated when a command fails in my Taskfile. However, I am encountering an issue where the EXIT_CODE variable is only available in a defer that uses a command directly, but not in a defer that references another task.

Steps to reproduce:

  1. Generate the Taskfile :
# https://taskfile.dev

version: '3'

tasks:
  exit:
    cmd: echo {{.MESSAGE}}

  exit-with-exit-code:
    cmd: echo Defer to task with EXIT_CODE management {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}

  exit-one:
    cmds:
      - defer:
          task: exit
          vars:
            MESSAGE:
              sh: "echo Defer to task via vars with sh {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}"
      - defer:
          task: exit
          vars:
            MESSAGE: "Defer to task via classic vars {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}"
      - defer:
          task: exit-with-exit-code
      - defer: "echo 'Defer to cmd' {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}"
      - |
        exit 1
  1. Execute this command : task exit-one
  2. This produces :
task: [exit-one] exit 1

task: [exit-one] echo 'Defer to cmd' Failed with 1!
Defer to cmd Failed with 1!
task: [exit-with-exit-code] echo Defer to task with EXIT_CODE management Success!
Defer to task with EXIT_CODE management Success!
task: [exit] echo Defer to task via classic vars Success!
Defer to task via classic vars Success!
task: [exit] echo Defer to task via vars with sh Success!
Defer to task via vars with sh Success!
task: [exit-with-exit-code] echo Defer to task with EXIT_CODE management Success!
Defer to task with EXIT_CODE management Success!
task: Failed to run task "exit-one": exit status 1
  1. But i expected :
task: [exit-one] exit 1

task: [exit-one] echo 'Defer to cmd' Failed with 1!
Defer to cmd Failed with 1!
task: [exit-with-exit-code] echo Defer to task with EXIT_CODE management Failed with 1!
Defer to task with EXIT_CODE management Failed with 1!
task: [exit] echo Defer to task via classic vars Failed with 1!
Defer to task via classic vars Failed with 1!
task: [exit] echo Defer to task via vars with sh Failed with 1!
Defer to task via vars with sh Failed with 1!
task: [exit-with-exit-code] echo Defer to task with EXIT_CODE management Failed with 1!
Defer to task with EXIT_CODE management Failed with 1!
task: Failed to run task "exit-one": exit status 1

Version

Task version: v3.40.1 (h1:Q/TjPh5SAI+XK2fBravDPZXNGm6mvnwrLJqCXfhZ3Wc=)

Operating system

Debian 12

Experiments Enabled

No response

Example Taskfile

# https://taskfile.dev

version: '3'

tasks:
  exit:
    cmd: echo {{.MESSAGE}}

  exit-with-exit-code:
    cmd: echo Defer to task with EXIT_CODE management {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}

  exit-one:
    cmds:
      - defer:
          task: exit
          vars:
            MESSAGE:
              sh: "echo Defer to task via vars with sh {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}"
      - defer:
          task: exit
          vars:
            MESSAGE: "Defer to task via classic vars {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}"
      - defer:
          task: exit-with-exit-code
      - defer: "echo 'Defer to cmd' {{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}"
      - |
        exit 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions