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

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

Closed
WinkelCode opened this issue Nov 3, 2024 · 1 comment · Fixed by #1904
Labels
area: variables Changes related to variables.

Comments

@WinkelCode
Copy link

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
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Nov 3, 2024
@WinkelCode WinkelCode changed the title In dynamic (shell) variables, an empty (no characters) script block after template expansion causes type error when referenced In dynamic (shell) variables, template expansion resulting in an empty (no characters) script block causes type error when referenced Nov 3, 2024
@pd93 pd93 added type: bug Something not working as intended. area: variables Changes related to variables. and removed state: needs triage Waiting to be triaged by a maintainer. labels Nov 4, 2024
@pd93
Copy link
Member

pd93 commented Nov 4, 2024

Thanks for the detailed repro. #1904 should fix this.

@pd93 pd93 closed this as completed in #1904 Nov 4, 2024
@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: variables Changes related to variables.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants