go-task / task

A task runner / simpler Make alternative written in Go
https://taskfile.dev
MIT License
11.4k stars 616 forks source link

[v3.39.0] defer no longer evaluates variables #1803

Closed robcao closed 1 month ago

robcao commented 1 month ago

defer is no longer evaluating variables in task 3.39.0. In the sample Taskfile.yml below, I've created a simple reproduction.

To reproduce:

Run task my-repro with the taskfile below.

See the following output is

my-value

Expected behavior (+ the behavior on v3.38.0):

my-value
my-value
# https://taskfile.dev

version: '3'
silent: true
tasks:
  my-repro:
    cmds:
      - defer: echo {{ .MY_VAR }}
      - echo {{ .MY_VAR }}
    vars:
      MY_VAR: my-value
robcao commented 1 month ago

I believe this was the change that introduced the behavior change: https://github.com/go-task/task/pull/1762

I pulled down the commit before the change: git co 35119c12ab222fc5a2a1d28536f5e575136e103e, and validated that the command works as expected on this commit via task my-repro.

Pulling down the change: git co b259edeb65e24f28c41604b9819869aca082d3b6 and running the task my-repro shows the erroneous behavior

officel commented 1 month ago

I have the same problem. However, I have been able to pass variables to defer for some time now, but it seems that was not correct in the schema, so I am having trouble.

https://github.com/go-task/vscode-task/issues/158

mgbowman commented 1 month ago

+1

mgbowman commented 1 month ago

This seems like a major regression that should have been caught by a test case.

All my defer: rm -f "{{.VAR}}" statements are now evaluating to rm -f "" 🙃

kevintijssen commented 1 month ago

Just updated to Task version: v3.39.1 (h1:yIaLD5TZWXU/SdmadBReQKu8jblRxzRT+lJG4xC3CMw=) but I still have the same issue

mgbowman commented 1 month ago

@kevintijssen are you sure? I just updated and it works fine on linux/amd64 (which I think you're using from your version hash) ...

# Taskfile.1803.yml
version: "3"
vars:
  FILE_VAR: "file-issue-1803"
tasks:
  repro:
    vars:
      TASK_VAR: "task-issue-1803"
    cmds:
      - defer: rmdir "{{.FILE_VAR}}"
      - defer: rmdir "{{.TASK_VAR}}"
      - mkdir -p "{{.FILE_VAR}}"
      - mkdir -p "{{.TASK_VAR}}"
❯ task -t Taskfile.1803.yml repro
task: [repro] mkdir -p "file-issue-1803"
task: [repro] mkdir -p "task-issue-1803"
task: [repro] rmdir "task-issue-1803"
task: [repro] rmdir "file-issue-1803"
kevintijssen commented 1 month ago

Task version: v3.39.1 (h1:yIaLD5TZWXU/SdmadBReQKu8jblRxzRT+lJG4xC3CMw=) Linux 828aaae9a89d 6.4.16-linuxkit #1 SMP PREEMPT Fri Nov 10 14:49:23 UTC 2023 aarch64 GNU/Linux

wait-for:
    desc: "Will wait for resources to gain the requested condition."
    deps:
      - task: check-tools
    requires:
      vars:
        - RESOURCE
        - CONDITION

    vars:
      CONDITIONAL_ARGUMENTS: "{{if .KUBECONTEXT}}--context={{.KUBECONTEXT}} {{end}} {{if .NAMESPACE}} --namespace={{.NAMESPACE}} {{end}}"
      TEMP_FILE:
        sh: mktemp

    cmds:
      - defer: rm {{.TEMP_FILE}}
      - cmd: |
          cat > {{.TEMP_FILE}} <<EOF
            until kubectl {{.CONDITIONAL_ARGUMENTS}} get {{.RESOURCE}} &> /dev/null; do
              sleep 2
            done

            {{- if ne .CONDITION "exists"}}
            kubectl {{ .CONDITIONAL_ARGUMENTS }} wait --for condition={{.CONDITION}} {{.RESOURCE}} --timeout 500s
            {{end}}
          EOF
      - task: execute-bash
        vars:
          GUM_SPIN_TITLE: "{{.GUM_SPIN_TITLE}}"
          TEMP_FILE: "{{.TEMP_FILE}}"

      - cmd: gum log --level info "{{.RESOURCE}} condition {{.CONDITION}} met"
INFO deployment/coredns condition available met
task: "dev:k3d:utils:wait-for" finished
task: [dev:k3d:utils:wait-for] rm 
rm: missing operand
Try 'rm --help' for more information.
task: ignored error in deferred cmd: exit status 1
mgbowman commented 1 month ago

Looks like the bug is partially fixed 🙈

@andreynering @vmaerten the fix in #1814 doesn't seem to support dynamic vars

# Taskfile.1803.yml
version: "3"
vars:
  FILE_VAR: "file-issue-1803"
tasks:
  repro:
    vars:
      TASK_VAR: "task-issue-1803"
      SH_TASK_VAR:
        sh: echo "sh-task-issue-1803"
    cmds:
      - defer: rmdir "{{.FILE_VAR}}"
      - defer: rmdir "{{.TASK_VAR}}"
      - defer: rmdir "{{.SH_TASK_VAR}}"
      - mkdir -p "{{.FILE_VAR}}"
      - mkdir -p "{{.TASK_VAR}}"
      - mkdir -p "{{.SH_TASK_VAR}}"
❯ task -t Taskfile.1803.yml repro
task: [repro] mkdir -p "file-issue-1803"
task: [repro] mkdir -p "task-issue-1803"
task: [repro] mkdir -p "sh-task-issue-1803"
task: [repro] rmdir ""
rmdir: failed to remove '': No such file or directory
task: [repro] rmdir "task-issue-1803"
task: [repro] rmdir "file-issue-1803"

❯ test -d sh-task-issue-1803 && echo 'directory still exists :('
directory still exists :(
vmaerten commented 1 month ago

You're totally right. I've opened another PR to fix this!

mgbowman commented 1 month ago

woohoo! 🚀

❯ task --version
Task version: v3.39.2 (h1:Zt7KXHmMNq5xWZ1ihphDb+n2zYLCo4BdRe09AnMMIgA=)

❯ task -t Taskfile.1803.yml repro
task: [repro] mkdir -p "file-issue-1803"
task: [repro] mkdir -p "task-issue-1803"
task: [repro] mkdir -p "sh-task-issue-1803"
task: [repro] rmdir "sh-task-issue-1803"
task: [repro] rmdir "task-issue-1803"
task: [repro] rmdir "file-issue-1803

Thanks @vmaerten for the quick turn around on this 💪

cwegener commented 1 month ago

It's still broken on snapcraft.io stable channel. It doesn't seem to make sense for me to manually switch to the edge channel for this snap to get a working version, or no?!

andreynering commented 1 month ago

@cwegener I just released on Snapcraft. Sorry, it was just forgotten.

cwegener commented 1 month ago

Thanks! The stable snapcraft channel works again now..