Skip to content

fix(config): stop loading the developer's real config in default tests - #375

Merged
github-actions[bot] merged 1 commit into
mainfrom
fix/hermetic-config-defaults-test
Aug 16, 2026
Merged

github-actions[bot] merged 1 commit into
mainfrom
fix/hermetic-config-defaults-test

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

The bug

crates/forge-config's tests::defaults_workspace_to_cwd fails on any machine where the developer has a personal Forge config:

assertion `left == right` failed
  left: "system"
 right: "forge-dark"

It is not an environment quirk — the test is non-hermetic by construction. Config::load merges the trusted user layer from ~/.../forge/config.toml first:

if let Some(user) = user_config_path() {
    if user.is_file() {
        merge_file(&mut cfg, &user, ConfigScope::Trusted)?;
    }
}

…but EnvGuard::clear_forge_env() only cleared the FORGE_* variables. So every assertion about default values in this module was silently layered over the developer's own settings. A two-line [tui] theme = "system" in my config dir was enough to trip it. CI has no user config, so it stayed green and the failure looked local-only.

21 tests use this guard. Only one happened to assert a value my config overrode; the rest were passing by luck.

The fix

EnvGuard now isolates everything Config::load reads from outside the repository — the FORGE_* vars and the platform config dir. It owns a TempDir, clears XDG_CONFIG_HOME, and points HOME at it, which lands dirs::config_dir() inside the temp dir on both Linux and macOS. All 21 tests become hermetic, not just the one that broke.

This is not a new pattern: permissions.rs already has IsolatedUserConfig doing exactly this, with a comment explaining that it shares the crate-wide ENV_LOCK with lib.rs's guards because rustc runs tests in parallel threads. The unified guard takes the same lock, so the two remain safe against each other.

That also makes XdgEnvGuard redundant — it existed solely to widen EnvGuard with HOME/XDG_CONFIG_HOME for one test. Removed; its sole user now gets the isolated directory from EnvGuard::user_config_dir().

Guarding the guard

A guard that isolated nothing would make every test pass, so the new test pins both directions:

  1. Seed a user config that changes a default, and assert it is merged — without this half, the test proves nothing.
  2. Take a fresh guard (fresh temp dir) and assert the default is back.

One detail worth recording: the first draft seeded theme = "not-the-default" and failed, because normalize_theme_id maps an unknown id back to DEFAULT_THEME_ID. The seed has to be a real alternate theme (THEME_SOLARIZED_DARK) for the positive half to mean anything.

Verification

  • cargo test --workspace — fully green, zero failures. This was the last failing test in the workspace on this machine.
  • cargo fmt --all and cargo clippy --all-targets clean.
  • defaults_workspace_to_cwd passes with the offending ~/Library/Application Support/forge/config.toml still on disk, so the fix is the isolation and not a cleaned-up environment.

No production code changed — this is test isolation only.

`Config::load` merges the trusted user layer from
`~/.../forge/config.toml` before anything else, but `EnvGuard` only
cleared the `FORGE_*` env vars. Every assertion in this module about
default values was therefore layered over whatever the developer running
the suite happened to have in their own config, while CI — which has no
user config — stayed green.

A personal `[tui] theme = "system"` was enough to fail
`defaults_workspace_to_cwd` locally with `left: "system", right:
"forge-dark"`. The test was not wrong about the default; it was reading
someone's settings.

`EnvGuard` now also redirects the platform config dir into a temp dir it
owns (clearing `XDG_CONFIG_HOME` and pointing `HOME` at it, covering
Linux and macOS), so all 21 tests that use it are hermetic rather than
just the one that happened to trip. This mirrors `permissions.rs`'s
existing `IsolatedUserConfig` and shares the same crate-wide `ENV_LOCK`,
since rustc runs tests in parallel threads.

That makes `XdgEnvGuard` redundant — its sole user now takes the isolated
config dir from the unified guard via `user_config_dir()`.

Adds a test that pins the guard itself: it seeds a user config that
changes a default, proves it is merged, then proves a separate guarded
load does not see it. Without the positive half, a guard that isolated
nothing would still pass.
@github-actions
github-actions Bot enabled auto-merge (squash) August 16, 2026 21:20
@github-actions
github-actions Bot merged commit ca7eec4 into main Aug 16, 2026
7 checks passed
mohitranka added a commit that referenced this pull request Aug 21, 2026
#375)

`Config::load` merges the trusted user layer from
`~/.../forge/config.toml` before anything else, but `EnvGuard` only
cleared the `FORGE_*` env vars. Every assertion in this module about
default values was therefore layered over whatever the developer running
the suite happened to have in their own config, while CI — which has no
user config — stayed green.

A personal `[tui] theme = "system"` was enough to fail
`defaults_workspace_to_cwd` locally with `left: "system", right:
"forge-dark"`. The test was not wrong about the default; it was reading
someone's settings.

`EnvGuard` now also redirects the platform config dir into a temp dir it
owns (clearing `XDG_CONFIG_HOME` and pointing `HOME` at it, covering
Linux and macOS), so all 21 tests that use it are hermetic rather than
just the one that happened to trip. This mirrors `permissions.rs`'s
existing `IsolatedUserConfig` and shares the same crate-wide `ENV_LOCK`,
since rustc runs tests in parallel threads.

That makes `XdgEnvGuard` redundant — its sole user now takes the isolated
config dir from the unified guard via `user_config_dir()`.

Adds a test that pins the guard itself: it seeds a user config that
changes a default, proves it is merged, then proves a separate guarded
load does not see it. Without the positive half, a guard that isolated
nothing would still pass.
mohitranka added a commit that referenced this pull request Sep 11, 2026
#375)

`Config::load` merges the trusted user layer from
`~/.../forge/config.toml` before anything else, but `EnvGuard` only
cleared the `FORGE_*` env vars. Every assertion in this module about
default values was therefore layered over whatever the developer running
the suite happened to have in their own config, while CI — which has no
user config — stayed green.

A personal `[tui] theme = "system"` was enough to fail
`defaults_workspace_to_cwd` locally with `left: "system", right:
"forge-dark"`. The test was not wrong about the default; it was reading
someone's settings.

`EnvGuard` now also redirects the platform config dir into a temp dir it
owns (clearing `XDG_CONFIG_HOME` and pointing `HOME` at it, covering
Linux and macOS), so all 21 tests that use it are hermetic rather than
just the one that happened to trip. This mirrors `permissions.rs`'s
existing `IsolatedUserConfig` and shares the same crate-wide `ENV_LOCK`,
since rustc runs tests in parallel threads.

That makes `XdgEnvGuard` redundant — its sole user now takes the isolated
config dir from the unified guard via `user_config_dir()`.

Adds a test that pins the guard itself: it seeds a user config that
changes a default, proves it is merged, then proves a separate guarded
load does not see it. Without the positive half, a guard that isolated
nothing would still pass.
mohitranka added a commit that referenced this pull request Sep 11, 2026
#375)

`Config::load` merges the trusted user layer from
`~/.../forge/config.toml` before anything else, but `EnvGuard` only
cleared the `FORGE_*` env vars. Every assertion in this module about
default values was therefore layered over whatever the developer running
the suite happened to have in their own config, while CI — which has no
user config — stayed green.

A personal `[tui] theme = "system"` was enough to fail
`defaults_workspace_to_cwd` locally with `left: "system", right:
"forge-dark"`. The test was not wrong about the default; it was reading
someone's settings.

`EnvGuard` now also redirects the platform config dir into a temp dir it
owns (clearing `XDG_CONFIG_HOME` and pointing `HOME` at it, covering
Linux and macOS), so all 21 tests that use it are hermetic rather than
just the one that happened to trip. This mirrors `permissions.rs`'s
existing `IsolatedUserConfig` and shares the same crate-wide `ENV_LOCK`,
since rustc runs tests in parallel threads.

That makes `XdgEnvGuard` redundant — its sole user now takes the isolated
config dir from the unified guard via `user_config_dir()`.

Adds a test that pins the guard itself: it seeds a user config that
changes a default, proves it is merged, then proves a separate guarded
load does not see it. Without the positive half, a guard that isolated
nothing would still pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant