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

Doubt about static/dynamic env and tasks with dir #854

Closed
Northburns opened this issue Sep 2, 2022 · 3 comments
Closed

Doubt about static/dynamic env and tasks with dir #854

Northburns opened this issue Sep 2, 2022 · 3 comments

Comments

@Northburns
Copy link

  • 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
Copy link
Author

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 😄

@pd93
Copy link
Member

pd93 commented Sep 8, 2022

@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 pd93 added the state: awaiting response Waiting for issue author to respond. label Sep 9, 2022
@Northburns
Copy link
Author

Thank you, that is totally suitable for my cases 👍

@github-actions github-actions bot removed the state: awaiting response Waiting for issue author to respond. label Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants