fix: escape newlines in shell arguments on Windows

close #70
This commit is contained in:
weishu
2026-01-17 19:59:59 +08:00
parent c44eae0b17
commit 3f465cd47d
3 changed files with 16 additions and 4 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ import { systemPrompt } from "./utils/systemPrompt";
import { withBunRuntimeEnv } from "@/utils/bunRuntime";
import { spawnWithAbort } from "@/utils/spawnWithAbort";
import { getHapiBlobsDir } from "@/constants/uploadPaths";
import { stripNewlinesForWindowsShellArg } from "@/utils/shellEscape";
export async function claudeLocal(opts: {
abort: AbortSignal,
@@ -50,7 +51,7 @@ export async function claudeLocal(opts: {
args.push('--resume', startFrom);
}
args.push('--append-system-prompt', systemPrompt);
args.push('--append-system-prompt', stripNewlinesForWindowsShellArg(systemPrompt));
const cleanupMcpConfig = appendMcpConfigArg(args, opts.mcpServers, {
baseDir: projectDir
+4 -3
View File
@@ -25,6 +25,7 @@ import {
import { getDefaultClaudeCodePath, logDebug, streamToStdin } from './utils'
import { withBunRuntimeEnv } from '@/utils/bunRuntime'
import { killProcessByChildProcess } from '@/utils/process'
import { stripNewlinesForWindowsShellArg } from '@/utils/shellEscape'
import type { Writable } from 'node:stream'
import { logger } from '@/ui/logger'
import { appendMcpConfigArg } from '../utils/mcpConfig'
@@ -289,8 +290,8 @@ export function query(config: {
const args = ['--output-format', 'stream-json', '--verbose']
let cleanupMcpConfig: (() => void) | null = null
if (customSystemPrompt) args.push('--system-prompt', customSystemPrompt)
if (appendSystemPrompt) args.push('--append-system-prompt', appendSystemPrompt)
if (customSystemPrompt) args.push('--system-prompt', stripNewlinesForWindowsShellArg(customSystemPrompt))
if (appendSystemPrompt) args.push('--append-system-prompt', stripNewlinesForWindowsShellArg(appendSystemPrompt))
if (maxTurns) args.push('--max-turns', maxTurns.toString())
if (model) args.push('--model', model)
if (canCallTool) {
@@ -317,7 +318,7 @@ export function query(config: {
// Handle prompt input
if (typeof prompt === 'string') {
args.push('--print', prompt.trim())
args.push('--print', stripNewlinesForWindowsShellArg(prompt.trim()))
} else {
args.push('--input-format', 'stream-json')
}
+10
View File
@@ -0,0 +1,10 @@
/**
* Strip newlines when passing args through Windows cmd.exe.
* cmd.exe treats CR/LF as command separators and truncates multiline args.
*/
export function stripNewlinesForWindowsShellArg(value: string): string {
if (process.platform !== 'win32') {
return value;
}
return value.replace(/\r?\n/g, ' ');
}