diff --git a/src/main/claude/claude-config-dir-pin.ts b/src/main/claude/claude-config-dir-pin.ts index da6fcac0328c..123fa7fdcef1 100644 --- a/src/main/claude/claude-config-dir-pin.ts +++ b/src/main/claude/claude-config-dir-pin.ts @@ -30,6 +30,11 @@ function samePath(a: string, b: string, platform: NodeJS.Platform): boolean { // Pin only nondefault homes: even an explicit default changes the CLI's Keychain identity. // Preserve the trimmed path's spelling; normalization is only for comparison. +/** Only the Claude CLI honors $CLAUDE_CONFIG_DIR; Claude-compatible variants keep their home-relative dir. */ +export function claudeCompatibleConfigDir(configDirName: string): string { + return configDirName === '.claude' ? defaultClaudeConfigDir() : join(homedir(), configDirName) +} + export function claudeConfigDirEnvPatch( accountHome: string, options: { env?: NodeJS.ProcessEnv; platform?: NodeJS.Platform } = {} diff --git a/src/main/claude/hook-settings.test.ts b/src/main/claude/hook-settings.test.ts new file mode 100644 index 000000000000..3e5ab072a779 --- /dev/null +++ b/src/main/claude/hook-settings.test.ts @@ -0,0 +1,38 @@ +// Why: locks the hook settings target to the config dir the Claude CLI actually reads, +// so a set CLAUDE_CONFIG_DIR cannot strand Orca's managed hooks in an unread ~/.claude (#24873). +import { homedir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { CLAUDE_HOOK_SETTINGS, getConfigPath, OPENCLAUDE_HOOK_SETTINGS } from './hook-settings' + +afterEach(() => { + vi.unstubAllEnvs() + delete process.env.CLAUDE_CONFIG_DIR +}) + +describe('getConfigPath', () => { + it('follows a set CLAUDE_CONFIG_DIR, the same resolution as the rest of Orca\u2019s Claude code', () => { + vi.stubEnv('CLAUDE_CONFIG_DIR', join('/config', 'claude-alt')) + expect(getConfigPath()).toBe(join('/config', 'claude-alt', 'settings.json')) + }) + + it('keeps the legacy ~/.claude path when the variable is unset, so the fallback is unchanged', () => { + delete process.env.CLAUDE_CONFIG_DIR + expect(getConfigPath()).toBe(join(homedir(), '.claude', 'settings.json')) + }) + + it('falls back on a whitespace-only value and keeps a real value verbatim, like defaultClaudeConfigDir', () => { + vi.stubEnv('CLAUDE_CONFIG_DIR', ' ') + expect(getConfigPath()).toBe(join(homedir(), '.claude', 'settings.json')) + vi.stubEnv('CLAUDE_CONFIG_DIR', `${join('/config', 'claude-alt')}/`) + expect(getConfigPath()).toBe(join('/config', 'claude-alt', 'settings.json')) + }) + + it('leaves Claude-compatible CLIs on their own home directory', () => { + vi.stubEnv('CLAUDE_CONFIG_DIR', join('/config', 'claude-alt')) + expect(getConfigPath(OPENCLAUDE_HOOK_SETTINGS)).toBe( + join(homedir(), '.openclaude', 'settings.json') + ) + expect(getConfigPath(CLAUDE_HOOK_SETTINGS)).toBe(join('/config', 'claude-alt', 'settings.json')) + }) +}) diff --git a/src/main/claude/hook-settings.ts b/src/main/claude/hook-settings.ts index edfc429d4b8e..acd117ba2f36 100644 --- a/src/main/claude/hook-settings.ts +++ b/src/main/claude/hook-settings.ts @@ -1,4 +1,4 @@ -import { homedir } from 'node:os' +import { claudeCompatibleConfigDir } from './claude-config-dir-pin' import { basename, extname, join, win32 } from 'node:path' import { buildManagedCommandHook, @@ -51,7 +51,7 @@ export const OPENCLAUDE_HOOK_SETTINGS: ClaudeCompatibleHookSettings = { export type NativeHookFormOptions = { execForm?: boolean } export function getConfigPath(settings = CLAUDE_HOOK_SETTINGS, configDir?: string): string { - return join(configDir ?? join(homedir(), settings.configDirName), 'settings.json') + return join(configDir ?? claudeCompatibleConfigDir(settings.configDirName), 'settings.json') } export function getStatusLineScriptBaseName(settings = CLAUDE_HOOK_SETTINGS): string {