Skip to content

Commit f877670

Browse files
committed
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
1 parent 87d50a7 commit f877670

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/integrations/claude-code/__tests__/run.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,61 @@ describe("runClaudeCode", () => {
518518
// Should throw ClaudeCodeNotFoundError, not generic exit code error
519519
await expect(generator.next()).rejects.toThrow(/errors\.claudeCode\.notFound/)
520520
})
521+
522+
test("should inherit environment variables for authentication", async () => {
523+
const { runClaudeCode } = await import("../run")
524+
525+
// Set a test environment variable
526+
process.env.ANTHROPIC_API_KEY = "test-api-key-12345"
527+
528+
const options = {
529+
systemPrompt: "You are a helpful assistant",
530+
messages: [{ role: "user" as const, content: "Hello" }],
531+
}
532+
533+
const generator = runClaudeCode(options)
534+
535+
// Consume at least one item to trigger process spawn
536+
await generator.next()
537+
538+
// Clean up the generator
539+
await generator.return(undefined)
540+
541+
// Verify execa was called with proper environment options
542+
const [, , execaOptions] = mockExeca.mock.calls[0]
543+
544+
// Should have extendEnv set to true to inherit environment
545+
expect(execaOptions.extendEnv).toBe(true)
546+
547+
// Should use shell to ensure proper environment variable expansion
548+
expect(execaOptions.shell).toBe(true)
549+
550+
// Should still include process.env
551+
expect(execaOptions.env).toBeDefined()
552+
553+
// Clean up
554+
delete process.env.ANTHROPIC_API_KEY
555+
})
556+
557+
test("should pass CLAUDE_CODE_MAX_OUTPUT_TOKENS in environment", async () => {
558+
const { runClaudeCode } = await import("../run")
559+
560+
const options = {
561+
systemPrompt: "You are a helpful assistant",
562+
messages: [{ role: "user" as const, content: "Hello" }],
563+
maxOutputTokens: 32000,
564+
}
565+
566+
const generator = runClaudeCode(options)
567+
568+
// Consume at least one item to trigger process spawn
569+
await generator.next()
570+
571+
// Clean up the generator
572+
await generator.return(undefined)
573+
574+
// Verify the environment variable was set correctly
575+
const [, , execaOptions] = mockExeca.mock.calls[0]
576+
expect(execaOptions.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS).toBe("32000")
577+
})
521578
})

src/integrations/claude-code/run.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ function runProcess({
191191
process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS ||
192192
CLAUDE_CODE_DEFAULT_MAX_OUTPUT_TOKENS.toString(),
193193
},
194+
// Inherit the shell environment to ensure authentication tokens are available
195+
extendEnv: true,
196+
// Use shell to ensure proper environment variable expansion
197+
shell: true,
194198
cwd,
195199
maxBuffer: 1024 * 1024 * 1000,
196200
timeout: CLAUDE_CODE_TIMEOUT,

0 commit comments

Comments
 (0)