fix: Use Git Bash on Windows instead of hardcoded /bin/sh for command execution

This commit is contained in:
Tushar Magar 2026-03-05 17:49:32 +05:30
parent 720e2c4d6e
commit c30e7bb2f7

View file

@ -1,8 +1,22 @@
import { exec, execSync, spawn, ChildProcess } from 'child_process'; import { exec, execSync, spawn, ChildProcess } from 'child_process';
import { existsSync } from 'fs';
import { promisify } from 'util'; import { promisify } from 'util';
import { getSecurityAllowList } from '../../config/security.js'; import { getSecurityAllowList } from '../../config/security.js';
const execPromise = promisify(exec); const execPromise = promisify(exec);
function getShell(): string {
if (process.platform !== 'win32') return '/bin/sh';
// On Windows, try Git Bash first, then fall back to cmd.exe
const gitBashPaths = [
'C:\\Program Files\\Git\\bin\\bash.exe',
'C:\\Program Files (x86)\\Git\\bin\\bash.exe',
];
for (const p of gitBashPaths) {
if (existsSync(p)) return p;
}
return 'cmd.exe';
}
const COMMAND_SPLIT_REGEX = /(?:\|\||&&|;|\||\n|`|\$\(|\(|\))/; const COMMAND_SPLIT_REGEX = /(?:\|\||&&|;|\||\n|`|\$\(|\(|\))/;
const ENV_ASSIGNMENT_REGEX = /^[A-Za-z_][A-Za-z0-9_]*=.*/; const ENV_ASSIGNMENT_REGEX = /^[A-Za-z_][A-Za-z0-9_]*=.*/;
const WRAPPER_COMMANDS = new Set(['sudo', 'env', 'time', 'command']); const WRAPPER_COMMANDS = new Set(['sudo', 'env', 'time', 'command']);
@ -85,7 +99,7 @@ export async function executeCommand(
cwd: options?.cwd, cwd: options?.cwd,
timeout: options?.timeout, timeout: options?.timeout,
maxBuffer: options?.maxBuffer || 1024 * 1024, // default 1MB maxBuffer: options?.maxBuffer || 1024 * 1024, // default 1MB
shell: '/bin/sh', // use sh for cross-platform compatibility shell: getShell(), // use sh for cross-platform compatibility
}); });
return { return {
@ -159,7 +173,7 @@ export function executeCommandAbortable(
} }
const proc = spawn(command, [], { const proc = spawn(command, [], {
shell: '/bin/sh', shell: getShell(),
cwd: options?.cwd, cwd: options?.cwd,
detached: process.platform !== 'win32', // Create process group on Unix detached: process.platform !== 'win32', // Create process group on Unix
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
@ -273,7 +287,7 @@ export function executeCommandSync(
cwd: options?.cwd, cwd: options?.cwd,
timeout: options?.timeout, timeout: options?.timeout,
encoding: 'utf-8', encoding: 'utf-8',
shell: '/bin/sh', shell: getShell(),
}); });
return { return {