Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(config): honor CLAUDE_CONFIG_DIR for Claude Code paths (#3215)
James Felix Black64d5eff260d1
diff --git a/src/cli/config.rs b/src/cli/config.rsindex 6989fda05..a6de88d4d 100644--- a/src/cli/config.rs+++ b/src/cli/config.rs@@ -357,13 +357,13 @@ $ claude plugin uninstall worktrunk@worktrunk/// Configure the Claude Code statusline#[command(name = "install-statusline",- after_long_help = r#"Writes the statusline configuration to `~/.claude/settings.json`, setting:+ after_long_help = r#"Writes the statusline configuration to `~/.claude/settings.json` (or `$CLAUDE_CONFIG_DIR/settings.json` when set), setting:```json{"statusLine":{"type":"command","command":"wt list statusline --format=claude-code"}}```-Preserves existing settings. Creates the `.claude/` directory and `settings.json` if needed.+Preserves existing settings. Creates the config directory and `settings.json` if needed.Skips gracefully if the statusline is already configured."#)]diff --git a/src/commands/config/plugins.rs b/src/commands/config/plugins.rsindex 8f100020a..eba12d80a 100644--- a/src/commands/config/plugins.rs+++ b/src/commands/config/plugins.rs@@ -7,7 +7,9 @@ use color_print::cformat;use worktrunk::shell_exec::Cmd;use worktrunk::styling::{eprintln, info_message, progress_message, success_message};-use super::show::{home_dir, is_claude_available, is_plugin_installed, is_statusline_configured};+use super::show::{+ claude_config_dir, is_claude_available, is_plugin_installed, is_statusline_configured,+};use crate::output::prompt::{PromptResponse, prompt_yes_no_preview};/// Handle `wt config plugins claude install`@@ -135,7 +137,7 @@ pub fn handle_claude_install_statusline(yes: bool) -> anyhow::Result<()> {// Ensure parent directory existsif let Some(parent) = settings_path.parent() {- std::fs::create_dir_all(parent).context("Failed to create .claude directory")?;+ std::fs::create_dir_all(parent).context("Failed to create Claude Code config directory")?;}// Read existing settings or start with empty object@@ -168,12 +170,13 @@ pub fn handle_claude_install_statusline(yes: bool) -> anyhow::Result<()> {Ok(())}-/// Get the path to `~/.claude/settings.json`, or bail if home dir is unavailable+/// Get the path to Claude Code's `settings.json` (under `CLAUDE_CONFIG_DIR` or+/// `~/.claude`), or bail if the config directory can't be determinedfn require_settings_path() -> anyhow::Result<PathBuf> {- let Some(home) = home_dir() else {- bail!("Could not determine home directory");+ let Some(config_dir) = claude_config_dir() else {+ bail!("Could not determine Claude Code config directory");};- Ok(home.join(".claude/settings.json"))+ Ok(config_dir.join("settings.json"))}/// Bail if `claude` CLI is not availablediff --git a/src/commands/config/show.rs b/src/commands/config/show.rsindex 538fc1502..6acc5b17e 100644--- a/src/commands/config/show.rs+++ b/src/commands/config/show.rs@@ -172,13 +172,32 @@ pub(super) fn home_dir() -> Option<PathBuf> {.or_else(worktrunk::path::home_dir)}+/// Get the Claude Code config directory.+///+/// Honors `CLAUDE_CONFIG_DIR`, which Claude Code uses to relocate its entire+/// config tree (`settings.json`, `plugins/`, ...) away from the default+/// `~/.claude`. A leading `~/` in the value is expanded against the home+/// directory; the shell normally expands it before the variable is set, so a+/// literal `~` only reaches us when the variable is set in a non-shell context.+pub(super) fn claude_config_dir() -> Option<PathBuf> {+ if let Ok(dir) = std::env::var("CLAUDE_CONFIG_DIR")+ && !dir.is_empty()+ {+ if let Some(rest) = dir.strip_prefix("~/") {+ return home_dir().map(|home| home.join(rest));+ }+ return Some(PathBuf::from(dir));+ }+ home_dir().map(|home| home.join(".claude"))+}+/// Check if the worktrunk plugin is installed in Claude Codepub(super) fn is_plugin_installed() -> bool {- let Some(home) = home_dir() else {+ let Some(config_dir) = claude_config_dir() else {return false;};- let plugins_file = home.join(".claude/plugins/installed_plugins.json");+ let plugins_file = config_dir.join("plugins/installed_plugins.json");let Ok(content) = std::fs::read_to_string(&plugins_file) else {return false;};@@ -194,11 +213,11 @@ pub(super) fn is_plugin_installed() -> bool {/// Check if the statusline is configured in Claude Code settingspub(super) fn is_statusline_configured() -> bool {- let Some(home) = home_dir() else {+ let Some(config_dir) = claude_config_dir() else {return false;};- let settings_file = home.join(".claude/settings.json");+ let settings_file = config_dir.join("settings.json");let Ok(content) = std::fs::read_to_string(&settings_file) else {return false;};diff --git a/src/testing/mod.rs b/src/testing/mod.rsindex b6e50b9c7..e165b8a0c 100644--- a/src/testing/mod.rs+++ b/src/testing/mod.rs@@ -744,6 +744,10 @@ pub fn set_temp_home_env(cmd: &mut Command, home: &Path) {// OpenCode: override config dir to avoid platform-specific dirs::config_dir() differences// (Linux: ~/.config, macOS: ~/Library/Application Support, Windows: AppData\Roaming)cmd.env("OPENCODE_CONFIG_DIR", home.join("opencode-config"));+ // Claude Code: pin the config dir to the temp home's `.claude` so detection+ // matches the setup helpers and stays hermetic against an ambient+ // CLAUDE_CONFIG_DIR inherited from the test runner's environment.+ cmd.env("CLAUDE_CONFIG_DIR", home.join(".claude"));}/// Override `WORKTRUNK_CONFIG_PATH` to point to the XDG-derived user config pathdiff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex fef22685e..cc0e8a47f 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -4220,6 +4220,61 @@ fn test_plugins_claude_install_statusline_empty_file(repo: TestRepo, temp_home:});}+#[rstest]+fn test_plugins_claude_install_statusline_honors_claude_config_dir(+ repo: TestRepo,+ temp_home: TempDir,+) {+ // Claude Code relocates its entire config tree under CLAUDE_CONFIG_DIR, so+ // the statusline must land at $CLAUDE_CONFIG_DIR/settings.json, not+ // ~/.claude/settings.json.+ let config_dir = temp_home.path().join("custom-claude");++ let mut cmd = wt_command();+ repo.configure_wt_cmd(&mut cmd);+ cmd.args(["config", "plugins", "claude", "install-statusline", "--yes"])+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());+ cmd.env("CLAUDE_CONFIG_DIR", &config_dir);++ let output = cmd.output().unwrap();+ assert!(+ output.status.success(),+ "install-statusline failed: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ // Written under CLAUDE_CONFIG_DIR...+ let settings_path = config_dir.join("settings.json");+ let content = fs::read_to_string(&settings_path).unwrap();+ let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();+ assert_eq!(+ parsed["statusLine"]["command"],+ "wt list statusline --format=claude-code"+ );++ // ...and NOT under the default ~/.claude.+ assert!(+ !temp_home.path().join(".claude/settings.json").exists(),+ "settings.json must not be written to ~/.claude when CLAUDE_CONFIG_DIR is set"+ );++ // Detection reads the same relocated path: a second run sees it as configured.+ let mut cmd = wt_command();+ repo.configure_wt_cmd(&mut cmd);+ cmd.args(["config", "plugins", "claude", "install-statusline", "--yes"])+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());+ cmd.env("CLAUDE_CONFIG_DIR", &config_dir);++ let output = cmd.output().unwrap();+ assert!(output.status.success());+ assert!(+ String::from_utf8_lossy(&output.stderr).contains("already configured"),+ "second run should detect the relocated statusline as already configured"+ );+}+// ==================== Plugin Command Failure Tests ====================#[rstest]