fix(bash): use PowerShell on Windows to capture system utility output

Windows system utilities like ipconfig, systeminfo, etc. write directly to
the console via WriteConsole API instead of stdout. When Node.js spawns
cmd.exe with piped stdio, these utilities produce empty output.

Changes:
- Switch from cmd.exe to PowerShell on Windows (properly redirects output)
- Disable detached mode on Windows (PowerShell doesn't pipe stdout when detached)
- Add windowsHide option to prevent console window flashing
- Update tests to use PowerShell-compatible syntax (Start-Sleep, semicolons)
This commit is contained in:
myfunc
2026-01-11 16:35:21 -08:00
committed by Peter Steinberger
parent e3be5f8369
commit b33bd6aaeb
3 changed files with 19 additions and 9 deletions

View File

@@ -9,11 +9,12 @@ import {
import { sanitizeBinaryOutput } from "./shell-utils.js";
const isWin = process.platform === "win32";
const shortDelayCmd = isWin ? "ping -n 2 127.0.0.1 > nul" : "sleep 0.05";
const yieldDelayCmd = isWin ? "ping -n 3 127.0.0.1 > nul" : "sleep 0.2";
const longDelayCmd = isWin ? "ping -n 4 127.0.0.1 > nul" : "sleep 2";
const joinCommands = (commands: string[]) =>
commands.join(isWin ? " & " : "; ");
// PowerShell: Start-Sleep for delays, ; for command separation, $null for null device
const shortDelayCmd = isWin ? "Start-Sleep -Milliseconds 50" : "sleep 0.05";
const yieldDelayCmd = isWin ? "Start-Sleep -Milliseconds 200" : "sleep 0.2";
const longDelayCmd = isWin ? "Start-Sleep -Seconds 2" : "sleep 2";
// Both PowerShell and bash use ; for command separation
const joinCommands = (commands: string[]) => commands.join("; ");
const echoAfterDelay = (message: string) =>
joinCommands([shortDelayCmd, `echo ${message}`]);
const echoLines = (lines: string[]) =>

View File

@@ -265,15 +265,17 @@ export function createBashTool(
{
cwd: workdir,
env: process.env,
detached: true,
detached: process.platform !== "win32",
stdio: ["pipe", "pipe", "pipe"],
windowsHide: true,
},
)
: spawn(shell, [...shellArgs, params.command], {
cwd: workdir,
env,
detached: true,
detached: process.platform !== "win32",
stdio: ["pipe", "pipe", "pipe"],
windowsHide: true,
});
const session = {

View File

@@ -2,8 +2,15 @@ import { spawn } from "node:child_process";
export function getShellConfig(): { shell: string; args: string[] } {
if (process.platform === "win32") {
const shell = process.env.COMSPEC?.trim() || "cmd.exe";
return { shell, args: ["/d", "/s", "/c"] };
// Use PowerShell instead of cmd.exe on Windows.
// Problem: Many Windows system utilities (ipconfig, systeminfo, etc.) write
// directly to the console via WriteConsole API, bypassing stdout pipes.
// When Node.js spawns cmd.exe with piped stdio, these utilities produce no output.
// PowerShell properly captures and redirects their output to stdout.
return {
shell: "powershell.exe",
args: ["-NoProfile", "-NonInteractive", "-Command"],
};
}
const shell = process.env.SHELL?.trim() || "sh";