Skip to content

fix: expand task-level dotenv in brackets#1627

Open
aminya wants to merge 2 commits intogo-task:mainfrom
aminya:task-dotenv
Open

fix: expand task-level dotenv in brackets#1627
aminya wants to merge 2 commits intogo-task:mainfrom
aminya:task-dotenv

Conversation

@aminya
Copy link
Copy Markdown
Contributor

@aminya aminya commented Apr 28, 2024

Fixes #997

Task level dotenv didn't expand the variables referenced by {{.var}} previously This fixes the issue. I added a test for it to prevent the bug from happening again.

Comment on lines 8 to +19
dotenv: ['.env']
cmds:
- echo "$FOO" > dotenv.txt
- echo "{{.FOO}}" > dotenv-2.txt
- task: dotenv_called
vars:
FOO: "{{.FOO}}"

dotenv_called:
cmds:
- echo "$FOO" > dotenv-called.txt
- echo "{{.FOO}}" > dotenv-called-2.txt
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR, this variable forwarding finally works. There's still room for improvement if the dotenv vars should be automatically propagated to any underlying task. But that could be handled separately.

@chimbori
Copy link
Copy Markdown

chimbori commented Jun 2, 2024

This PR solves an issue I am running into (thank you @aminya)!

For the maintainers: is there a plan or timeline for when this will be merged? Or is there another action required here before it can be considered ready? Thank you!

@dmitry-mightydevops
Copy link
Copy Markdown

can someone review and merge?

@EvilCheetah
Copy link
Copy Markdown

I can confirm this issue: the Taskfile doesn't expand environment variables from the dotenv files that are specified at the task level. They do work when placed at the root level, but not within individual tasks. I’ve attempted multiple approaches in line with the Documentation | .env files, yet the variables still aren’t being expanded as expected.

My Taskfile version: Task version: 3.42.1 (Homebrew)

Context

.env File Structure:

# Database Configuration
DB_SCHEME=postgres
DB_USERNAME=postgres
DB_PASSWORD=Qk3doyOiZcNpDh9
DB_HOST=localhost
DB_PORT=5555
DB_DATABASE=postges
DB_SSL=disable

Working Setup (Root-Level .env)

version: '3'

vars:
    COMPILER: go

dotenv: ['./environment/.test.env']

tasks:
    test:
        cmds:
            - |
              docker run                                       \
                    --name test_database                       \
                    --publish {{ .DB_PORT }}:5432              \
                    --env POSTGRES_USER={{ .DB_USERNAME }}     \
                    --env POSTGRES_PASSWORD={{ .DB_PASSWORD }} \
                    --env POSTGRES_DB={{ .DB_DATABASE }}       \
                    --detach                                   \
                    postgres:16-alpine

Output:

task: [test] docker run                                       \
      --name test_database                       \
      --publish 5555:5432              \
      --env POSTGRES_USER=postgres     \
      --env POSTGRES_PASSWORD=Qk3doyOiZcNpDh9 \
      --env POSTGRES_DB=postgres       \
      --detach                                   \
      postgres:16-alpine

Failing Setup (Command Level .env) - Regular Expansion

Taskfile.yaml:

version: '3'

vars:
    COMPILER: go


tasks:
    test:
        dotenv: ['./environment/.test.env']
        cmds:
            - |
              docker run                                       \
                    --name test_database                       \
                    --publish {{ .DB_PORT }}:5432              \
                    --env POSTGRES_USER={{ .DB_USERNAME }}     \
                    --env POSTGRES_PASSWORD={{ .DB_PASSWORD }} \
                    --env POSTGRES_DB={{ .DB_DATABASE }}       \
                    --detach                                   \
                    postgres:16-alpine

Output:

task: [test] docker run                                       \
      --name test_database                       \
      --publish :5432              \
      --env POSTGRES_USER=     \
      --env POSTGRES_PASSWORD= \
      --env POSTGRES_DB=       \
      --detach                                   \
      postgres:16-alpine

Failing Setup (Command Level .env) - Environment Expansion(from Docs)

version: '3'

vars:
    COMPILER: go


tasks:
    test:
        dotenv: ['./environment/.test.env']
        cmds:
            - |
              docker run                                       \
                    --name test_database                       \
                    --publish $DB_PORT:5432              \
                    --env POSTGRES_USER=$DB_USERNAME     \
                    --env POSTGRES_PASSWORD=$DB_PASSWORD \
                    --env POSTGRES_DB=$DB_DATABASE       \
                    --detach                                   \
                    postgres:16-alpine

Output:

task: [test] docker run                                       \
      --name test_database                       \
      --publish $DB_PORT:5432              \
      --env POSTGRES_USER=$DB_USERNAME     \
      --env POSTGRES_PASSWORD=$DB_PASSWORD \
      --env POSTGRES_DB=$DB_DATABASE       \
      --detach                                   \
      postgres:16-alpine

@EvilCheetah
Copy link
Copy Markdown

Currently, one way to reference a different environment file at the command level is to define a separate Taskfile pointing to the desired .env file, then invoke that Taskfile from the main Taskfile. However, this method may feel somewhat redundant.


Current workaround

Taskfile.yaml:

version: '3'

vars:
    COMPILER: go

dotenv: ['./environment/.application.env']

tasks:
    run:
        cmds:
            - go run ./cmd/main.go

    test:
        cmds:
            - task --taskfile test.taskfile.yaml test

test.taskfile.yaml:

version: '3'


dotenv: ['./environment/.test.env']


tasks:
    test:
        cmds:
            - |
              docker run                                       \
                    --name test_database                       \
                    --publish {{ .DB_PORT }}:5432              \
                    --env POSTGRES_USER={{ .DB_USERNAME }}     \
                    --env POSTGRES_PASSWORD={{ .DB_PASSWORD }} \
                    --env POSTGRES_DB={{ .DB_DATABASE }}       \
                    --detach                                   \
                    postgres:16-alpine
            - go test ./...
            - docker rm -f test_database

@dalaen
Copy link
Copy Markdown

dalaen commented Nov 5, 2025

Is there any way to move this PR forward?

@aminya
Copy link
Copy Markdown
Contributor Author

aminya commented Nov 5, 2025

I've had only 25% chance of merging contributions to go-task. The probability is low.
https://github.com/go-task/task/pulls?q=sort%3Aupdated-desc+is%3Apr+is%3Aopen+author%3Aaminya

@emmekappa
Copy link
Copy Markdown

any news on this PR ?

@trulede trulede self-assigned this Mar 1, 2026
@trulede
Copy link
Copy Markdown
Contributor

trulede commented Mar 1, 2026

This PR is changing the behaviour of Task. That may not be a bad thing, but its a change I don't think people have fully appreciated.

@trulede trulede added area: variables Changes related to variables. area: env Changes related to environment variables. breaking change Changes that are not backward compatible. labels Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: env Changes related to environment variables. area: variables Changes related to variables. breaking change Changes that are not backward compatible.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotenv not working at task level

7 participants