import { quote } from './shellQuote.js' /** * Detects if a command contains a heredoc pattern * Matches patterns like: <nul` redirects to POSIX `/dev/null`. * * The model occasionally hallucinates Windows CMD syntax (e.g., `ls 2>nul`) * even though our bash shell is always POSIX (Git Bash / WSL on Windows). * When Git Bash sees `2>nul`, it creates a literal file named `nul` — a * Windows reserved device name that is extremely hard to delete and breaks * `git add .` and `git clone`. See anthropics/claude-code#4928. * * Matches: `>nul`, `> NUL`, `2>nul`, `&>nul`, `>>nul` (case-insensitive) * Does NOT match: `>null`, `>nullable`, `>nul.txt`, `cat nul.txt` * * Limitation: this regex does not parse shell quoting, so `echo ">nul"` * will also be rewritten. This is acceptable collateral — it's extremely * rare and rewriting to `/dev/null` inside a string is harmless. */ const NUL_REDIRECT_REGEX = /(\d?&?>+\s*)[Nn][Uu][Ll](?=\s|$|[|&;)\n])/g export function rewriteWindowsNullRedirect(command: string): string { return command.replace(NUL_REDIRECT_REGEX, '$1/dev/null') }