Skip to content

{{.USER_WORKING_DIR}} evaluated after dotenv #1312

@ttudre977

Description

@ttudre977

I'm assuming that the issue is due to the order of evaluation at startup.

The following dotenv directive, does not source in the local .env, when the Taskfile.yaml resides in a parent directory (no local Taskfile.yaml), and using the {{.USER_WORKING_DIR}} in the path to the .env file.

dotenv:
    - "{{.USER_WORKING_DIR}}/.env" 

I'm trying to use a parent directory Taskfile.yaml for multiple environments:

.
├── Taskfile.yaml
├── env1
│   ├── .env
│   ├── myscript1.sh
├── env2
│   ├── .env
│   ├── myscript2.sh
├── env3
│   ├── .env
│   ├── myscript3.sh

The use case is to cd into any one of the env* directories, then run task X:

>cd env1
>task mytask

Where env1/.env contents are something like:

DOTENV_VAR="env1 specific value"

and where env2/.env contents are something like:

DOTENV_VAR="env2 specific value"

etc.

# Taskfile.yaml:
---
version: "3"

dotenv:
  - "env1/.env"  # works, but static, need to evaluate based on dir from which `task` command was run (child env1,2,3 dir).
  # - "<full_dir_path_to_env1>/.env"  # works
  # Options below, DO NOT WORK
  # - "{{.USER_WORKING_DIR}}/.env"  # does NOT work


tasks:
    mytask:
        dir: "{{.USER_WORKING_DIR}}"
        env:
          DEV: '{{.DOTENV_VAR}}'
        cmds:
          - echo "my local env DOTENV_VAR = $DEV"
...

The trick I'm using to get around this for now is that from #1008 (comment) :

tasks:
    mytask:
        dir: "{{.USER_WORKING_DIR}}"
        cmds:
            - |
              dotenv -f "{{.USER_WORKING_DIR}}/.env" run -- bash -c \
                'echo "getting the dotenv the hard way, LOCAL_DOTENV_VAR = [$DOTENV_VAR]"'

OR

tasks:
    mytask:
        dir: "{{.USER_WORKING_DIR}}"
        vars:
          LDV_BY_VARS:
            sh: dotenv -f "{{.USER_WORKING_DIR}}/.env" get DOTENV_VAR
    
        cmds:
          - echo ".env LDV_BY_VARS = [{{.LDV_BY_VARS}}]"
  • Task version: 3.27.1
  • Operating system: MacOS Ventura 13.3.1 (a) (22E772610a)
  • Experiments enabled: No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No fields configured for bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions