go-task / task

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

Task is doing something weird with escaping... #712

Open ghostsquad opened 2 years ago

ghostsquad commented 2 years ago

As related to #179

Example Taskfile showing the issue

version: '3'

tasks:
  one:
    vars:
      FOO: '{{.FOO}}'
    cmds:
    - echo '{{.FOO}}'

  two:
    cmds:
    - task: one
      vars:
        FOO: '{{`${{should be escaped}}`}}'
at 18:03:01 ❯ t --taskfile Taskfile.test.yml --dry two
task: Failed to run task "two": template: :1: function "should" not defined

The more upvoted answer in stackoverflow indicates that this should work:

{{`{{Your.Angular.Data}}`}}

As should the following which is syntax needed for templating github actions workflow yaml file.

{{`${{Your.Angular.Data}}`}}

Gomplate (A CLI tool for go template) handles this just fine:

at 18:09:37 ❯ gomplate --in '{{`${{should be escaped}}`}}'

${{should be escaped}}%

**Note that the trailing % is the shell's way of telling you there's no trailing newline character at the end of this output.

Also reproducible on go playground: https://go.dev/play/p/JkgDIrUs3RQ

It's almost as if the task is double evaluating things?

ghostsquad commented 2 years ago

some more information on this:

version: '3'
tasks:
  # works
  foo:
    cmds:
    - echo '{{`${{blahblah}}`}}'

  # works
  foo2:
    vars:
      MYFOO: '{{`${{blahblah}}`}}'
    cmds:
    - echo '{{.MYFOO}}'

  # fails
  foo3:
    cmds:
    - task: foo2
      vars:
        MYFOO: '{{`${{blahblah}}`}}'

  # successfully prints an empty string when called alone
  foo4:
    vars:
      MYFOO: '{{.MYFOO}}'
    cmds:
    - echo '{{.MYFOO}}'

  # fails
  foo5:
    cmds:
    - task: foo4
      vars:
        MYFOO: '{{`${{blahblah}}`}}'
siggimoo commented 2 years ago

Looking at the source code, a task's variables are evaluated every time that task is run. So if you're passing a value through a chain of tasks it will be processed at each one. Perhaps there should be an option on variables to bypass the template engine.

ghostsquad commented 2 years ago

Definitely think that's a bug.

ghostsquad commented 2 years ago

Yes, a bypass option would work, but escaping also would work. Bypass in this case wouldn't work well, because it prevents any sort of default behavior.