Skip to content

fix: normalize Windows backslash paths to forward slashes for git#21

Open
quyentho wants to merge 1 commit intomolon:mainfrom
quyentho:fix/windows-path-normalization
Open

fix: normalize Windows backslash paths to forward slashes for git#21
quyentho wants to merge 1 commit intomolon:mainfrom
quyentho:fix/windows-path-normalization

Conversation

@quyentho
Copy link
Copy Markdown

Summary

  • On Windows, path.relative() returns backslash-separated paths (e.g. .vscode\launch.json), but git requires forward slashes on all platforms
  • Without this fix, all git index operations fail on Windows with errors like error: Invalid path '.vscode\launch.json'
  • Added a toGitPath() helper that converts backslashes to forward slashes, applied to all relative paths before they are passed to git in hunkwiseGit.ts

Affected operations

All HunkwiseGit methods that compute a relative path from path.relative():

  • snapshot() — stage a single file baseline
  • snapshotBatch() — stage multiple file baselines at once
  • removeFile() — remove a single baseline
  • removeFileBatch() — remove multiple baselines at once
  • renameFile() — rename a baseline entry
  • getBaseline() — read a baseline from the index

Test plan

  • Enable hunkwise on a Windows machine
  • Edit a tracked file externally — baseline should be staged without "Invalid path" errors in the output log
  • Accept / Discard a hunk — should complete successfully
  • Verify the extension output log is free of error: Invalid path entries

🤖 Generated with Claude Code

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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 from path.relative() before they’re used in git commands (snapshot, batch ops, remove, rename, getBaseline).
  • Normalized parsed ls-files output paths through toGitPath() as well.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/hunkwiseGit.ts
Comment on lines +9 to +15
/**
* 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, '/');
}
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/hunkwiseGit.ts
Comment on lines +10 to +14
* 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, '/');
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
* 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);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants