fix(env): stop reporting inherited process env vars as conflicts on Unix

On macOS/Linux the conflict check also scanned the current process
environment, which is inherited from whatever launched CC Switch. Claude
Desktop injects ANTHROPIC_BASE_URL=https://api.anthropic.com into the
terminals it spawns, so `pnpm dev` started there always showed a false
conflict. Such variables cannot be removed from inside the app either
(the Unix delete path for them is a no-op).

Only shell config file definitions are reported on Unix now; the Windows
registry check is unchanged.
This commit is contained in:
Jason committed 2026-10-19 18:01:47 +08:00
1 parent e2af66ca61
commit cd449274b4
1 file changed
+20 -21
+20 -21
View File
@@ -21,10 +21,15 @@ pub fn check_env_conflicts(app: &str) -> Result<Vec<EnvConflict>, String> {
let keywords = get_keywords_for_app(app);
let mut conflicts = Vec::new();
// Check system environment variables
// Check system environment variables (Windows Registry)
#[cfg(target_os = "windows")]
conflicts.extend(check_system_env(&keywords)?);
// Check shell configuration files (Unix only)
// Check shell configuration files (Unix only). The process environment is
// deliberately not scanned here: it is inherited from whatever launched
// CC Switch (e.g. Claude Desktop injects ANTHROPIC_BASE_URL into the
// terminals it spawns, so `pnpm dev` started there always "conflicts"),
// and such variables cannot be removed from inside the app anyway.
#[cfg(not(target_os = "windows"))]
conflicts.extend(check_shell_configs(&keywords)?);
@@ -100,25 +105,6 @@ fn check_system_env(keywords: &[EnvKeyword]) -> Result<Vec<EnvConflict>, String>
Ok(conflicts)
}
#[cfg(not(target_os = "windows"))]
fn check_system_env(keywords: &[EnvKeyword]) -> Result<Vec<EnvConflict>, String> {
let mut conflicts = Vec::new();
// Check current process environment
for (key, value) in std::env::vars() {
if matches_env_keyword(&key, keywords) {
conflicts.push(EnvConflict {
var_name: key,
var_value: value,
source_type: "system".to_string(),
source_path: "Process Environment".to_string(),
});
}
}
Ok(conflicts)
}
/// Check shell configuration files for environment variable exports (Unix only)
#[cfg(not(target_os = "windows"))]
fn check_shell_configs(keywords: &[EnvKeyword]) -> Result<Vec<EnvConflict>, String> {
@@ -232,4 +218,17 @@ mod tests {
assert!(!matches_env_keyword("MY_ANTHROPIC_API_KEY", &keywords));
assert!(!matches_env_keyword("NOT_ANTHROPIC", &keywords));
}
#[cfg(not(target_os = "windows"))]
#[test]
fn inherited_process_env_is_not_reported_on_unix() {
const VAR: &str = "ANTHROPIC_CC_SWITCH_INHERITED_PROBE";
std::env::set_var(VAR, "https://api.anthropic.com");
let conflicts = check_env_conflicts("claude");
std::env::remove_var(VAR);
let conflicts = conflicts.expect("check should succeed");
assert!(conflicts.iter().all(|c| c.source_type == "file"));
assert!(!conflicts.iter().any(|c| c.var_name == VAR));
}
}