fix: normalize Windows backslash paths to forward slashes for git#21
fix: normalize Windows backslash paths to forward slashes for git#21quyentho wants to merge 1 commit intomolon:mainfrom
Conversation
On Windows, path.relative() returns backslash-separated paths which git rejects in update-index and other commands with "Invalid path" errors. Added toGitPath() helper that converts all relative paths to forward slashes before passing them to git operations. Fixes snapshotBatch, snapshot, removeFile, removeFileBatch, renameFile, and getBaseline on Windows. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes Windows failures in the extension’s internal git repo by ensuring file paths passed to git use forward slashes (git pathspec format), even when path.relative() returns backslash-separated paths on Windows.
Changes:
- Added
toGitPath()helper to convert\to/for git compatibility. - Applied
toGitPath()to all relative-path values derived frompath.relative()before they’re used in git commands (snapshot, batch ops, remove, rename, getBaseline). - Normalized parsed
ls-filesoutput paths throughtoGitPath()as well.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /** | ||
| * Convert Windows backslash paths to forward slashes for git compatibility. | ||
| * Git expects forward slashes on all platforms. | ||
| */ | ||
| function toGitPath(p: string): string { | ||
| return p.replace(/\\/g, '/'); | ||
| } |
There was a problem hiding this comment.
There are existing unit tests for HunkwiseGit, but this Windows-specific path fix isn’t covered. Please add a test that exercises the new backslash→slash conversion (e.g., temporarily monkeypatch path.relative to return a value with \\ and assert snapshot()/getBaseline() (and ideally removeFile/renameFile) still succeed). This will prevent regressions while running the test suite on non-Windows CI.
| * Convert Windows backslash paths to forward slashes for git compatibility. | ||
| * Git expects forward slashes on all platforms. | ||
| */ | ||
| function toGitPath(p: string): string { | ||
| return p.replace(/\\/g, '/'); |
There was a problem hiding this comment.
toGitPath() duplicates the same backslash-to-slash normalization logic that already exists elsewhere (e.g. FileWatcher uses .replace(/\\/g, '/')). Consider centralizing this into a shared path utility (and reusing it from both places) so future path-handling fixes don’t diverge.
| * Convert Windows backslash paths to forward slashes for git compatibility. | |
| * Git expects forward slashes on all platforms. | |
| */ | |
| function toGitPath(p: string): string { | |
| return p.replace(/\\/g, '/'); | |
| * Convert paths to git-compatible forward-slash form. | |
| * Git expects forward slashes on all platforms. | |
| */ | |
| function toGitPath(p: string): string { | |
| return normalizePath(p); |
Summary
path.relative()returns backslash-separated paths (e.g..vscode\launch.json), but git requires forward slashes on all platformserror: Invalid path '.vscode\launch.json'toGitPath()helper that converts backslashes to forward slashes, applied to all relative paths before they are passed to git inhunkwiseGit.tsAffected operations
All
HunkwiseGitmethods that compute a relative path frompath.relative():snapshot()— stage a single file baselinesnapshotBatch()— stage multiple file baselines at onceremoveFile()— remove a single baselineremoveFileBatch()— remove multiple baselines at oncerenameFile()— rename a baseline entrygetBaseline()— read a baseline from the indexTest plan
error: Invalid pathentries🤖 Generated with Claude Code