From 75c18ad2565c49ec5d84eb57497072c434e3d969 Mon Sep 17 00:00:00 2001 From: argenis de la rosa Date: Mon, 16 Feb 2026 18:11:04 -0500 Subject: [PATCH] fix(config): check ZEROCLAW_WORKSPACE before loading config - Move ZEROCLAW_WORKSPACE check to the start of load_or_init() - Use custom workspace for both config and workspace directories - Fixes issue where env var was applied AFTER config loading Fixes #417 Co-Authored-By: Claude Opus 4.6 --- src/config/schema.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/config/schema.rs b/src/config/schema.rs index 34be770..d5b2a7c 100644 --- a/src/config/schema.rs +++ b/src/config/schema.rs @@ -1625,16 +1625,34 @@ impl Default for Config { impl Config { pub fn load_or_init() -> Result { - let home = UserDirs::new() - .map(|u| u.home_dir().to_path_buf()) - .context("Could not find home directory")?; - let zeroclaw_dir = home.join(".zeroclaw"); + // Check ZEROCLAW_WORKSPACE first, before determining config path + let (zeroclaw_dir, workspace_dir) = + if let Ok(custom_workspace) = std::env::var("ZEROCLAW_WORKSPACE") { + if !custom_workspace.is_empty() { + let workspace = PathBuf::from(&custom_workspace); + let config_dir = workspace.join(".zeroclaw"); + (config_dir, workspace) + } else { + // Fall through to default if empty + let home = UserDirs::new() + .map(|u| u.home_dir().to_path_buf()) + .context("Could not find home directory")?; + let default_dir = home.join(".zeroclaw"); + (default_dir.clone(), default_dir.join("workspace")) + } + } else { + let home = UserDirs::new() + .map(|u| u.home_dir().to_path_buf()) + .context("Could not find home directory")?; + let default_dir = home.join(".zeroclaw"); + (default_dir.clone(), default_dir.join("workspace")) + }; + let config_path = zeroclaw_dir.join("config.toml"); if !zeroclaw_dir.exists() { fs::create_dir_all(&zeroclaw_dir).context("Failed to create .zeroclaw directory")?; - fs::create_dir_all(zeroclaw_dir.join("workspace")) - .context("Failed to create workspace directory")?; + fs::create_dir_all(&workspace_dir).context("Failed to create workspace directory")?; } if config_path.exists() { @@ -1644,13 +1662,13 @@ impl Config { toml::from_str(&contents).context("Failed to parse config file")?; // Set computed paths that are skipped during serialization config.config_path = config_path.clone(); - config.workspace_dir = zeroclaw_dir.join("workspace"); + config.workspace_dir = workspace_dir; config.apply_env_overrides(); Ok(config) } else { let mut config = Config::default(); config.config_path = config_path.clone(); - config.workspace_dir = zeroclaw_dir.join("workspace"); + config.workspace_dir = workspace_dir; config.save()?; config.apply_env_overrides(); Ok(config)