Repository navigation
🐛 fix(dotenv): keep earlier file values with skip-if-set - #364
Closed
draarivpatel-ui wants to merge 2 commits into
Closed
draarivpatel-ui wants to merge 2 commits into
draarivpatel-ui wants to merge 2 commits into
Conversation
With env_files_skip_if_set enabled and several files in env_files, the
skip rule compared each file against the environment as it stood before
any file loaded. A variable assigned by an earlier file did not count as
existing, so a later file overwrote it and ${VAR} references in the
later file expanded with the earlier file's value while the variable
itself ended up with the later one. Each file now counts variables set
by earlier files as existing: the first file wins, matching the kept
value semantics of the option, and references expand to the kept value.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
env_files_skip_if_set = trueand several files inenv_files, the skip rule compared each file against the environment as it stood before any file loaded. A variable assigned by an earlier file did not count as existing, so a later file overwrote it. Worse,${VAR}references in the later file expanded with the earlier file's value while the variable itself ended up with the later file's one, an inconsistent mix.Reproducer
# .env MAGIC=from_firstBefore this change pytest sees
MAGIC=from_secondwhileEXPANDED=from_first_suffix. The skip option exists to keep existing environment variables (its help text says "only set .env file variables when not already defined"), and once.envis appliedMAGICis defined in the session, so the first file should win:MAGIC=from_firstandEXPANDED=from_first_suffix. This matches the session-value semantics established in #340 ("a${VAR}reference sees the value the variable has in the session").Fix
The
preexistingsnapshot is now taken per file instead of once before the loop, so variables set by earlier files count as existing when later files load. No behavior changes for single-file setups, for files loaded with the option off, or for variables the caller exported.The regression test fails without the fix and passes with it; full suite is 196 passed with 100% coverage, and
ruff check,ruff format --checkandty checkare clean.Thanks for contributing, a couple of things to double check: