Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

T12 Implement env-free config resolution (implements ADR-0017)

Question

The 14 unsafe blocks in crates/cerebro-core/src/lib.rs are edition-2024 std::env::set_var/remove_var calls in XDG-config tests, serialized by an ENV_LOCK mutex. Until they’re gone, forbid(unsafe_code) from T08 cannot land.

Refactor config-path resolution to take an injected environment (a small Env lookup: a closure or &BTreeMap<String,String>) instead of reading std::env internally. Production passes a thin std::env adapter (no unsafe, reading var is safe, only the setters are unsafe); tests pass a literal map. The ENV_LOCK and the unsafe setters disappear.

Exit predicate

rg '\bunsafe\s*\{' crates/cerebro-core/src/lib.rs returns nothing; config tests pass without touching process env; then flip forbid(unsafe_code) in [workspace.lints.rust] (closes the T08 defer). ADR-0017 accepted.

Resolution

Done 2026-10-31, implementing ADR-0017 as written.

cerebro-core gained an Env trait (var + home_dir; the latter because dirs::home_dir itself consults $HOME, so injecting only var would leave ambient state in play) and a ProcessEnv production adapter. xdg_config_dir, default_config_path, and resolve_config_path keep their zero-argument public signatures by delegating to new _with variants that take &dyn Env; cerebro, cerebro-tui, and cerebro-mcp call sites are untouched, so the ADR-0014 single-resolution property holds by construction.

The seven ENV_LOCK tests became nine FakeEnv tests over literal maps plus real tempdirs (XDG set / unset / empty-string, home fallback, the joined documented path, explicit override, existing-file preference, CORTEX_PATH fallback, last-resort relative) - coverage is a superset: empty-value-as-unset was never tested before. ENV_LOCK, env_guard, all fourteen unsafe blocks, and the paired-restore discipline are gone; unsafe_code = "forbid" is now live in [workspace.lints.rust] with the T08 hold lifted, and the workspace compiles under it.

Regression guard per Definition of Done: the injected tests would have failed against the old implementation (it reads real env), and cargo test --workspace + just quality + cerebro status all green.