Skip to content

computed vars in dotenv paths #2826

Open

Description

@rpf3

Description

When a dotenv path references a variable computed via sh, the template renders incorrectly — the variable appears empty, causing the resolved path to fall back to the task's working directory.

Expected behavior

{{.DOTENV}} resolves to the output of mktemp and the dotenv file at that path is loaded.

Actual behavior

{{.DOTENV}} renders to an empty string. The empty path is resolved against the task's working directory.

$ task
task: Failed to read env file ...: read /home/user/project: is a directory

Related issues

Workaround

Avoid dotenv entirely and source the file explicitly in each task that needs it:

cmds:
  - |
    set -a && source {{.DOTENV}} && set +a

Version

3.48.0

Operating system

Ubuntu 20.04

Experiments Enabled

No response

Example Taskfile

version: '3'

vars:
  DOTENV:
    sh: mktemp

tasks:
  default:
    dotenv:
      - '{{.DOTENV}}'
    cmds:
      - echo "AWS_KEY=$AWS_KEY"

Activity

  1. rpf3 commented on May 7, 2026

    @rpf3
    Author

    If this is unintended, or intended but a possible feature addition, I'd like to assist in fixing/implementing.

  2. trulede commented on May 7, 2026

    @trulede
    Contributor

    In this PR, on the following line, you might be able to expand the path using the templater already created in the function. It should work.

    https://github.com/go-task/task/pull/2711/changes#diff-88a1dcc1be6c418f7d053085cf094890a7a3e4f767c4f8500cf51373589e5bb9R89

    If it does work (will be one line change I think) make a review with the suggestion. Add a test case too if you what, the PR already covers a few cases, so add yours.

  3. rpf3 commented on May 7, 2026

    @rpf3
    Author

    @trulede I have a local branch to work on this but my initial testing is failing. I think the block here

    if _, err := os.Stat(dotEnvPath); os.IsNotExist(err)

    is still causing the dotenv sh to fail because the template didn't resolve the dotenv variable {{.DOTENV}} so it returns the working directory which means it moves on to envs, err := godotenv.Read(dotEnvPath). We could change your conditional to check if dotEnvPath is a directory but I think this would be a little hacky. I feel like what's really called for is that if the template can't resolve {{.DOTENV}}, we want to know about it instead of just giving back a valid path.

  4. trulede commented on May 8, 2026

    @trulede
    Contributor

    I don't think the sh var will be available (at that point in the processing). You could try and set the value at the cli, at which point the var will be available to the templater.

    $ foo=$(mktemp) task sometask

    Then its a question if the dotenv paths are also processed by the templater. If not, see my previous suggestion.

  5. rpf3 commented on May 10, 2026

    @rpf3
    Author

    Running DOTENV=$(mktemp) task on my test file resolves the primary issue; but obviously I'd like to be able to just set this in the Taskfile definition itself.

    Is this in the ballpark of what you were thinking?

    dotEnvPath = templater.Replace(dotEnvPath, cache)
    if dotEnvPath == "" {
    	continue
    }

    If so, I tried to open a PR to your fork branch but I don't think it is open.

  6. added
    area: envChanges related to environment variables.
    and removed
    state: needs triageWaiting to be triaged by a maintainer.
    on May 19, 2026
  7. added theissue type on May 19, 2026
  8. trulede commented on May 19, 2026

    @trulede
    Contributor

    I think your commit (especially the test) is a really good example of how to use the capabilities of task to solve a common workflow.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: envChanges related to environment variables.

    Fields

    No fields configured for feature.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions