Skip to content

In dynamic (shell) variables, template expansion resulting in an empty (no characters) script block causes type error when referenced #1903

@WinkelCode

Description

@WinkelCode

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
Task version: 3.39.2 ()
  • Operating system: macOS Sequoia 15.1
  • Experiments enabled: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions