Skip to content

Conversation

@aminya
Copy link
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
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

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

can someone review and merge?

@EvilCheetah
Copy link

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

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

dalaen commented Nov 5, 2025

Is there any way to move this PR forward?

@aminya
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotenv not working at task level

5 participants