Skip to content

🐛 fix(envfile): reject an empty --envfile value - #330

Merged
gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/empty-envfile
Nov 17, 2026
Merged

gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/empty-envfile

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

_load_env_files decided whether --envfile was passed by testing the option's truthiness (old line). pytest --envfile "", which is what pytest --envfile "$CI_ENV_FILE" runs when the variable is unset, was therefore handled like a run without the option: pytest loaded the configured env_files and could exit 0. 🐛 After this PR an empty value raises FileNotFoundError, as the README documents for a CLI file that does not exist. Fixes #320.

The empty value was treated as a missing option

The option defaults to None, and argparse stores an empty string for --envfile "". Both are falsy, so the function yielded the configured files and returned before the existence check. _load_env_files now compares the option with None, so the empty name reaches that check, where the rootdir joined with an empty name is a directory and not a file.

Behavior changes

pytest --envfile "" and pytest --envfile= stop with FileNotFoundError: Environment file not found: before collection. A script that relied on an empty value to fall back to the configured files has to drop the option instead. Runs without --envfile and runs with a non-empty value behave as before.

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

`pytest --envfile "$CI_ENV_FILE"` with the variable unset passed an empty
string, which the truthiness test took for a missing option. The run loaded
the configured env files and could pass, where the README promises
FileNotFoundError for a CLI file that does not exist.

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

Fixes pytest-dev#320
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit 7a37a25 into pytest-dev:main Nov 17, 2026
20 checks passed
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.

--envfile "" loads the configured env files instead of failing

1 participant