task icon indicating copy to clipboard operation
task copied to clipboard

Doubt about static/dynamic `env` and tasks with `dir`

Open Northburns opened this issue 2 years ago • 2 comments

  • Task version: v3.14.1 (h1:myTmEIbMbQx2D+g5lJvnbSqX531HmxiaQuefAqz8ISA=)
  • Operating System: Windows (I have busybox installed for ls, printenv, etc)

Example Taskfile showing the issue

version: '3'

vars:
  DIR_TEMP: temp
  DIR_DATA: data

env:
  D1:
    sh: ls -1 {{.DIR_DATA}} || echo "ERROR1 ({{.DIR_DATA}} @ $(basename "$(pwd)"))"
  D2:
    sh: ls -1 data || echo "ERROR2 (data @ $(basename "$(pwd)"))"
  
tasks:
  setup:
    dir: '{{.DIR_DATA}}'
    cmds: [ 'echo "data" > data.txt' ]
  
  default:
    cmds:
      - printenv D1
      - printenv D2
      - task: t1

  t1:
    dir: '{{.DIR_TEMP}}'
    cmds:
      - printenv D1
      - printenv D2

Running task setup sets up files for the demo. Execution:

> task 
task: [default] printenv D1
data.txt
task: [default] printenv D2
data.txt
task: [t1] printenv D1
data.txt
task: [t1] printenv D2
data.txt

> task t1
ls: data: No such file or directory
task: [t1] printenv D1
ERROR1 (data @ temp)
task: [t1] printenv D2
data.txt

It looks like if I run the default task, the envs are evaluated in the taskfile's directory. If I run t1 task, the envs are evaluated in t1's directory. The thing that I have doubts about is that D2 is evaluated as I expected. It doesn't have any templating, could that be a lead?

In my actual use case I just need the contents of a certain fixed directory (data 😄 ) in an env variable. I think I can circumvent this by hardcoding directory names, which leads to a non-templated env sh, which is totally fine for my use case. I tried to read the documentation if there's something I'm misunderstanding or missing. Any insight or help is appreciated. This might relate to issues #442 #524 #591

Northburns avatar Sep 02 '22 11:09 Northburns

Super-tiny update: I refactored my global envs to be global vars, and what I really needed as an env var I setup as a task local env. This works as I expected, it seems 🤔 But yeah, still interested in this, but I'm of course not putting any pressure here 😄

Northburns avatar Sep 02 '22 11:09 Northburns

@Northburns ~the env keyword does not officially support the sh subkey~. To get around this, you can set a variable using sh and then set the environment variable using templating syntax. The following works for me:

vars:
  DIR_TEMP: temp
  DIR_DATA: data
  D1:
    sh: ls -1 {{.DIR_DATA}} || echo "ERROR1 ({{.DIR_DATA}} @ $(basename "$(pwd)"))"
  D2:
    sh: ls -1 data || echo "ERROR2 (data @ $(basename "$(pwd)"))"

env:
  D1: "{{.D1}}"
  D2: "{{.D2}}"

pd93 avatar Sep 08 '22 19:09 pd93

Thank you, that is totally suitable for my cases 👍

Northburns avatar Nov 30 '22 09:11 Northburns