Repository navigation
🐛 fix(envfile): resolve --envfile from the cwd first - #332
Merged
gaborbernat merged 2 commits intoNov 17, 2026
Merged
Conversation
pytest picks the rootdir before plugins register their options, so the value of a separate `--envfile ../ci.env` argument counted as a path and moved the rootdir to the parent of the project. Joining the same relative value to that rootdir then opened a file one directory above the one the command line named. The value now resolves against the invocation directory and falls back to the rootdir, which keeps a root-relative name working from a subdirectory. Reported by Anthropic's OSS Scanner: ANT-2026-ZUMAZHJI. Fixes pytest-dev#321
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.
_load_env_filesjoined the--envfilevalue to the rootdir (old line). When the value was its own argument and pointed outside the invocation directory, as inpytest --envfile ../ci.env, pytest had already moved the rootdir to the parent of the project, and the join opened a file one directory above the one the command named. The tests then ran with variables from a file nobody selected. 🐛 After this PR the value resolves against the invocation directory first. Fixes #321.pytest moved the rootdir before the option existed
pytest selects the rootdir from the arguments it cannot attribute to an option, and it does so before it loads plugin entry points. At that point
--envfileis unknown, so../ci.envcounts as a path.get_dirs_from_argskeeps the directory of every argument that exists, anddetermine_setuproots the session at the common ancestor of that directory and the invocation directory when it finds no configuration file there.--envfile=../ci.envkeeps the value attached to the option, which is why that spelling loaded the named file._load_env_filesnow tries the invocation directory, then the rootdir, and takes the first candidate that is a file. The rootdir fallback keeps a root-relative name working from a subdirectory. Configuredenv_filesstill resolve against the rootdir alone.flowchart LR A["pytest --envfile ../ci.env<br/>run in work/project"] --> R[pytest roots the session at work/] R -- before --> B["rootdir / ../ci.env<br/>opens ci.env above work/"] R -- now --> I{"invocation dir / ../ci.env<br/>is a file?"} I -- yes --> N[opens work/ci.env] I -- no --> F[rootdir / name, else FileNotFoundError] classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000; classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332; class B bad; class N,F ok;Behavior changes
--envfile PATHand--envfile +PATHname a file relative to the directory pytest was invoked from. When that file does not exist, the path is tried against the rootdir as before, and a miss in both places raisesFileNotFoundError. A run from a subdirectory that holds a file with the same relative name as one in the rootdir now loads the subdirectory's file. The project's own configuration file is still not selected in a run where pytest moved the rootdir; that happens in pytest and this PR does not change it. The README and the threat model describe the new order.Reported by Anthropic's OSS Scanner: ANT-2026-ZUMAZHJI.