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

Template function breaks the "requires" feature #1950

Open
NorbertHauriel opened this issue Dec 12, 2024 · 3 comments · May be fixed by #1962
Open

Template function breaks the "requires" feature #1950

NorbertHauriel opened this issue Dec 12, 2024 · 3 comments · May be fixed by #1962
Labels
area: templating Changes related to the templating engine. area: variables Changes related to variables.

Comments

@NorbertHauriel
Copy link
Contributor

NorbertHauriel commented Dec 12, 2024

This could be the exact same, or similar issue as #1557, but there is no reproduction happening there, so I decided to make a new issue, because I found a way to reproduce the "requires" breakage. The required variable is not enforced in the below example.

Expected: "task: Task "default" cancelled because it is missing required variables: MY_VAR"
Result: "template: :1:28: executing "" at <" ">: invalid value; expected string"
Taskfile:

version: 3
tasks:
  default:
    requires:
      vars: [MY_VAR]
    cmd: |
      {{range .MY_VAR | splitList " " }}
      {{end}}

When not using the template function, the required variable feature works as expected.
Result: "task: Task "default" cancelled because it is missing required variables: MY_VAR"

version: 3
tasks:
  default:
    requires:
      vars: [MY_VAR]
    cmd: |
      {{range .MY_VAR }}
      {{end}}
  • Task version: v3.40.0
  • Operating system: Linux, macOS
  • Experiments enabled: no
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Dec 12, 2024
@vmaerten
Copy link
Member

Hello
This is not linked to the require feature. This is because there is a template error.

version: 3
tasks:
  default:
    cmd: |
      {{range .MY_VAR | splitList " " }}
      {{end}}

The above still results in an error.

Your template seems incorrect, and I'm not sure what you're trying to achieve.
For reference, here’s a valid template:

cmd: '{{range .MY_VAR}}{{range splitList . " "}}{{.}}{{end}}{{end}}'

@task-bot task-bot removed the state: needs triage Waiting to be triaged by a maintainer. label Dec 15, 2024
@NorbertHauriel
Copy link
Contributor Author

NorbertHauriel commented Dec 15, 2024

Hi vmaerten.

The use case is simple. Set MY_VAR to a space delimited list of words (by the task runner person), then iterate on them. But MY_VAR is REQUIRED!
So if you do not provide the required MY_VAR, the implicit documentation (by failing early with a clear message) should be done by the "requires" feature. But instead, the template resolution front runs it, but why is this happening? It does not make sense to move forward when dependencies are missing.

In other words, the task fails late, not early. And it is an incorrect behavior from the user's standpoint. Early failure is when we immediately see the missing required variable, and not even attempt to resolve the template (as it depends on the variable, hence the template error. It is NOT a syntax error, it is fully valid templating syntax).

Here is an example:

  default:
    requires:
      vars: [MY_VAR]
    cmd: |
      {{range .MY_VAR | splitList " " }}
        echo {{.}}
      {{end}}

To call the task, the user gives something like this:
task MY_VAR="my words"

And the result is:

task: [default] 
  echo my

  echo words

my
words

And of course, since we are talking about real world use cases, the template error should rightfully happen when the user provides an incorrect variable. But here, the user explicitly wants to tell Task to ensure the existence of a variable, becasue it is a dependency of the template.

@vmaerten vmaerten reopened this Dec 15, 2024
@vmaerten vmaerten added type: bug Something not working as intended. area: variables Changes related to variables. area: templating Changes related to the templating engine. labels Dec 15, 2024
@vmaerten
Copy link
Member

Thank you for the explanation! I apologize for not fully understanding your issue initially.

You're absolutely right—in your case, which is valid, the message should be:
task: Task "default" cancelled because it is missing required variables: MY_VAR

Until this is addressed, you can use the following workaround by defining a default value:

version: 3
tasks:
  default:
    requires:
      vars: [MY_VAR]
    cmd: |
      {{range (default "" .MY_VAR) | splitList " " }}
        echo {{.}}
      {{end}}

@vmaerten vmaerten linked a pull request Dec 15, 2024 that will close this issue
@vmaerten vmaerten linked a pull request Dec 15, 2024 that will close this issue
@pd93 pd93 removed the type: bug Something not working as intended. label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: templating Changes related to the templating engine. area: variables Changes related to variables.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants