Skip to content

Commit 3a5d054

Browse files
authored
Require bash on Windows. Throw error if missing (#413)
1 parent f559866 commit 3a5d054

File tree

3 files changed

+131
-20
lines changed

3 files changed

+131
-20
lines changed

WINDOWS.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,60 @@ Codebuff checks GitHub for the latest release on first run. This fails when:
7979

8080
---
8181

82+
### Issue: "Bash is required but was not found" Error
83+
84+
**Symptom**:
85+
```
86+
Bash is required but was not found on this Windows system.
87+
```
88+
89+
**Cause**:
90+
Codebuff requires bash for command execution. This error appears when:
91+
- Git for Windows is not installed
92+
- You're not running inside WSL
93+
- bash.exe is not in your PATH
94+
95+
**Solutions**:
96+
97+
1. **Install Git for Windows** (recommended):
98+
- Download from https://git-scm.com/download/win
99+
- This installs `bash.exe` which Codebuff will automatically detect
100+
- Works in PowerShell, CMD, or Git Bash terminals
101+
102+
2. **Use WSL (Windows Subsystem for Linux)**:
103+
- Provides full Linux environment with native bash
104+
- Install: `wsl --install` in PowerShell (Admin)
105+
- Run codebuff inside WSL for best compatibility
106+
107+
3. **Set custom bash path** (advanced):
108+
- If bash.exe is installed in a non-standard location:
109+
```powershell
110+
set CODEBUFF_GIT_BASH_PATH=C:\path\to\bash.exe
111+
```
112+
113+
**Reference**: Issue [#274](https://github.com/CodebuffAI/codebuff/issues/274)
114+
115+
---
116+
82117
### Issue: Git Commands Fail on Windows
83118

84119
**Symptom**:
85120
Git operations (commit, rebase, complex commands) fail with syntax errors or unexpected behavior.
86121

87122
**Cause**:
88-
Codebuff uses Windows `cmd.exe` for command execution, which:
89-
- Does not support bash syntax (HEREDOC, process substitution)
90-
- Has limited quote escaping compared to bash
91-
- Cannot execute complex git commands that work in Git Bash
123+
Complex git commands may have issues with Windows path handling or shell escaping.
92124

93125
**Solutions**:
94126

95-
1. **Install Git for Windows** (if not already installed):
127+
1. **Ensure Git for Windows is installed**:
96128
- Download from https://git-scm.com/download/win
97-
- Ensures git commands are available in PATH
98-
99-
2. **Use Git Bash terminal** instead of PowerShell:
100-
- Git Bash provides better compatibility with bash-style commands
101-
- Launch Git Bash and run `codebuff` from there
129+
- Codebuff uses bash.exe from Git for Windows for command execution
102130

103-
3. **Or use WSL (Windows Subsystem for Linux)**:
131+
2. **Use WSL for complex operations**:
104132
- Provides full Linux environment with native bash
105133
- Install: `wsl --install` in PowerShell (Admin)
106134
- Run codebuff inside WSL for best compatibility
107135

108-
**Note**: Even when running in Git Bash, Codebuff spawns commands using `cmd.exe`. Using WSL provides the most reliable experience for git operations.
109-
110136
**Reference**: Issue [#274](https://github.com/CodebuffAI/codebuff/issues/274)
111137

112138
---

sdk/src/run-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ export async function initialSessionState(
502502
shellConfigFiles: {},
503503
systemInfo: {
504504
platform: process.platform,
505-
shell: process.platform === 'win32' ? 'cmd.exe' : 'bash',
505+
shell: 'bash',
506506
nodeVersion: process.version,
507507
arch: process.arch,
508508
homedir: os.homedir(),

sdk/src/tools/run-terminal-command.ts

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { spawn } from 'child_process'
2+
import * as fs from 'fs'
23
import * as os from 'os'
34
import * as path from 'path'
45

@@ -12,6 +13,75 @@ import type { CodebuffToolOutput } from '../../../common/src/tools/list'
1213

1314
const COMMAND_OUTPUT_LIMIT = 50_000
1415

16+
// Common locations where Git Bash might be installed on Windows
17+
const GIT_BASH_COMMON_PATHS = [
18+
'C:\\Program Files\\Git\\bin\\bash.exe',
19+
'C:\\Program Files (x86)\\Git\\bin\\bash.exe',
20+
'C:\\Git\\bin\\bash.exe',
21+
]
22+
23+
/**
24+
* Find bash executable on Windows.
25+
* Priority:
26+
* 1. CODEBUFF_GIT_BASH_PATH environment variable
27+
* 2. bash.exe in PATH (e.g., inside WSL or Git Bash terminal)
28+
* 3. Common Git Bash installation locations
29+
*/
30+
function findWindowsBash(env: NodeJS.ProcessEnv): string | null {
31+
// Check for user-specified path via environment variable
32+
const customPath = env.CODEBUFF_GIT_BASH_PATH
33+
if (customPath && fs.existsSync(customPath)) {
34+
return customPath
35+
}
36+
37+
// Check if bash.exe is in PATH (works inside WSL or Git Bash)
38+
const pathEnv = env.PATH || env.Path || ''
39+
const pathDirs = pathEnv.split(path.delimiter)
40+
41+
for (const dir of pathDirs) {
42+
const bashPath = path.join(dir, 'bash.exe')
43+
if (fs.existsSync(bashPath)) {
44+
return bashPath
45+
}
46+
// Also check for just 'bash' (for WSL)
47+
const bashPathNoExt = path.join(dir, 'bash')
48+
if (fs.existsSync(bashPathNoExt)) {
49+
return bashPathNoExt
50+
}
51+
}
52+
53+
// Check common Git Bash installation locations
54+
for (const commonPath of GIT_BASH_COMMON_PATHS) {
55+
if (fs.existsSync(commonPath)) {
56+
return commonPath
57+
}
58+
}
59+
60+
return null
61+
}
62+
63+
/**
64+
* Create an error message for Windows users when bash is not available.
65+
*/
66+
function createWindowsBashNotFoundError(): Error {
67+
return new Error(
68+
`Bash is required but was not found on this Windows system.
69+
70+
To fix this, you have several options:
71+
72+
1. Install Git for Windows (includes bash.exe):
73+
Download from: https://git-scm.com/download/win
74+
75+
2. Use WSL (Windows Subsystem for Linux):
76+
Run in PowerShell (Admin): wsl --install
77+
Then run Codebuff inside WSL.
78+
79+
3. Set a custom bash path:
80+
Set the CODEBUFF_GIT_BASH_PATH environment variable to your bash.exe location.
81+
Example: set CODEBUFF_GIT_BASH_PATH=C:\\path\\to\\bash.exe`,
82+
)
83+
}
84+
1585
export function runTerminalCommand({
1686
command,
1787
process_type,
@@ -31,18 +101,33 @@ export function runTerminalCommand({
31101

32102
return new Promise((resolve, reject) => {
33103
const isWindows = os.platform() === 'win32'
34-
const shell = isWindows ? 'cmd.exe' : 'bash'
35-
const shellArgs = isWindows ? ['/c'] : ['-c']
104+
const processEnv = {
105+
...getSystemProcessEnv(),
106+
...(env ?? {}),
107+
} as NodeJS.ProcessEnv
108+
109+
let shell: string
110+
let shellArgs: string[]
111+
112+
if (isWindows) {
113+
const bashPath = findWindowsBash(processEnv)
114+
if (!bashPath) {
115+
reject(createWindowsBashNotFoundError())
116+
return
117+
}
118+
shell = bashPath
119+
shellArgs = ['-c']
120+
} else {
121+
shell = 'bash'
122+
shellArgs = ['-c']
123+
}
36124

37125
// Resolve cwd to absolute path
38126
const resolvedCwd = path.resolve(cwd)
39127

40128
const childProcess = spawn(shell, [...shellArgs, command], {
41129
cwd: resolvedCwd,
42-
env: {
43-
...getSystemProcessEnv(),
44-
...(env ?? {}),
45-
} as NodeJS.ProcessEnv,
130+
env: processEnv,
46131
stdio: 'pipe',
47132
})
48133

0 commit comments

Comments
 (0)