Skip to content

fix: Inconsistent Variable Expansion When Using {{.VAR}} in .env Files with Nested Variables#2612

Open
majiayu000 wants to merge 1 commit intogo-task:mainfrom
majiayu000:fix-1847-inconsistent-variable-expansio-1230-0908
Open

fix: Inconsistent Variable Expansion When Using {{.VAR}} in .env Files with Nested Variables#2612
majiayu000 wants to merge 1 commit intogo-task:mainfrom
majiayu000:fix-1847-inconsistent-variable-expansio-1230-0908

Conversation

@majiayu000
Copy link

Summary

This PR fixes #1847

Changes

  • taskfile/dotenv.go
  • taskfile/dotenv_test.go
  • variables.go

Fixed non-deterministic iteration order when processing dotenv files by
introducing ReadDotenvOrdered which reads key-value pairs in the order
they appear in the file. This prevents race conditions when variables
reference each other with template syntax like {{.VAR}}.

The issue occurred because godotenv.Read() returns a map[string]string,
and Go map iteration order is non-deterministic. This caused inconsistent
variable expansion when nested variables depended on the order of
processing.

Signed-off-by: majiayu000 <1835304752@qq.com>
@trulede trulede self-assigned this Mar 1, 2026
@Bannini
Copy link

Bannini commented Mar 2, 2026

(ignore my previous comment, i was on the main branch...)
I have cloned and can confirm that the fix works for processing variables used in .env files e.g.

ENVIRONMENT=myenv
CONCATVAR='{{.ENVIRONMENT}}-concat'

A task to run the cmds:

      - 'echo Environment: {{.ENVIRONMENT}}'
      - 'echo CONTACVAR: {{.CONCATVAR}}'

work consistently and as expected now.

}

envs, err := godotenv.Read(dotEnvPath)
envs, err := ReadDotenvOrdered(dotEnvPath)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to parse directly into the ast.Vars object. Something like this:

err := ReadFromDotenv(env, dotEnvPath)

That would simply things significantly (all the key/slice handling).

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.

Inconsistent Variable Expansion When Using {{.VAR}} in .env Files with Nested Variables

3 participants