Skip to content

Commit 67d7867

Browse files
committed
fix(bash): fix PowerShell command handling and stream reading
- Change shouldBypassShell to false for PowerShell (proper argument parsing) - Improve stream reader to guarantee streams drain before returning - Fixes Issue anomalyco#17 (data loss) and Issue anomalyco#27 (argument parsing)
1 parent 90c0b16 commit 67d7867

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

‎packages/opencode/src/tool/bash.ts‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export function parseCommand(command: string): { executable: string; args: strin
134134
const trimmed = command.trim()
135135
const shellType = detectCommandShell(trimmed)
136136

137-
// PowerShell commands: extract PowerShell executable and arguments
137+
// PowerShell commands: MUST use shell wrapper for proper argument parsing
138+
// Issue #27 fix: PowerShell -Command "..." requires cmd.exe to parse correctly
139+
// Without shell wrapping, arguments are split incorrectly and commands fail
138140
if (shellType === 'powershell' || shellType === 'pwsh') {
139141
const parts = trimmed.split(/\s+/)
140142
const executable = shellType === 'pwsh' ? 'pwsh' : 'powershell.exe'
@@ -143,7 +145,7 @@ export function parseCommand(command: string): { executable: string; args: strin
143145
return {
144146
executable,
145147
args,
146-
shouldBypassShell: true // Direct execution, no shell wrapping
148+
shouldBypassShell: false // Use shell wrapper for proper parsing
147149
}
148150
}
149151

@@ -332,17 +334,12 @@ export const BashTool = Tool.define("bash", async () => {
332334
if (!reader) return
333335
try {
334336
while (true) {
335-
const { done, value } = await Promise.race([
336-
reader.read(),
337-
new Promise<{ done: true }>((_, reject) => {
338-
ctx.abort.addEventListener("abort", () => reject(new Error("Aborted")), { once: true })
339-
})
340-
])
337+
const { done, value } = await reader.read()
341338
if (done) break
342339
append(value)
343340
}
344-
} catch (_e) {
345-
// Stream reading ended or was aborted
341+
} catch {
342+
// Stream reading ended (abort or natural completion)
346343
}
347344
}
348345

@@ -352,6 +349,7 @@ export const BashTool = Tool.define("bash", async () => {
352349

353350
const kill = () => Shell.killTree(proc, { exited: () => exited })
354351

352+
// Handle abort before starting
355353
if (ctx.abort.aborted) {
356354
aborted = true
357355
await kill()
@@ -369,14 +367,18 @@ export const BashTool = Tool.define("bash", async () => {
369367
void kill()
370368
}, timeout + 100)
371369

372-
// Concurrent stream reading and process monitoring
373-
await Promise.race([
374-
proc.exited,
375-
Promise.all([
376-
readOutput(stdoutReader),
377-
readOutput(stderrReader),
378-
]).then(() => proc.exited),
379-
])
370+
// Start reading streams
371+
const stdoutPromise = readOutput(stdoutReader)
372+
const stderrPromise = readOutput(stderrReader)
373+
374+
// Wait for process exit
375+
await proc.exited
376+
377+
// Guarantee streams drain before returning (Issue #17 fix)
378+
// This prevents data loss when proc.exited resolves before streams finish
379+
await Promise.all([stdoutPromise, stderrPromise]).catch(() => {})
380+
381+
exited = true
380382

381383
// Cleanup
382384
clearTimeout(timeoutTimer)

0 commit comments

Comments
 (0)