Skip to content

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

Closed

Description

@gaborbernat

With env_files_skip_if_set = true, a variable that is already set keeps its value, and a ${VAR} reference to it later in the same .env file expands to the file's value, the one that was skipped. The tests see a DB_HOST and a DATABASE_URL that disagree.

Affected code

Mechanism

_apply_env_file reads the file with dotenv_values, which interpolates with override=True. In that mode resolve_variables lets a value assigned earlier in the file win over os.environ, so ${DB_HOST} becomes localhost. pytest-env applies the skip rule afterwards, one key at a time, on values that are already expanded.

Reproduction

repro.sh

cd "$(mktemp -d)"
printf '[pytest]\nenv_files = .env\nenv_files_skip_if_set = true\n' > pytest.ini
printf 'DB_HOST=localhost\nDATABASE_URL=postgres://${DB_HOST}/app\n' > .env
cat > test_env.py <<'PY'
import os


def test_env() -> None:
    print({name: os.environ.get(name) for name in ("DB_HOST", "DATABASE_URL")})
PY
DB_HOST=ci-db.internal pytest -q -s

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

{'DB_HOST': 'ci-db.internal', 'DATABASE_URL': 'postgres://localhost/app'}
.
1 passed in 0.00s

Expected: DATABASE_URL is postgres://ci-db.internal/app, built from the value that was kept.

Impact

A derived variable points at the host the skip rule was meant to replace.

  • Reproducibility: moderate. It needs env_files_skip_if_set, a variable set before pytest starts, and a later line in the same file that references it.
  • Real-world exposure: niche. CI jobs that export a few variables and keep the rest in .env meet it when one line builds on another.

Credits

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

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