task icon indicating copy to clipboard operation
task copied to clipboard

Unable to override dotenv file loaded using $ENV value, as per the example in the docs

Open toby-griffiths opened this issue 1 year ago • 1 comments

  • Task version: Task version: v3.36.0 (h1:XVJ5hQ5hdzTAulHpAGzbUMUuYr9MUOEQFOFazI3hUsY=)
  • Operating system: MacOS 14.1.2
  • Experiments enabled…
* GENTLE_FORCE:         off
* REMOTE_TASKFILES: off
* ANY_VARIABLES:    off

Following the example in this docs here I've created the following files

# Taskfile
version: '3'

env:
    ENV: testing

dotenv: ['.env', '{{.ENV}}/.env']

tasks:
    greet:
        cmds:
            - echo "Using $KEYNAME and endpoint $ENDPOINT"
# .env
KEYNAME=VALUE
# testing/.env
ENDPOINT=testing.com
# wibble/.env
ENDPOINT=wibble.com

If I run task greet, I get…

$ task greet
task: [greet] echo "Using $KEYNAME and endpoint $ENDPOINT"
Using VALUE and endpoint testing.com

If I run ENV=wibble task greet, I get…

$ ENV=wibble task greet
task: [greet] echo "Using $KEYNAME and endpoint $ENDPOINT"
Using VALUE and endpoint testing.com

$ export ENV=wibble
$ task greet
task: [greet] echo "Using $KEYNAME and endpoint $ENDPOINT"
Using VALUE and endpoint testing.com

I would expect that if I set ENV=wibble I should see env vars from the wibble/.env file, but I cannot get this working.

No matter what I try, I cannot get this working other than changing the env value in the Taskfile itself to get the alternative env var values.

Am I missing something obvious, or is this a bug?

toby-griffiths avatar Apr 16 '24 21:04 toby-griffiths

I am experiencing the same bug.

marziply avatar Apr 24 '24 16:04 marziply

Hello @toby-griffiths

To do what you are trying to do, you need to use the default fonction in the go templating. This is working :

# Taskfile
version: '3'

env:
  ENV: '{{.ENV | default "testing"}}'

dotenv: ['.env', '{{.ENV}}/.env']

tasks:
  greet:
    cmds:
      - echo "Using $KEYNAME and endpoint $ENDPOINT"
➜ task greet                                                                                                                                                                                                                                                                               
task: [greet] echo "Using $KEYNAME and endpoint $ENDPOINT"
Using VALUE and endpoint testing.com

➜ ENV=wibble task  greet                                                                                                                                                                                                                                                                   
task: [greet] echo "Using $KEYNAME and endpoint $ENDPOINT"
Using VALUE and endpoint wibble.com

vmaerten avatar May 16 '24 17:05 vmaerten