go-task / task

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

Inconsistent Variable Expansion When Using `{{.VAR}}` in `.env` Files with Nested Variables #1847

Open WinkelCode opened 2 months ago

WinkelCode commented 2 months ago

When using a .env file to define environment variables that rely on other variables using {{.VAR}} template syntax, the variables sometimes expand inconsistently across task runs. The issue becomes more apparent as the variables become more deeply nested.


Steps to Reproduce:

  1. Create a Taskfile.yml:

    version: "3"
    
    dotenv: [".env"]
    
    vars:
     BASE_DIR_VAR: "/home/user"
     MID_DIR_VAR: "{{.BASE_DIR_VAR}}/nested"
     FINAL_DIR_VAR: "{{.MID_DIR_VAR}}/deeper"
     FULL_PATH_VAR: "{{.FINAL_DIR_VAR}}/file.txt"
    
    env:
     BASE_DIR_ENV: "/home/user"
     MID_DIR_ENV: "{{.BASE_DIR_ENV}}/nested"
     FINAL_DIR_ENV: "{{.MID_DIR_ENV}}/deeper"
     FULL_PATH_ENV: "{{.FINAL_DIR_ENV}}/file.txt"
    
    tasks:
     show_vars:
       cmds:
         - 'echo "FULL_PATH:     $FULL_PATH"'
         - 'echo "FULL_PATH2:    $FULL_PATH_2"'
         - 'echo "FULL_PATH_VAR: {{.FULL_PATH_VAR}}"'
         - 'echo "FULL_PATH_ENV: {{.FULL_PATH_ENV}}"'
       silent: true
  2. Create a .env File:

    BASE_DIR=/home/user
    MID_DIR=$BASE_DIR/nested
    FINAL_DIR=$MID_DIR/deeper
    FULL_PATH=$FINAL_DIR/file.txt
    MID_DIR_2={{.BASE_DIR}}/nested
    FINAL_DIR_2={{.MID_DIR_2}}/deeper
    FULL_PATH_2={{.FINAL_DIR_2}}/file.txt
  3. Run the task multiple times:

    task show_vars

Sample Outputs:

$ task show_vars
FULL_PATH:     /home/user/nested/deeper/file.txt
FULL_PATH2:    /home/user/nested/deeper/file.txt
FULL_PATH_VAR: /home/user/nested/deeper/file.txt
FULL_PATH_ENV: /home/user/nested/deeper/file.txt

$ task show_vars
FULL_PATH:     /home/user/nested/deeper/file.txt
FULL_PATH2:    /nested/deeper/file.txt
FULL_PATH_VAR: /home/user/nested/deeper/file.txt
FULL_PATH_ENV: /home/user/nested/deeper/file.txt

$ task show_vars
FULL_PATH:     /home/user/nested/deeper/file.txt
FULL_PATH2:    /nested/deeper/file.txt
FULL_PATH_VAR: /home/user/nested/deeper/file.txt
FULL_PATH_ENV: /home/user/nested/deeper/file.txt

$ task show_vars
FULL_PATH:     /home/user/nested/deeper/file.txt
FULL_PATH2:    /deeper/file.txt
FULL_PATH_VAR: /home/user/nested/deeper/file.txt
FULL_PATH_ENV: /home/user/nested/deeper/file.txt