Skip to content

🐛 fix(dotenv): keep earlier file values with skip-if-set - #364

Closed
draarivpatel-ui wants to merge 2 commits into
pytest-dev:mainfrom
draarivpatel-ui:fix-dotenv-skip-earlier-files
Closed

draarivpatel-ui wants to merge 2 commits into
pytest-dev:mainfrom
draarivpatel-ui:fix-dotenv-skip-earlier-files

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

With env_files_skip_if_set = true 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. 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

[tool.pytest_env]
env_files = [".env", ".env.test"]
env_files_skip_if_set = true
# .env
MAGIC=from_first
# .env.test
MAGIC=from_second
EXPANDED=${MAGIC}_suffix

Before this change pytest sees MAGIC=from_second while EXPANDED=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 .env is applied MAGIC is defined in the session, so the first file should win: MAGIC=from_first and EXPANDED=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 preexisting snapshot 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 --check and ty check are clean.

Thanks for contributing, a couple of things to double check:

  1. Make sure the tests pass on your end.
  2. Update the documentation (an inline type hint/docstring). Not needed, behavior now matches the documented option semantics.
  3. Add a changelog fragment. Done.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

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.

3 participants