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
This commit is contained in:
Roo Code 2025-09-26 00:26:55 +00:00
parent 87d50a78cb
commit f877670143
2 changed files with 61 additions and 0 deletions

View file

@ -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")
})
})

View file

@ -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,