task icon indicating copy to clipboard operation
task copied to clipboard

ENV vars are not usable within other env var declarations

Open vexvec opened this issue 3 years ago • 0 comments

  • Task version: 3.13.0
  • Operating System: linux
version: "3"
env:
  CGO_ENABLED: 0
  DISTDIR:
    sh: 'echo $(pwd)/dist'
  MODULE_NAME:
    sh: "cat ./go.mod | head -n 1 | sed 's/module//'"
  VERSION:
    sh: git describe --tags || printf "dev"
  COMMIT:
    sh: git log --format="%H" -n 1 || true
  BRANCH:
    sh: git branch --show-current
  PRJ_NAME:
    sh: basename ${MODULE_NAME}
tasks:
  prj:
    cmds:
    - echo $PRJ_NAME

With the above yaml there it gives an error when running the prj task. The reason is that the variable MODULE_NAME was not evaluated before executing the basename cmd. Currently they are not evaluated one by one, but all together, I think. As maps by nature don't have a specific ordering it cannot be guaranteed that they are evaluated in the correct order. Therefore I would suggest to make the env declarations in a list, similar like kubernetes does it. example:

version: "3"
env:
- CGO_ENABLED=0
- DISTDIR=$(echo $(pwd)/dist)

Then the env vars should be evaluated one by one and can be set in a map that can then be used in the tasks for variable substitution (sprig...)

This approach would also allow combining and overriding env vars.

vexvec avatar Jun 28 '22 13:06 vexvec