feat: honor FOREST_PATH env var for data directory#7080
Conversation
Adds a FOREST_PATH environment variable that overrides `client.data_dir` for `forest`, `forest-cli`, `forest-tool` and `forest-wallet`. Precedence is env > config > defaults. The daemon also logs the resolved data directory on startup. Closes ChainSafe#6008
WalkthroughImplement ChangesFOREST_PATH Environment Variable Override
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/cli_shared/mod.rs`:
- Around line 85-99: The test read_config_forest_path_env_override mutates the
global FOREST_PATH_ENV without restoring any prior value, so capture the
previous value (Option<String>) before calling std::env::set_var and install a
drop guard that will restore the previous state (std::env::set_var(previous) or
std::env::remove_var if None) when it goes out of scope; update the test (around
uses of FOREST_PATH_ENV and read_config) to use this guard so cleanup runs even
on panic and the global process state is preserved for other tests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3c359569-6f3e-4580-9a69-d14f2f8b9ae7
📒 Files selected for processing (5)
CHANGELOG.mddocs/docs/users/reference/env_variables.mdsrc/cli_shared/mod.rssrc/daemon/mod.rssrc/wallet/subcommands/wallet_cmd.rs
| #[test] | ||
| #[serial_test::serial] | ||
| fn read_config_forest_path_env_override() { | ||
| let temp_dir = tempfile::tempdir().expect("couldn't create temp dir"); | ||
| // SAFETY: tests touching the process environment are gated by `#[serial]`. | ||
| unsafe { | ||
| std::env::set_var(FOREST_PATH_ENV, temp_dir.path()); | ||
| } | ||
| let (_, config) = read_config(None, None).unwrap(); | ||
| // SAFETY: tests touching the process environment are gated by `#[serial]`. | ||
| unsafe { | ||
| std::env::remove_var(FOREST_PATH_ENV); | ||
| } | ||
| assert_eq!(config.client.data_dir, temp_dir.path()); | ||
| } |
There was a problem hiding this comment.
Restore the previous FOREST_PATH value in the test cleanup
Line 96 always removes the variable. If FOREST_PATH was already set before this test, global process state is changed for later tests. Save the previous value and restore it via a drop guard so cleanup still runs on panic.
Proposed fix
#[test]
#[serial_test::serial]
fn read_config_forest_path_env_override() {
let temp_dir = tempfile::tempdir().expect("couldn't create temp dir");
+ let previous_forest_path = std::env::var_os(FOREST_PATH_ENV);
+ struct ForestPathEnvGuard(Option<std::ffi::OsString>);
+ impl Drop for ForestPathEnvGuard {
+ fn drop(&mut self) {
+ // SAFETY: tests touching the process environment are gated by `#[serial]`.
+ unsafe {
+ if let Some(value) = &self.0 {
+ std::env::set_var(FOREST_PATH_ENV, value);
+ } else {
+ std::env::remove_var(FOREST_PATH_ENV);
+ }
+ }
+ }
+ }
+ let _guard = ForestPathEnvGuard(previous_forest_path);
// SAFETY: tests touching the process environment are gated by `#[serial]`.
unsafe {
std::env::set_var(FOREST_PATH_ENV, temp_dir.path());
}
let (_, config) = read_config(None, None).unwrap();
- // SAFETY: tests touching the process environment are gated by `#[serial]`.
- unsafe {
- std::env::remove_var(FOREST_PATH_ENV);
- }
assert_eq!(config.client.data_dir, temp_dir.path());
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/cli_shared/mod.rs` around lines 85 - 99, The test
read_config_forest_path_env_override mutates the global FOREST_PATH_ENV without
restoring any prior value, so capture the previous value (Option<String>) before
calling std::env::set_var and install a drop guard that will restore the
previous state (std::env::set_var(previous) or std::env::remove_var if None)
when it goes out of scope; update the test (around uses of FOREST_PATH_ENV and
read_config) to use this guard so cleanup runs even on panic and the global
process state is preserved for other tests.
Codecov Report❌ Patch coverage is
Additional details and impacted files
... and 4 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Summary of changes
Changes introduced in this pull request:
FOREST_PATHenvironment variable that overrides the data directory forforest,forest-cli,forest-toolandforest-wallet.FOREST_PATHwins overclient.data_dirfrom the config file; when unset, behavior is unchanged.forest-wallethonorsFOREST_PATHwhen set, otherwise keeps the existingForest-WalletProjectDirslocation for backwards compatibility.env_variables.mdand aCHANGELOG.mdentry under### Added.Reference issue to close (if applicable)
Closes #6008
Other information and links
Mirrors Lotus's
LOTUS_PATHso operators switching between implementations get a consistent escape hatch. The wallet override is intentionally opt-in (no implicit migration of the existingForest-WalletProjectDirslocation).A unit test (
read_config_forest_path_env_override) exercises the env-over-config precedence withserial_test::serialso it doesn't race other env-touching tests.Change checklist
Summary by CodeRabbit
Release Notes
New Features
FOREST_PATHenvironment variable to override the default data directory location, taking precedence over config file settings.Documentation
FOREST_PATH.