Skip to content

🐛 fix(envfile): resolve --envfile from the cwd first - #332

Merged
gaborbernat merged 2 commits into
pytest-dev:mainfrom
gaborbernat:fix/envfile-invocation-dir
Nov 17, 2026
Merged

gaborbernat merged 2 commits into
pytest-dev:mainfrom
gaborbernat:fix/envfile-invocation-dir

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

_load_env_files joined the --envfile value to the rootdir (old line). When the value was its own argument and pointed outside the invocation directory, as in pytest --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 --envfile is unknown, so ../ci.env counts as a path. get_dirs_from_args keeps the directory of every argument that exists, and determine_setup roots the session at the common ancestor of that directory and the invocation directory when it finds no configuration file there. --envfile=../ci.env keeps the value attached to the option, which is why that spelling loaded the named file.

_load_env_files now 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. Configured env_files still 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;
Loading

Behavior changes

--envfile PATH and --envfile +PATH name 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 raises FileNotFoundError. 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.

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
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit 9235cda into pytest-dev:main Nov 17, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the fix/envfile-invocation-dir 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.

--envfile ../file as a separate argument loads a file one directory too high

1 participant