go-task / task

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

[BUG] Enable dotenv will make go template pipeline be invalid. #1232

Open 100gle opened 1 year ago

100gle commented 1 year ago

How to reproduce it?

When I don't include dotenv key in config file, all things work well:

version: '3'

vars:
  ROOT: 
    sh: echo {{toSlash .ROOT_DIR}}
  DOCKER: 
    sh: echo {{.ROOT}}/docker

tasks:
  foo:
    cmds: 
      - echo {{.DOCKER}}
# in my directory: D:/Reops/test
$ task foo
task: [foo] echo D:/Repos/test/docker
D:/Repos/test/docker

But when I add dotenv keys in config file with an empty .env file(created but no content):

version: '3'

dotenv: 
  - .env

vars:
  ROOT: 
    sh: echo {{toSlash .ROOT_DIR}}
  DOCKER: 
    sh: echo {{.ROOT}}/docker

tasks:
  foo:
    cmds: 
      - echo {{.DOCKER}}

then run task again and the console will show:

$ task foo
template: :1:15: executing "" at <.ROOT_DIR>: invalid value; expected string
dennybaa commented 11 months ago

+1 Well it seems more like that all taskfile defined env is simply not substituted in dynamic vars.


env:
   IMAGE_DIR: "HELLO"

# or
# dotenv: [".env"]

tasks:
  mkdisk:
    vars:
      imageDir: '{{ env "IMAGE_DIR" }}'
    cmds:
      - echo '{{.imageDir}}'  
MuriloGhignatti commented 5 months ago

This is also happening to me, the following doesn't work as well:

vars:
  TEST: "{{.USER_WORKING_DIR | toSlash}}"

dotenv:
  - ".env"

tasks:
  test:
    desc: "A test task"
    cmds:
      - echo 'This is a test!'
lukasphase commented 3 weeks ago

This works as expected when defining vars at the task level:

dotenv:
  - sandwich.env

vars:
  SANDWICH: '{{ env "SANDWICH" | default .DEFAULT_SANDWICH }}.sandwich'

tasks:
  sandwich:
    vars:
      SANDWICH_PATH: '{{ joinPath .ROOT_DIR .SANDWICH_DIR .SANDWICH | osClean }}'
    cmds:
      - echo {{ relPath .ROOT_DIR .SANDWICH_PATH | toSlash }}
$ task sandwich
task: [sandwich] echo sandwiches/cheese-and-pickle.sandwich
sandwiches/cheese-and-pickle.sandwich

It breaks when a template pipeline is used at the global var level:

dotenv:
  - sandwich.env

vars:
  SANDWICH: '{{ env "SANDWICH" | default .DEFAULT_SANDWICH }}.sandwich'
  SANDWICH_PATH: '{{ joinPath .ROOT_DIR .SANDWICH_DIR .SANDWICH | osClean }}'

tasks:
  sandwich:
    cmds:
      - echo {{ relPath .ROOT_DIR .SANDWICH_PATH | toSlash }}
$ task sandwich
template: :1:12: executing "" at <.ROOT_DIR>: invalid value; expected string

sandwich.env for reference:

SANDWICH_DIR=sandwiches
DEFAULT_SANDWICH=cheese-and-pickle