Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NikitaCOEUR opened this issue Dec 18, 2024 · 4 comments · May be fixed by #1967
Open

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

NikitaCOEUR opened this issue Dec 18, 2024 · 4 comments · May be fixed by #1967
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@NikitaCOEUR
Copy link

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
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Dec 18, 2024
@trulede
Copy link

trulede commented Dec 18, 2024

You might try and add EXIT_CODE as a parameter to the task being deferred.

version: '3'

tasks:
  exit:
    vars:
      EXIT_CODE: '{{.EXIT_CODE}}'
    cmd: echo '{{if .EXIT_CODE}}Failed with {{.EXIT_CODE}}!{{else}}Success!{{end}}'

  exit-one:
    cmds:
      - defer:
          task: exit
          vars:
            EXIT_CODE: {{.EXIT_CODE}}
      - |
        exit 1

@NikitaCOEUR
Copy link
Author

Hey, thanks for the suggestion, but it doesn’t seem to work either:

$ task exit-one
task: [exit-one] exit 1

task: [exit] echo 'Success!'
Success!
task: Failed to run task "exit-one": exit status 1

@trulede
Copy link

trulede commented Dec 20, 2024

OK, the code here is only operating on the Cmd.

task/task.go

Line 343 in c5be676

cmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, extra)

I think if the EXIT_CODE was also added to the cmd.Vars at the same point; in the case when cmd.Vars is available, i.e. a Task and not a command; then probably this would work like you expect.

@NikitaCOEUR
Copy link
Author

That makes sense ! I will test that.

@NikitaCOEUR NikitaCOEUR linked a pull request Dec 21, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants