task icon indicating copy to clipboard operation
task copied to clipboard

Fix and modify requires behavior.

Open trulede opened this issue 1 year ago • 1 comments

Fix and modify the behavior of requires.

Fixes #1676 (discussion #1620) Modifies/updates #1204

Requires was not working in the case that a variable was set to nil (see discussion/issue).

Additional, the behavior of requires is modified so that an zero length (empty) string value fails the required check. In more complex use cases, with sub-tasks and variable templates, the expectation is that requires will ensure that a variable is set to something. In such cases, an empty string is not considered as something and so the feature is not useful.

An alternative would be to have a way to set a variable value to nil via the template mechanism.

The change is tested with the following taskfile (modified from the original discussion).

version: "3"

silent: true

vars:
  IMAGE_NAME: my-web-app
  CONTAINER_REGISTRY: 
  # CONTAINER_REGISTRY: # comment
  # CONTAINER_REGISTRY: ''
  # CONTAINER_REGISTRY: ' '

tasks:
  check-missing:
    desc: Test the task variables
    requires:
      vars: [CONTAINER_REGISTRY]
    cmds:
      - echo "CONTAINER_REGISTRY is {{spew .CONTAINER_REGISTRY}}"

trulede avatar Jun 01 '24 11:06 trulede

Further to the change in behaviour, please see the results of this experiment where the templating system is considering a zero length string as "not set".

version: "3"

vars:
  A: fubar
  B: ''
  C:

tasks:
  check-set:
    desc: Test the task variables
    vars:
      A_VAR:  '{{.A | default "A NOT SET"}}'
      B_VAR:  '{{.B | default "B NOT SET"}}'
      C_VAR:  '{{.C | default "C NOT SET"}}'
    cmds:
      - echo "A is {{.A_VAR}}"
      - echo "B is {{.B_VAR}}"
      - echo "C is {{.C_VAR}}"

produces this output

$ task check-set -f -v
task: [/mnt/c/Users/trule] Not found - Using alternative (Taskfile.yml)
task: "check-set" started
task: [check-set] echo "A is fubar"
A is fubar
task: [check-set] echo "B is B NOT SET"
B is B NOT SET
task: [check-set] echo "C is C NOT SET"
C is C NOT SET
task: "check-set" finished

This PR modifies the requires behaviour to match that of the templating system, which is I think essential for requires to be usable. Currently, we do this FOO: '{{.FOO| default "FOO_NOT_SPECIFIED"}}' and hope that the user will notice the strange message in the output ... we tried requires, but it did not work. The concise error message that requires produces would be very welcome, both for developers and users.

trulede avatar Jun 02 '24 09:06 trulede