Skip to content

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

Closed

Description

@gaborbernat

Passing an empty value to --envfile, as pytest --envfile "$CI_ENV_FILE" does when the variable is unset, behaves like not passing the option. pytest loads the env_files from the configuration and can exit 0, where the README says a CLI file that does not exist raises FileNotFoundError.

Affected code

Mechanism

_load_env_files tests the option's truthiness. The option defaults to None, and --envfile "" stores an empty string, which is falsy too. The function then yields the configured files and returns before the existence check. --envfile + takes the other branch and raises, because the name after the + is checked.

Reproduction

repro.sh

cd "$(mktemp -d)"
printf '[pytest]\nenv_files = .env\n' > pytest.ini
echo 'DATABASE_URL=postgres://localhost/dev' > .env
printf 'import os\n\n\ndef test_url() -> None:\n    print(os.environ.get("DATABASE_URL"))\n' > test_url.py
pytest -q -s --envfile missing.env 2>&1 | tail -n 1
pytest -q -s --envfile ""

Run with sh repro.sh at 3ebb6b4872fd0e450b6670d529e375e11c218e61, with pytest 9.2.0, python-dotenv 1.2.4 and CPython 3.14 on Linux:

FileNotFoundError: Environment file not found: missing.env
postgres://localhost/dev
.
1 passed in 0.00s

Expected: --envfile "" fails like --envfile missing.env does.

Impact

A CI script that builds the argument from an unset variable runs its tests against the configured .env files and reports success.

  • Reproducibility: easy. One command with default options on any platform.
  • Real-world exposure: niche. It needs an empty argument, which in practice comes from an unset shell variable.

Credits

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

Activity

  1. added
    bug
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions