Sample taskfile showcasing the bug:
version: '3'
vars:
ITEMS:
- "a"
- "b"
tasks:
default:
vars:
FILTERED_STR:
sh: |-
{{range .ITEMS -}}
{{if eq . "c" -}}
echo {{.}}
{{end -}}
{{end -}}
FILTERED:
ref: compact (splitList "\n" .FILTERED_STR)
cmds:
- echo "{{.FILTERED}}"
Explanation
When a sh: block becomes completely empty after template expansion and its value is referenced later, Task throws a type error instead of treating it as an empty string.
$ task
template: resolver:1:26: executing "resolver" at <.FILTERED_STR>: wrong type for value; expected string; got interface {}
Workaround
The workaround is to ensure any character always remains in the script block. For instance, a # could be added, or whitespace trimming - }} could be omited.
[...]
FILTERED_STR:
sh: |-
#
{{range .ITEMS -}}
{{if eq . "c" -}}
echo {{.}}
{{end -}}
{{end -}}
[...]
$ task
task: [default] echo "[]"
[]
$ task --version
Task version: 3.39.2 ()
- Operating system: macOS Sequoia 15.1
- Experiments enabled: None