go-task / task

A task runner / simpler Make alternative written in Go
https://taskfile.dev
MIT License
11.01k stars 584 forks source link

[Bug] Global Taskfile with local dotenv file not loading Taskfile variables using `USER_WORKING_DIR` path reference #1629

Open rrmistry opened 4 months ago

rrmistry commented 4 months ago

With this Taskfile:

# Global Taskfile: ~/Taskfile.yaml
version: "3"
dotenv:
  - "{{.USER_WORKING_DIR}}/.env" # <<<< Does not work (but it should)
  # - "~/repos/my-project/.env"  # <<<< Works
tasks:
  build:
    dir: '{{.USER_WORKING_DIR}}'
    dotenv: ["{{.USER_WORKING_DIR}}/.env"] # <<<< Does not load Taskfile Variable either but it does load env variable
    cmds:
      # Should print: "value = testABC123"
      - echo value = {{.DOT_ENV_VAR}}

And .env file:

# Local .env file: ~/repos/my-project/.env
DOT_ENV_VAR=testABC123

Then running this:

mkdir -p ~/repos/my-project
cd ~/repos/my-project
echo "DOT_ENV_VAR=testABC123" > .env
task -g build

Should print:

task: [build] echo value = testABC123
value = testABC123

But prints:

task: [build] echo value = 
value = 

This is not a blocker as we can still use script variables (instead of Taskfile variables):

# Global Taskfile: ~/Taskfile.yaml
version: "3"
tasks:
  build:
    dir: '{{.USER_WORKING_DIR}}'
    dotenv: ["{{.USER_WORKING_DIR}}/.env"] # <<<< Workaround loads env variables but not as Taskfile variables
    cmds:
      # Will print: "value = testABC123"
      - echo value = $DOT_ENV_VAR # Workaround

But it is not ideal as it prints:

task: [build] echo value = $DOT_ENV_VAR 
value = testABC123

We would like to see task output with variable values replaced in the script like below:

task: [build] echo value = testABC123
value = testABC123

Task Version Output:

 task --version
Task version: v3.36.0 (h1:XVJ5hQ5hdzTAulHpAGzbUMUuYr9MUOEQFOFazI3hUsY=)
meoyawn commented 3 months ago

unfortunately local dotenvs don't work at all https://github.com/go-task/task/issues/997