Skip to content

Fix git extension not working when repo root is "/" (chroot)#317637

Open
vanklompf wants to merge 5 commits into
microsoft:mainfrom
vanklompf:cursor/fix-root-path-trailing-slash-4e40
Open

Fix git extension not working when repo root is "/" (chroot)#317637
vanklompf wants to merge 5 commits into
microsoft:mainfrom
vanklompf:cursor/fix-root-path-trailing-slash-4e40

Conversation

@vanklompf
Copy link
Copy Markdown

@vanklompf vanklompf commented May 20, 2026

Problem

When working in a chroot environment where the project/repository root is /, the git extension fails silently — discard changes, diffs, and staging all break.

Root cause: The normalizePath() function in extensions/git/src/util.ts unconditionally strips trailing path separators. When the path is / (root), stripping the trailing / produces an empty string "", which path.normalize("") converts to ".". This breaks both isDescendant() and pathEquals():

// Before fix:
normalizePath("/")   "."     // WRONG — should be "/"
isDescendant("/", "/foo")   false  // WRONG — should be true

As a result, the git extension cannot match files to their repository when the repository root is /.

Fix

The fix now uses path !== dirname(path). This correctly detects all filesystem roots.

// After fix:
normalizePath("/")   "/"     // Correct
isDescendant("/", "/foo")   true  // Correct

This is consistent with how the core VS Code removeTrailingPathSeparator() in src/vs/base/common/extpath.ts already handles this case.

Testing

  • Added unit tests for isDescendant, pathEquals, and relativePath covering the root path / scenario.
  • Verified compilation passes with 0 errors.
  • Verified old behavior was broken and new behavior is correct.

cursoragent and others added 3 commits May 18, 2026 22:15
The normalizePath() function in the git extension's util.ts would strip
the trailing "/" from the root path to get an empty string, which
path.normalize() then converts to ".". This broke isDescendant() and
pathEquals() for any repository with root path "/", which is common in
chroot environments.

The fix adds a length > 1 check before stripping trailing separators,
preserving root paths like "/" on Linux/macOS (and "C:\" on Windows
where normalize restores the separator anyway).

Fixes microsoft#298003

Co-authored-by: Franek Korta <vanklompf@users.noreply.github.com>
Use dirname() to detect filesystem roots instead of a length > 1 check.
This correctly preserves all root paths: "/" on POSIX, "C:\" and UNC
roots on Windows, rather than relying on normalize() to restore them.

Co-authored-by: Franek Korta <vanklompf@users.noreply.github.com>
Add win32-guarded tests for isDescendant, pathEquals, and relativePath
covering C:\ drive roots, ensuring the dirname()-based root detection
does not regress on Windows.

Co-authored-by: Franek Korta <vanklompf@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 20, 2026 21:27
@vanklompf vanklompf changed the title Cursor/fix root path trailing slash 4e40 Fix git extension not working when repo root is "/" (chroot) May 20, 2026
Copy link
Copy Markdown
Contributor

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

This PR fixes path normalization in the built-in Git extension so that repository roots that are filesystem roots (notably / in chroot environments) are preserved during normalization, restoring correct behavior for descendant checks, path equality, and relative path computation.

Changes:

  • Update normalizePath() to avoid stripping the trailing separator when the input is a filesystem root (detected via path !== dirname(path)).
  • Add unit tests covering root-path behavior for isDescendant, pathEquals, and relativePath on both POSIX and Windows drive roots.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
extensions/git/src/util.ts Prevents root paths (e.g. /, C:\) from being normalized into incorrect non-root values by avoiding trailing-separator stripping for roots.
extensions/git/src/test/git.test.ts Adds unit coverage for root-path scenarios to ensure correct behavior in chroot and drive-root cases.

@vanklompf vanklompf marked this pull request as ready for review May 20, 2026 22:02
@vs-code-engineering
Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@lszomoru

Matched files:

  • extensions/git/src/test/git.test.ts
  • extensions/git/src/util.ts

@vanklompf
Copy link
Copy Markdown
Author

Fixes #298003
#298003

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.

4 participants