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:
committed by
Peter Steinberger
parent
e3be5f8369
commit
b33bd6aaeb
@@ -9,11 +9,12 @@ import {
|
|||||||
import { sanitizeBinaryOutput } from "./shell-utils.js";
|
import { sanitizeBinaryOutput } from "./shell-utils.js";
|
||||||
|
|
||||||
const isWin = process.platform === "win32";
|
const isWin = process.platform === "win32";
|
||||||
const shortDelayCmd = isWin ? "ping -n 2 127.0.0.1 > nul" : "sleep 0.05";
|
// PowerShell: Start-Sleep for delays, ; for command separation, $null for null device
|
||||||
const yieldDelayCmd = isWin ? "ping -n 3 127.0.0.1 > nul" : "sleep 0.2";
|
const shortDelayCmd = isWin ? "Start-Sleep -Milliseconds 50" : "sleep 0.05";
|
||||||
const longDelayCmd = isWin ? "ping -n 4 127.0.0.1 > nul" : "sleep 2";
|
const yieldDelayCmd = isWin ? "Start-Sleep -Milliseconds 200" : "sleep 0.2";
|
||||||
const joinCommands = (commands: string[]) =>
|
const longDelayCmd = isWin ? "Start-Sleep -Seconds 2" : "sleep 2";
|
||||||
commands.join(isWin ? " & " : "; ");
|
// Both PowerShell and bash use ; for command separation
|
||||||
|
const joinCommands = (commands: string[]) => commands.join("; ");
|
||||||
const echoAfterDelay = (message: string) =>
|
const echoAfterDelay = (message: string) =>
|
||||||
joinCommands([shortDelayCmd, `echo ${message}`]);
|
joinCommands([shortDelayCmd, `echo ${message}`]);
|
||||||
const echoLines = (lines: string[]) =>
|
const echoLines = (lines: string[]) =>
|
||||||
|
|||||||
@@ -265,15 +265,17 @@ export function createBashTool(
|
|||||||
{
|
{
|
||||||
cwd: workdir,
|
cwd: workdir,
|
||||||
env: process.env,
|
env: process.env,
|
||||||
detached: true,
|
detached: process.platform !== "win32",
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
windowsHide: true,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
: spawn(shell, [...shellArgs, params.command], {
|
: spawn(shell, [...shellArgs, params.command], {
|
||||||
cwd: workdir,
|
cwd: workdir,
|
||||||
env,
|
env,
|
||||||
detached: true,
|
detached: process.platform !== "win32",
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
windowsHide: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const session = {
|
const session = {
|
||||||
|
|||||||
@@ -2,8 +2,15 @@ import { spawn } from "node:child_process";
|
|||||||
|
|
||||||
export function getShellConfig(): { shell: string; args: string[] } {
|
export function getShellConfig(): { shell: string; args: string[] } {
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
const shell = process.env.COMSPEC?.trim() || "cmd.exe";
|
// Use PowerShell instead of cmd.exe on Windows.
|
||||||
return { shell, args: ["/d", "/s", "/c"] };
|
// 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";
|
const shell = process.env.SHELL?.trim() || "sh";
|
||||||
|
|||||||
Reference in New Issue
Block a user