Skip to content

🐛 fix(config): set_env override lost after load#3775

Merged
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:3773
Feb 19, 2026
Merged

🐛 fix(config): set_env override lost after load#3775
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:3773

Conversation

@gaborbernat
Copy link
Copy Markdown
Member

When set_env both inherits via cross-section substitution (e.g., {[testenv]set_env}) and explicitly overrides the same variable in the same section, the explicit value is silently replaced by the inherited one. 🐛 This regression was introduced in v4.39.0 and breaks a common configuration pattern where users layer environment variables across sections.

The root cause is a timing issue in _iter_needs_replacement(). During iteration, load() moves explicitly-set keys from _raw to _materialized. By the time the deferred cross-section substitution expands, the key not in self._raw guard no longer sees those keys, so inherited values overwrite explicit ones. The fix tracks originally-defined keys in a separate _defined_keys set that persists across load() calls, ensuring deferred substitutions never overwrite values the user explicitly configured.

All existing precedence behaviors are preserved: defaults still get overridden by cross-section values (#2872), explicit values still win over cross-section values (#2831), and file| ordering still respects later inline definitions (#3335).

Fixes #3773

When `set_env` inherits via cross-section substitution and explicitly
overrides the same key, `load()` moves the key from `_raw` to
`_materialized` during iteration. By the time `_iter_needs_replacement`
runs, the `key not in self._raw` guard no longer protects the explicit
value, so the inherited one silently wins.

Track originally-defined keys in a separate `_defined_keys` set that
persists across `load()` calls, so deferred substitutions never
overwrite values the user explicitly configured in the same section.

Fixes tox-dev#3773
@gaborbernat gaborbernat added bug:normal affects many people or has quite an impact area:configuration labels Feb 19, 2026
@gaborbernat gaborbernat enabled auto-merge (squash) February 19, 2026 05:05
@gaborbernat gaborbernat merged commit 5929bdb into tox-dev:main Feb 19, 2026
28 checks passed
@gaborbernat gaborbernat deleted the 3773 branch February 20, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:configuration bot:chronographer:provided bug:normal affects many people or has quite an impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tox 4.39.0 doesn't override setenv variables properly

1 participant