From f877670143d9dd7972e0e730b9d7d214ae48f445 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Fri, 26 Sep 2025 00:26:55 +0000 Subject: [PATCH 1/2] fix: inherit shell environment for Claude Code CLI authentication - Added extendEnv: true to execa options to inherit shell environment variables - Added shell: true to ensure proper environment variable expansion - This ensures ANTHROPIC_API_KEY and other auth tokens are available to Claude CLI - Added tests to verify environment variable inheritance Fixes #8322 --- .../claude-code/__tests__/run.spec.ts | 57 +++++++++++++++++++ src/integrations/claude-code/run.ts | 4 ++ 2 files changed, 61 insertions(+) diff --git a/src/integrations/claude-code/__tests__/run.spec.ts b/src/integrations/claude-code/__tests__/run.spec.ts index a07120c28a..a11fd6fb8c 100644 --- a/src/integrations/claude-code/__tests__/run.spec.ts +++ b/src/integrations/claude-code/__tests__/run.spec.ts @@ -518,4 +518,61 @@ describe("runClaudeCode", () => { // Should throw ClaudeCodeNotFoundError, not generic exit code error await expect(generator.next()).rejects.toThrow(/errors\.claudeCode\.notFound/) }) + + test("should inherit environment variables for authentication", async () => { + const { runClaudeCode } = await import("../run") + + // Set a test environment variable + process.env.ANTHROPIC_API_KEY = "test-api-key-12345" + + const options = { + systemPrompt: "You are a helpful assistant", + messages: [{ role: "user" as const, content: "Hello" }], + } + + const generator = runClaudeCode(options) + + // Consume at least one item to trigger process spawn + await generator.next() + + // Clean up the generator + await generator.return(undefined) + + // Verify execa was called with proper environment options + const [, , execaOptions] = mockExeca.mock.calls[0] + + // Should have extendEnv set to true to inherit environment + expect(execaOptions.extendEnv).toBe(true) + + // Should use shell to ensure proper environment variable expansion + expect(execaOptions.shell).toBe(true) + + // Should still include process.env + expect(execaOptions.env).toBeDefined() + + // Clean up + delete process.env.ANTHROPIC_API_KEY + }) + + test("should pass CLAUDE_CODE_MAX_OUTPUT_TOKENS in environment", async () => { + const { runClaudeCode } = await import("../run") + + const options = { + systemPrompt: "You are a helpful assistant", + messages: [{ role: "user" as const, content: "Hello" }], + maxOutputTokens: 32000, + } + + const generator = runClaudeCode(options) + + // Consume at least one item to trigger process spawn + await generator.next() + + // Clean up the generator + await generator.return(undefined) + + // Verify the environment variable was set correctly + const [, , execaOptions] = mockExeca.mock.calls[0] + expect(execaOptions.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS).toBe("32000") + }) }) diff --git a/src/integrations/claude-code/run.ts b/src/integrations/claude-code/run.ts index 1d617b9242..1696769a65 100644 --- a/src/integrations/claude-code/run.ts +++ b/src/integrations/claude-code/run.ts @@ -191,6 +191,10 @@ function runProcess({ process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS || CLAUDE_CODE_DEFAULT_MAX_OUTPUT_TOKENS.toString(), }, + // Inherit the shell environment to ensure authentication tokens are available + extendEnv: true, + // Use shell to ensure proper environment variable expansion + shell: true, cwd, maxBuffer: 1024 * 1024 * 1000, timeout: CLAUDE_CODE_TIMEOUT, From a4b0bc0229b0d31ac0aac3956cc85f1c94db110b Mon Sep 17 00:00:00 2001 From: Roo Code Date: Fri, 26 Sep 2025 00:30:05 +0000 Subject: [PATCH 2/2] docs: add detailed comments explaining environment inheritance for Claude CLI auth --- src/integrations/claude-code/run.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/integrations/claude-code/run.ts b/src/integrations/claude-code/run.ts index 1696769a65..291d75003c 100644 --- a/src/integrations/claude-code/run.ts +++ b/src/integrations/claude-code/run.ts @@ -191,9 +191,11 @@ function runProcess({ process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS || CLAUDE_CODE_DEFAULT_MAX_OUTPUT_TOKENS.toString(), }, - // Inherit the shell environment to ensure authentication tokens are available + // IMPORTANT: These options are critical for Claude CLI authentication + // extendEnv: true - Inherits parent process environment variables including ANTHROPIC_API_KEY + // shell: true - Ensures proper environment variable expansion for authentication tokens + // Without these, Claude CLI fails with "Invalid API key" even when properly authenticated extendEnv: true, - // Use shell to ensure proper environment variable expansion shell: true, cwd, maxBuffer: 1024 * 1024 * 1000,