Task: Overriding vars in called task not working as expected

Created on 10 Sep 2020  路  6Comments  路  Source: go-task/task

macOS Mojave 10.14.6

$ task --version
Task version: 2.8.1
version: "2"

silent: true

tasks:
  default:
    cmds:
      - task: build

  build:
    desc: Build the GO binary
    vars:
      GOOS: linux
    cmds:
      - echo {{.GOOS}}

  build-darwin:
    desc: Build the GO binary for macOS
    cmds:
      - task: build
        vars: {GOOS: "darwin"}

  build-linux:
    desc: Build the GO binary for Linux
    cmds:
      - task: build
        vars: {GOOS: "linux"}

Expected output:

$ task build
linux
$ task build-darwin
darwin
$ task build-linux
linux

Actual output:

$ task build
linux
$ task build-darwin
linux
$ task build-linux
linux

What am I doing wrong?

Manual says: "Overriding variables in the called task is as simple as informing vars attribute" (my emphasis)

question variables

Most helpful comment

@BeyondEvil I believe what is missing here is single quotes around the assignment in vars:, ie.

      GOOS: '{{default "linux" .GOOS}}'

All 6 comments

Using the example from the manual and calling write-file generates: task: Failed to run task "write-file": 1:19: > must be followed by a word

Hi @BeyondEvil,

That's by design. Your build task should probably be like:

tasks:
  build:
    desc: Build the Go binary
    vars:
      GOOS: {{default "linux" .GOOS}}
    cmds:
      - echo {{.GOOS}}

As you was doing, you were always overriding whatever value is passed as argument by linux, because it was fixed in the task.

Ok, I can't find any mention of that syntax in the documentation and running your example (regardless of version 2 or 3) generates an error:

task: can't unmarshal var value

@andreynering

@BeyondEvil I believe what is missing here is single quotes around the assignment in vars:, ie.

      GOOS: '{{default "linux" .GOOS}}'

@BeyondEvil I believe what is missing here is single quotes around the assignment in vars:, ie.

      GOOS: '{{default "linux" .GOOS}}'

Thank you! 馃憤 @achton

@andreynering The documentation states:

Overriding variables in the called task is as simple as informing vars attribute

... But the example there doesn't actually do any overriding, it only sets unset variables. It would be really helpful to adapt a working example of actually overriding a {{default ...}} variable value.

... So I opened PR #407 with a modest proposal to do that.

Was this page helpful?
0 / 5 - 0 ratings