Skip to content

Make the last dotenv file win over the earlier ones - #42

Merged
llambeau merged 1 commit into
masterfrom
fix/dotenv-file-precedence
Aug 17, 2026
Merged

Make the last dotenv file win over the earlier ones#42
llambeau merged 1 commit into
masterfrom
fix/dotenv-file-precedence

Conversation

@llambeau

Copy link
Copy Markdown
Contributor

dotenv gives priority to the first file that defines a variable when several paths are passed in one call, so config: ['.env.commons', '.env'] made .env.commons shadow .env. Users coming from docker-compose expect the opposite, where entries listed later in env_file override the ones before.

Reverse the list before handing it to dotenv: the last configured file is parsed first and wins the override: false race, while process.env still takes precedence over every file.

The existing tests only covered variables defined in a single file each, so the ordering was never exercised.

dotenv gives priority to the first file that defines a variable when
several paths are passed in one call, so `config: ['.env.commons', '.env']`
made `.env.commons` shadow `.env`. Users coming from docker-compose expect
the opposite, where entries listed later in `env_file` override the ones
before.

Reverse the list before handing it to dotenv: the last configured file is
parsed first and wins the `override: false` race, while process.env still
takes precedence over every file.

The existing tests only covered variables defined in a single file each,
so the ordering was never exercised.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@llambeau
llambeau merged commit 05422a2 into master Aug 17, 2026
4 checks passed
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.

1 participant