17. Forbid unsafe code workspace-wide with env-free config tests
Date: 2026-10-23
Status
Accepted
Relates to 9. Storage trait for dependency injection
Context
The rust-standard lint table (docs/standard/lints.md) lists unsafe_code as forbid, a stricter level than deny,
“no escape at any level”. Adopting that table as-is would fail the build, because cerebro-core currently contains
fourteen unsafe { ... } blocks, all in #[cfg(test)] test code.
The unsafe is real, not vestigial. Since Rust edition 2024, std::env::set_var and std::env::remove_var are unsafe
functions. Cerebro’s XDG config resolution reads XDG_CONFIG_HOME and HOME, so its tests manipulate the process
environment directly. Each test takes a global ENV_LOCK mutex to serialize against other env-mutating tests, then
calls the unsafe setters inside an unsafe block, with a SAFETY comment, and restores the prior value afterward.
This creates two problems. First, adopting the standard’s forbid(unsafe_code) fails immediately. Second, the
global-lock pattern is fragile: it relies on every env-touching test acquiring the same mutex, and it mutates
process-global state that the library reads through std::env::var. The workspace already favors dependency injection
over ambient state (ADR-0009, Storage trait for DI).
Decision
Refactor config-path resolution so tests no longer mutate the process environment. Introduce an explicit environment
source in cerebro-core: a function that resolves the XDG config path from an injected key-value lookup (a closure or a
&BTreeMap<String, String> implementing a small Env trait) rather than calling std::env::var internally. Production
callers pass a thin adapter over std::env; tests pass a literal map, with no set_var, no mutex, and no unsafe.
After the unsafe blocks are gone, adopt the standard’s forbid(unsafe_code) in [workspace.lints.rust]. The forbid
then holds with nothing to escape, matching the standard’s intent.
This is sequenced as its own phase in the rust-standard alignment plan, separate from the general lint-table adoption, because the refactor must land and pass before the forbid can be turned on without breaking CI.
Consequences
The test suite loses the ENV_LOCK mutex and the paired-restore discipline, removing a footgun where a forgotten
restore poisons later tests. Environment handling becomes injectable and testable without global state, consistent with
ADR-0009.
Enabling forbid(unsafe_code) closes the door on future unsafe, including the edition-2024 env functions, so any later
need for set_var must route through the injected lookup instead of reintroducing unsafe blocks.
Cost: a small API change in cerebro-core to thread the environment source through the call sites that resolve config and cortex paths. Any code that resolved paths by reading ambient env must switch to the injected form. The refactor is confined to config-path resolution and its tests; collectors that read git/sqlx do not read env this way.
Until the refactor lands, the standard’s full lint table cannot be adopted as written, because the forbid on
unsafe_code would fail on the fourteen existing blocks. The alignment plan treats this ADR as a prerequisite gate for
the lint-table phase.