diff --git a/packages/cli/src/services/prompt-processors/shellProcessor.test.ts b/packages/cli/src/services/prompt-processors/shellProcessor.test.ts index fa2afe4fd..c47758574 100644 --- a/packages/cli/src/services/prompt-processors/shellProcessor.test.ts +++ b/packages/cli/src/services/prompt-processors/shellProcessor.test.ts @@ -19,6 +19,19 @@ import type { PromptPipelineContent } from './types.js'; // mirroring the logic in the actual `escapeShellArg` implementation. function getExpectedEscapedArgForPlatform(arg: string): string { if (os.platform() === 'win32') { + // Detect Git Bash / MSYS2 / MinTTY environments (same logic as getShellConfiguration) + const msystem = process.env['MSYSTEM']; + const term = process.env['TERM'] || ''; + const isGitBash = + msystem?.startsWith('MINGW') || + msystem?.startsWith('MSYS') || + term.includes('msys') || + term.includes('cygwin'); + + if (isGitBash) { + return quote([arg]); + } + const comSpec = (process.env['ComSpec'] || 'cmd.exe').toLowerCase(); const isPowerShell = comSpec.endsWith('powershell.exe') || comSpec.endsWith('pwsh.exe'); diff --git a/packages/core/src/utils/shell-utils.test.ts b/packages/core/src/utils/shell-utils.test.ts index 7485384f8..8224f9950 100644 --- a/packages/core/src/utils/shell-utils.test.ts +++ b/packages/core/src/utils/shell-utils.test.ts @@ -556,12 +556,20 @@ describe('getShellConfiguration', () => { }); describe('on Windows', () => { + const originalEnv = { ...process.env }; + beforeEach(() => { mockPlatform.mockReturnValue('win32'); }); + afterEach(() => { + process.env = originalEnv; + }); + it('should return cmd.exe configuration by default', () => { delete process.env['ComSpec']; + delete process.env['MSYSTEM']; + delete process.env['TERM']; const config = getShellConfiguration(); expect(config.executable).toBe('cmd.exe'); expect(config.argsPrefix).toEqual(['/d', '/s', '/c']); @@ -571,6 +579,8 @@ describe('getShellConfiguration', () => { it('should respect ComSpec for cmd.exe', () => { const cmdPath = 'C:\\WINDOWS\\system32\\cmd.exe'; process.env['ComSpec'] = cmdPath; + delete process.env['MSYSTEM']; + delete process.env['TERM']; const config = getShellConfiguration(); expect(config.executable).toBe(cmdPath); expect(config.argsPrefix).toEqual(['/d', '/s', '/c']); @@ -581,6 +591,8 @@ describe('getShellConfiguration', () => { const psPath = 'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'; process.env['ComSpec'] = psPath; + delete process.env['MSYSTEM']; + delete process.env['TERM']; const config = getShellConfiguration(); expect(config.executable).toBe(psPath); expect(config.argsPrefix).toEqual(['-NoProfile', '-Command']); @@ -590,6 +602,8 @@ describe('getShellConfiguration', () => { it('should return PowerShell configuration if ComSpec points to pwsh.exe', () => { const pwshPath = 'C:\\Program Files\\PowerShell\\7\\pwsh.exe'; process.env['ComSpec'] = pwshPath; + delete process.env['MSYSTEM']; + delete process.env['TERM']; const config = getShellConfiguration(); expect(config.executable).toBe(pwshPath); expect(config.argsPrefix).toEqual(['-NoProfile', '-Command']); @@ -598,11 +612,76 @@ describe('getShellConfiguration', () => { it('should be case-insensitive when checking ComSpec', () => { process.env['ComSpec'] = 'C:\\Path\\To\\POWERSHELL.EXE'; + delete process.env['MSYSTEM']; + delete process.env['TERM']; const config = getShellConfiguration(); expect(config.executable).toBe('C:\\Path\\To\\POWERSHELL.EXE'); expect(config.argsPrefix).toEqual(['-NoProfile', '-Command']); expect(config.shell).toBe('powershell'); }); + + describe('Git Bash / MSYS2 / MinTTY detection', () => { + it('should return bash configuration when MSYSTEM starts with MINGW', () => { + process.env['MSYSTEM'] = 'MINGW64'; + const config = getShellConfiguration(); + expect(config.executable).toBe('bash'); + expect(config.argsPrefix).toEqual(['-c']); + expect(config.shell).toBe('bash'); + }); + + it('should return bash configuration when MSYSTEM starts with MSYS', () => { + process.env['MSYSTEM'] = 'MSYS'; + const config = getShellConfiguration(); + expect(config.executable).toBe('bash'); + expect(config.argsPrefix).toEqual(['-c']); + expect(config.shell).toBe('bash'); + }); + + it('should return bash configuration when TERM includes msys', () => { + delete process.env['MSYSTEM']; + process.env['TERM'] = 'xterm-256color-msys'; + const config = getShellConfiguration(); + expect(config.executable).toBe('bash'); + expect(config.argsPrefix).toEqual(['-c']); + expect(config.shell).toBe('bash'); + }); + + it('should return bash configuration when TERM includes cygwin', () => { + delete process.env['MSYSTEM']; + process.env['TERM'] = 'xterm-256color-cygwin'; + const config = getShellConfiguration(); + expect(config.executable).toBe('bash'); + expect(config.argsPrefix).toEqual(['-c']); + expect(config.shell).toBe('bash'); + }); + + it('should prioritize MSYSTEM over TERM for Git Bash detection', () => { + process.env['MSYSTEM'] = 'MINGW64'; + process.env['TERM'] = 'xterm'; + const config = getShellConfiguration(); + expect(config.executable).toBe('bash'); + expect(config.argsPrefix).toEqual(['-c']); + expect(config.shell).toBe('bash'); + }); + + it('should return cmd.exe when MSYSTEM and TERM do not indicate Git Bash', () => { + process.env['MSYSTEM'] = 'UNKNOWN'; + process.env['TERM'] = 'xterm'; + delete process.env['ComSpec']; + const config = getShellConfiguration(); + expect(config.executable).toBe('cmd.exe'); + expect(config.argsPrefix).toEqual(['/d', '/s', '/c']); + expect(config.shell).toBe('cmd'); + }); + + it('should return bash when MSYSTEM is MINGW32', () => { + process.env['MSYSTEM'] = 'MINGW32'; + const config = getShellConfiguration(); + expect(config.executable).toBe('bash'); + expect(config.argsPrefix).toEqual(['-c']); + expect(config.shell).toBe('bash'); + }); + }); }); }); diff --git a/packages/core/src/utils/shell-utils.ts b/packages/core/src/utils/shell-utils.ts index c30e55493..1ef1dd09e 100644 --- a/packages/core/src/utils/shell-utils.ts +++ b/packages/core/src/utils/shell-utils.ts @@ -48,6 +48,24 @@ export interface ShellConfiguration { */ export function getShellConfiguration(): ShellConfiguration { if (isWindows()) { + // Detect Git Bash / MSYS2 / MinTTY environments + // These environments should use bash instead of cmd/PowerShell + const msystem = process.env['MSYSTEM']; + const term = process.env['TERM'] || ''; + const isGitBash = + msystem?.startsWith('MINGW') || + msystem?.startsWith('MSYS') || + term.includes('msys') || + term.includes('cygwin'); + + if (isGitBash) { + return { + executable: 'bash', + argsPrefix: ['-c'], + shell: 'bash', + }; + } + const comSpec = process.env['ComSpec'] || 'cmd.exe'; const executable = comSpec.toLowerCase();