[BUG] Windows (MSYS2/Zsh): Unescaped backslashes in 'cli.js' break shell snapshot integration
What's Wrong?
When running Claude Code in a Windows MSYS2/Zsh environment, a syntax error (zsh: unmatched ') occurs immediately after launch and persists with every command execution (both manual ! cmd and agent tool use).
This error originates from the dynamically generated shell snapshot script. Because of this syntax error, the snapshot script may fail to fully load, compromising the integrity of the shell state preservation (aliases, exports, etc.) between steps.
Root Cause: In cli.js, the logic detects a native Windows path for ripgrep (e.g., C:\Users\...\rg.exe) and injects it into the Zsh snapshot script without escaping backslashes.
What Should Happen?
The tool should properly sanitize Windows paths (convert \ to /) or use robust quoting before injecting them into a Zsh script. Alternatively, it should prefer the shell's native rg (/usr/bin/rg) if available.
Error Messages/Logs
# Example output when running '! echo hi' inside Claude Code
! echo hi
⎿ hi
/c/Users/username/.claude/shell-snapshots/snapshot-zsh-1769886418591.sh:10: unmatched '
Steps to Reproduce
- Open terminal (MSYS2/Zsh) on Windows.
- Ensure
ripgrep(rg.exe) is detected in the Windows PATH (triggering the aliasing logic incli.js). - Run
claude. - Execute a command:
! echo hi(or ask the agent to run a command). - Result: Output is displayed, followed by the
unmatched 'syntax error from the snapshot file.
Claude Model
Not sure / Multiple models
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.27 (Claude Code)
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
Other
Additional Information
The Problematic Logic:
// In cli.js, variable ${z} contains backslashes (Windows path) but is not escaped.
echo ' alias rg='"'${z}'" >> "$SNAPSHOT_FILE"
The Generated Broken Script:
# The backslash escapes the following character, breaking the single-quote structure.
alias rg=''\''C:sers\username\.local\bin\rg.exe'
Verified Fix: I manually patched cli.js to use a safe path, which resolved the issue immediately.
// Convert backslashes (\) to forward slashes (/) which are safe in MSYS2/Zsh
const safePath = z.replace(/\\/g, '/');
echo ' alias rg="' + safePath + '"' >> "$SNAPSHOT_FILE"
Screenshots (Before vs After)
Every command triggers a syntax error in the snapshot script, polluting the output.
Before Patch (Current Bug):
<img width="1045" height="80" alt="Image" src="https://github.com/user-attachments/assets/cb8e9fda-27df-40f8-b2f2-00fed4e382e5" />
After Patch (Verified Fix):
With the cli.js fix applied, the shell environment loads correctly without any errors.
<img width="116" height="54" alt="Image" src="https://github.com/user-attachments/assets/2cbc5429-4f71-4f6a-acb3-18b6a5a8c6bc" />
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗