Skip to content

fix: respect CLAUDE_CONFIG_DIR environment variable for Claude Code config path#2740

Open
lihan3238 wants to merge 3 commits into
farion1231:mainfrom
lihan3238:fix-2706-claude-config-dir
Open

fix: respect CLAUDE_CONFIG_DIR environment variable for Claude Code config path#2740
lihan3238 wants to merge 3 commits into
farion1231:mainfrom
lihan3238:fix-2706-claude-config-dir

Conversation

@lihan3238
Copy link
Copy Markdown

Fixes #2706

Summary

When the CLAUDE_CONFIG_DIR environment variable is set, cc-switch now reads Claude Code configuration from that directory instead of always defaulting to ~/.claude.

The priority order for determining the Claude Code config directory is now:

  1. cc-switch settings override (claude_config_dir in settings) - highest priority
  2. CLAUDE_CONFIG_DIR environment variable (only if the path is absolute)
  3. Default ~/.claude - fallback

Changes

  • Modified get_claude_config_dir() in src-tauri/src/config.rs to check std::env::var("CLAUDE_CONFIG_DIR") before falling back to the default path
  • Only accepts absolute paths from the env var (relative paths are ignored for safety)

Test plan

  • Build passes (cargo build in src-tauri)
  • Manual test: set CLAUDE_CONFIG_DIR=/custom/path and verify cc-switch reads config from that path
  • Verify that without the env var, behavior is unchanged (still uses ~/.claude)
  • Verify that cc-switch settings override still takes priority over the env var

When CLAUDE_CONFIG_DIR is set as an environment variable, cc-switch
should read Claude Code configuration from that directory instead of
the default ~/.claude path.

The priority order is now:
1. cc-switch settings override (claude_config_dir in settings)
2. CLAUDE_CONFIG_DIR environment variable (if absolute path)
3. Default ~/.claude

Fixes farion1231#2706
@farion1231
Copy link
Copy Markdown
Owner

@codex review
@claude review

@claude
Copy link
Copy Markdown

claude Bot commented May 12, 2026

Claude finished @farion1231's task in 1m 39s —— View job


Claude Code Review

  • Read changed file src-tauri/src/config.rs
  • Check repo conventions
  • Inspect related code (config dir usage across the crate)
  • Post review

Finding: MCP path (.claude.json) does not follow CLAUDE_CONFIG_DIRImportant (confidence ~85)

The PR makes get_claude_config_dir() respect CLAUDE_CONFIG_DIR, but the sibling MCP path is computed independently and is not updated. See src-tauri/src/config.rs:71-78:

pub fn get_claude_mcp_path() -> PathBuf {
    if let Some(custom_dir) = crate::settings::get_claude_override_dir() {
        if let Some(path) = derive_mcp_path_from_override(&custom_dir) {
            return path;
        }
    }
    get_default_claude_mcp_path()   // ~/.claude.json — env var ignored
}

Concrete effect: with CLAUDE_CONFIG_DIR=/foo/.claude and no cc-switch override set, cc-switch will:

  • read settings from /foo/.claude/settings.json
  • read MCP from ~/.claude.json

This is inconsistent with the existing GUI-override behavior (which derives MCP from the configured dir via derive_mcp_path_from_override) and with the PR's stated intent of treating CLAUDE_CONFIG_DIR as a fallback for the override. A user who switches their entire Claude config to a non-default location via the env var will silently keep editing the wrong .claude.json.

Suggested fix: in get_claude_mcp_path, also fall back to derive_mcp_path_from_override against the env-var path when no GUI override is set. Same priority ladder (GUI override → env var → default) as get_claude_config_dir.

Fix this →


Other notes

  • Empty / whitespace env value is safely handled: PathBuf::from("").is_absolute() is false, so it falls through to the default. No change needed.
  • On Windows, is_absolute() correctly rejects drive-relative paths like \foo\bar, so the absolute-only guard behaves sanely cross-platform.
  • No tests added for the new branch; repo has no explicit testing mandate, so noting only — not a blocker.

The new code itself (lines 42–47) is correct and minimal. The only blocking concern is the MCP-path asymmetry above.
• Branch: fix-2706-claude-config-dir

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d84288102b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src-tauri/src/config.rs
Comment on lines +42 to +46
if let Ok(env_dir) = std::env::var("CLAUDE_CONFIG_DIR") {
let p = PathBuf::from(env_dir);
if p.is_absolute() {
return p;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Align MCP path with CLAUDE_CONFIG_DIR

When CLAUDE_CONFIG_DIR is set (and no claude_config_dir override exists), this new branch makes get_claude_config_dir() return the env directory, but MCP/state file resolution still goes through get_default_claude_mcp_path() (~/.claude.json). That splits one Claude profile across two locations: settings.json/projects under CLAUDE_CONFIG_DIR, while MCP servers and onboarding flags are read/written in home, so users on custom profiles will see missing or stale MCP config.

Useful? React with 👍 / 👎.

lihan3238 added 2 commits May 12, 2026 17:56
When CLAUDE_CONFIG_DIR is set (and no GUI override exists),
get_claude_mcp_path() now derives the MCP path from the env var
directory instead of falling back to ~/.claude.json.

This mirrors the priority ladder in get_claude_config_dir():
GUI override > CLAUDE_CONFIG_DIR > default.

Addresses review feedback from Codex and Claude bots.
@lihan3238
Copy link
Copy Markdown
Author

Thanks for the review feedback from both Codex and Claude bots! I've addressed the MCP path asymmetry:

Problem: get_claude_mcp_path() ignored CLAUDE_CONFIG_DIR, so with the env var set, settings would be read from the custom dir but MCP would still fall back to ~/.claude.json.

Fix: Updated get_claude_mcp_path() to follow the same priority ladder as get_claude_config_dir():

  1. GUI override → derive_mcp_path_from_override(custom_dir)
  2. CLAUDE_CONFIG_DIR env var (absolute only) → derive_mcp_path_from_override(env_dir)
  3. Default → ~/.claude.json

This ensures MCP config and settings stay in the same profile when using CLAUDE_CONFIG_DIR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

windows中设置了CLAUDE_CONFIG_DIR, 指定claudecode配置文件路径, ccswitch仍然只读写~/.claude/下的配置

2 participants