Skip to content

🐛 fix(dotenv): expand references after skip-if-set - #340

Merged
gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/skip-if-set-interpolation
Nov 17, 2026
Merged

gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/skip-if-set-interpolation

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

_apply_env_file read each .env file with dotenv_values and applied env_files_skip_if_set afterwards, key by key (old lines). The values were already expanded by then, with the file's own assignments taking priority. With DB_HOST=ci-db.internal exported and a file holding DB_HOST=localhost and DATABASE_URL=postgres://${DB_HOST}/app, the tests saw the exported DB_HOST and a DATABASE_URL that pointed at localhost. 🐛 After this PR the reference expands to the value that was kept. Fixes #327.

Expansion ran before the skip rule

dotenv_values interpolates with override=True, and in that mode resolve_variables lets a value assigned earlier in the file win over os.environ. pytest-env then skipped DB_HOST and kept the already expanded DATABASE_URL.

With the option on, _apply_env_file now reads the raw values and expands each one when it applies it, against os.environ as it stands, using python-dotenv's own parse_variables. A skipped key is not expanded. With the option off, the file is read with dotenv_values as before.

Behavior changes

Under env_files_skip_if_set = true, a ${VAR} reference sees the value the variable has in the session: the kept one when the file's assignment was skipped, the file's when it was applied. ${VAR:-default} works as before. One edge differs under the option: when a file assigns the same key twice, a reference between the two assignments now expands to the last assignment, because python-dotenv returns one value per key. Files read without the option are not affected. dotenv.variables is not part of python-dotenv's documented API, so a future release of it could require a change here.

Reported by Anthropic's OSS Scanner: ANT-2026-5JX2GCSN.

python-dotenv expanded ${VAR} while reading a file, letting the file's own
earlier assignment win over the environment, and only then did
env_files_skip_if_set decide which keys to keep. A variable the rule kept
and a second one built from it therefore disagreed: DB_HOST stayed at the
exported host while DATABASE_URL pointed at the host from the file.

With the option on, each value is now expanded when it is applied, against
the environment as it stands. Without it the file is read as before.

Reported by Anthropic's OSS Scanner: ANT-2026-5JX2GCSN.

Fixes pytest-dev#327
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit 3e7d5d2 into pytest-dev:main Nov 17, 2026
20 checks passed
@gaborbernat
gaborbernat deleted the fix/skip-if-set-interpolation branch November 17, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

env_files_skip_if_set expands ${VAR} with the skipped .env value

1 participant