diff --git a/src/main/git/source-control/discard-changes.ts b/src/main/git/source-control/discard-changes.ts index 407e2289db9f..c612faefaf38 100644 --- a/src/main/git/source-control/discard-changes.ts +++ b/src/main/git/source-control/discard-changes.ts @@ -32,19 +32,16 @@ export async function discardChanges( let tracked = false try { - await gitExecFileAsync( - ['ls-files', '--error-unmatch', '--', literalPathspec(filePath, options)], - { - ...gitOptionsForWorktree(worktreePath, options) - } - ) + await gitExecFileAsync(['ls-files', '--error-unmatch', '--', literalPathspec(filePath)], { + ...gitOptionsForWorktree(worktreePath, options) + }) tracked = true } catch { // File is not tracked by git } if (tracked) { - await gitExecFileAsync(['restore', '--worktree', '--', literalPathspec(filePath, options)], { + await gitExecFileAsync(['restore', '--worktree', '--', literalPathspec(filePath)], { ...gitOptionsForWorktree(worktreePath, options) }) await redoWriteForDriveCheckoutSuspects(worktreePath, [filePath], DISCARD_WRITE, options) @@ -133,9 +130,7 @@ export async function bulkDiscardChanges( ['restore', '--worktree', '--pathspec-from-file=-', '--pathspec-file-nul'], { ...gitOptionsForWorktree(worktreePath, options), - stdin: encodeGitPathspecs( - trackedPaths.map((filePath) => literalPathspec(filePath, options)) - ) + stdin: encodeGitPathspecs(trackedPaths.map(literalPathspec)) } ) await redoWriteForDriveCheckoutSuspects(worktreePath, trackedPaths, DISCARD_WRITE, options) diff --git a/src/main/git/source-control/git-pathspec.ts b/src/main/git/source-control/git-pathspec.ts index 7a31b8f33968..4c1affacdcda 100644 --- a/src/main/git/source-control/git-pathspec.ts +++ b/src/main/git/source-control/git-pathspec.ts @@ -17,10 +17,9 @@ const BULK_CHUNK_SIZE = 100 */ const POSIX_COMMAND_LINE_BUDGET = 128_000 -export function literalPathspec(filePath: string, options: GitRuntimeOptions): string { - // Why: Git inside WSL needs POSIX paths, but host paths must stay literal, so convert backslashes only for WSL. - const runtimePath = options.wslDistro ? filePath.replace(/\\/g, '/') : filePath - return `:(literal)${runtimePath}` +// Why no separator rewrite: callers pass Git-spelled paths, so a `\` here is a POSIX filename character. +export function literalPathspec(filePath: string): string { + return `:(literal)${filePath}` } /** @@ -76,7 +75,7 @@ export function bulkPathspecCommands( let pathspecs: string[] = [] let length = baseLength for (const filePath of filePaths) { - const pathspec = literalPathspec(filePath, options) + const pathspec = literalPathspec(filePath) const cost = finishedCommandLineLength([...leadingArgs, pathspec], worktreePath, options) - baseLength if (pathspecs.length > 0 && (pathspecs.length >= BULK_CHUNK_SIZE || length + cost > budget)) { diff --git a/src/main/git/source-control/staging.ts b/src/main/git/source-control/staging.ts index 371b46c69d2d..f49935cb7bde 100644 --- a/src/main/git/source-control/staging.ts +++ b/src/main/git/source-control/staging.ts @@ -25,7 +25,7 @@ export async function stageFile( invalidateGitReadCaches() try { await gitExecFileAsync( - ['add', '--', literalPathspec(filePath, options)], + ['add', '--', literalPathspec(filePath)], gitOptionsForWorktree(worktreePath, options) ) await redoWriteForDriveCheckoutSuspects(worktreePath, [filePath], STAGE_WRITE, options) @@ -45,7 +45,7 @@ export async function unstageFile( invalidateGitReadCaches() try { // Reset treats an unborn HEAD as an empty tree, preserving the working file. - await gitExecFileAsync(['reset', '--quiet', '--', literalPathspec(filePath, options)], { + await gitExecFileAsync(['reset', '--quiet', '--', literalPathspec(filePath)], { ...gitOptionsForWorktree(worktreePath, options) }) } finally { @@ -68,7 +68,7 @@ export async function bulkStageFiles( try { await gitExecFileAsync(['add', '--pathspec-from-file=-', '--pathspec-file-nul'], { ...gitOptionsForWorktree(worktreePath, options), - stdin: encodeGitPathspecs(filePaths.map((filePath) => literalPathspec(filePath, options))) + stdin: encodeGitPathspecs(filePaths.map(literalPathspec)) }) await redoWriteForDriveCheckoutSuspects(worktreePath, filePaths, STAGE_WRITE, options) } finally { @@ -123,7 +123,7 @@ export async function bulkUnstageFiles( try { await gitExecFileAsync(['reset', '--quiet', '--pathspec-from-file=-', '--pathspec-file-nul'], { ...gitOptionsForWorktree(worktreePath, options), - stdin: encodeGitPathspecs(filePaths.map((filePath) => literalPathspec(filePath, options))) + stdin: encodeGitPathspecs(filePaths.map(literalPathspec)) }) } finally { invalidateGitReadCaches() diff --git a/src/main/git/status-wsl-pathspecs.test.ts b/src/main/git/status-wsl-pathspecs.test.ts index c735bb42493b..750ead555556 100644 --- a/src/main/git/status-wsl-pathspecs.test.ts +++ b/src/main/git/status-wsl-pathspecs.test.ts @@ -24,7 +24,7 @@ import { } from './status' const worktreePath = path.resolve('repo') -const windowsRelativePath = 'tests\\breakgit' +const gitRelativePath = 'tests/breakgit' const wslOptions = { wslDistro: 'Ubuntu' } describe('WSL git pathspecs', () => { @@ -33,13 +33,13 @@ describe('WSL git pathspecs', () => { gitExecFileAsyncMock.mockResolvedValue({ stdout: 'tests/breakgit\0', stderr: '' }) }) - it('uses POSIX separators for tracked file mutations executed inside WSL', async () => { - await stageFile(worktreePath, windowsRelativePath, wslOptions) - await unstageFile(worktreePath, windowsRelativePath, wslOptions) - await bulkStageFiles(worktreePath, [windowsRelativePath], wslOptions) - await bulkUnstageFiles(worktreePath, [windowsRelativePath], wslOptions) - await discardChanges(worktreePath, windowsRelativePath, wslOptions) - await bulkDiscardChanges(worktreePath, [windowsRelativePath], wslOptions) + it('passes Git-spelled paths to tracked file mutations executed inside WSL', async () => { + await stageFile(worktreePath, gitRelativePath, wslOptions) + await unstageFile(worktreePath, gitRelativePath, wslOptions) + await bulkStageFiles(worktreePath, [gitRelativePath], wslOptions) + await bulkUnstageFiles(worktreePath, [gitRelativePath], wslOptions) + await discardChanges(worktreePath, gitRelativePath, wslOptions) + await bulkDiscardChanges(worktreePath, [gitRelativePath], wslOptions) const pathspecs = gitExecFileAsyncMock.mock.calls.flatMap(([args]) => (args as string[]).filter((arg) => arg.startsWith(':(literal)')) @@ -51,9 +51,9 @@ describe('WSL git pathspecs', () => { expect(inputs).toEqual(Array(3).fill(':(literal)tests/breakgit\0')) }) - it('uses POSIX separators when discarding untracked files inside WSL', async () => { + it('passes Git-spelled paths when discarding untracked files inside WSL', async () => { const repo = await mkdtemp(path.join(tmpdir(), 'orca-wsl-pathspec-')) - const targetPath = path.resolve(repo, windowsRelativePath) + const targetPath = path.resolve(repo, gitRelativePath) await mkdir(path.dirname(targetPath), { recursive: true }) await writeFile(targetPath, 'untracked') @@ -61,7 +61,7 @@ describe('WSL git pathspecs', () => { gitExecFileAsyncMock .mockRejectedValueOnce(new Error('not tracked')) .mockResolvedValue({ stdout: '', stderr: '' }) - await discardChanges(repo, windowsRelativePath, wslOptions) + await discardChanges(repo, gitRelativePath, wslOptions) expect(gitExecFileAsyncMock.mock.calls.map(([args]) => args)).toEqual([ ['ls-files', '--error-unmatch', '--', ':(literal)tests/breakgit'], @@ -70,7 +70,7 @@ describe('WSL git pathspecs', () => { gitExecFileAsyncMock.mockReset() gitExecFileAsyncMock.mockResolvedValue({ stdout: '', stderr: '' }) - await bulkDiscardChanges(repo, [windowsRelativePath], wslOptions) + await bulkDiscardChanges(repo, [gitRelativePath], wslOptions) expect(gitExecFileAsyncMock.mock.calls.map(([args]) => args)).toEqual([ ['ls-files', '-z', '--', ':(literal)tests/breakgit'], @@ -81,11 +81,15 @@ describe('WSL git pathspecs', () => { } }) - it('preserves backslashes for host Git where they can be literal filename characters', async () => { - await stageFile(worktreePath, windowsRelativePath) + it.each([ + ['host Git', {}], + ['WSL Git', wslOptions] + ])('keeps a backslash literal for %s, where it is a filename character', async (_, options) => { + await stageFile(worktreePath, 'tests\\breakgit', options) - expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['add', '--', ':(literal)tests\\breakgit'], { - cwd: worktreePath - }) + expect(gitExecFileAsyncMock).toHaveBeenCalledWith( + ['add', '--', ':(literal)tests\\breakgit'], + expect.anything() + ) }) }) diff --git a/src/main/ipc/filesystem-auth.test.ts b/src/main/ipc/filesystem-auth.test.ts index 137051c9e331..1c12de43f237 100644 --- a/src/main/ipc/filesystem-auth.test.ts +++ b/src/main/ipc/filesystem-auth.test.ts @@ -377,10 +377,19 @@ describe('filesystem-auth path containment', () => { it('allows git-relative files under dotdot-prefixed child directories', () => { expect(validateGitRelativeFilePath(resolve('/workspace/repo'), '..fixtures/file.ts')).toBe( - join('..fixtures', 'file.ts') + '..fixtures/file.ts' ) }) + it.skipIf(process.platform === 'win32')( + 'keeps a backslash on POSIX, where it is a filename character', + () => { + expect(validateGitRelativeFilePath(resolve('/workspace/repo'), 'tests/a\\b.ts')).toBe( + 'tests/a\\b.ts' + ) + } + ) + it('still rejects parent-directory escapes', () => { const root = resolve('/workspace/repo') const outside = resolve('/workspace/repo/../other/file.ts') @@ -421,4 +430,36 @@ describe('filesystem-auth path containment', () => { vi.resetModules() } }) + + // #28422: a `\\wsl.localhost` worktree runs Linux Git even when the project's runtime says Windows. + it('spells Windows git-relative paths with forward slashes', async () => { + vi.resetModules() + vi.doMock('path', async () => { + const path = await vi.importActual('node:path') + return { + ...path.win32, + default: path.win32 + } + }) + + try { + const { validateGitRelativeFilePath: validateWithWinPath } = + await import('./filesystem-path-containment') + + for (const host of ['wsl.localhost', 'wsl$']) { + expect( + validateWithWinPath( + String.raw`\\${host}\Ubuntu-24.04\home\dev\repo`, + String.raw`.claude\settings.json` + ) + ).toBe('.claude/settings.json') + } + expect(validateWithWinPath(String.raw`C:\repo`, String.raw`src\.\nested\app.ts`)).toBe( + 'src/nested/app.ts' + ) + } finally { + vi.doUnmock('path') + vi.resetModules() + } + }) }) diff --git a/src/main/ipc/filesystem-branch-compare-diff.test.ts b/src/main/ipc/filesystem-branch-compare-diff.test.ts index 86be671848bc..ed80a12a529d 100644 --- a/src/main/ipc/filesystem-branch-compare-diff.test.ts +++ b/src/main/ipc/filesystem-branch-compare-diff.test.ts @@ -226,15 +226,13 @@ describe('registerFilesystemHandlers', () => { oldPath: 'src/old-file.ts' }) - // Why: validateGitRelativeFilePath uses path.relative() which produces - // platform-specific separators (backslashes on Windows). expect(getBranchDiffMock).toHaveBeenCalledWith( WORKTREE_FEATURE_PATH, { headOid: 'head-oid', mergeBase: 'merge-base-oid', - filePath: path.join('src', 'file.ts'), - oldPath: path.join('src', 'old-file.ts') + filePath: 'src/file.ts', + oldPath: 'src/old-file.ts' }, { admissionTier: 'interactive' } ) diff --git a/src/main/ipc/filesystem-git-status-staging.test.ts b/src/main/ipc/filesystem-git-status-staging.test.ts index dea7d29d00a3..0ef63c15765b 100644 --- a/src/main/ipc/filesystem-git-status-staging.test.ts +++ b/src/main/ipc/filesystem-git-status-staging.test.ts @@ -1,5 +1,4 @@ import { EventEmitter } from 'node:events' -import path from 'node:path' import { beforeEach, describe, expect, it, vi } from 'vitest' import { handlers, @@ -86,9 +85,8 @@ describe('registerFilesystemHandlers', () => { filePath: './src/../src/file.ts' }) - // Why: validateGitRelativeFilePath uses path.relative() which produces - // platform-specific separators (backslashes on Windows). - expect(stageFileMock).toHaveBeenCalledWith(WORKTREE_FEATURE_PATH, path.join('src', 'file.ts'), { + // Why: Git spells paths with `/` on every host, so Windows separators must not leak through. + expect(stageFileMock).toHaveBeenCalledWith(WORKTREE_FEATURE_PATH, 'src/file.ts', { admissionTier: 'interactive' }) }) @@ -297,7 +295,7 @@ describe('registerFilesystemHandlers', () => { expect(checkIgnoredPathsMock).toHaveBeenCalledWith( WORKTREE_FEATURE_PATH, - [path.join('dist', 'bundle.js'), path.join('src', 'index.ts')], + ['dist/bundle.js', 'src/index.ts'], {} ) }) @@ -380,7 +378,7 @@ describe('registerFilesystemHandlers', () => { expect(bulkStageFilesMock).toHaveBeenCalledWith( WORKTREE_FEATURE_PATH, - [path.join('src', 'file.ts'), path.join('nested', 'child.ts')], + ['src/file.ts', 'nested/child.ts'], { admissionTier: 'interactive' } ) }) @@ -438,7 +436,7 @@ describe('registerFilesystemHandlers', () => { expect(bulkDiscardChangesMock).toHaveBeenCalledWith( WORKTREE_FEATURE_PATH, - [path.join('src', 'file.ts'), path.join('nested', 'child.ts')], + ['src/file.ts', 'nested/child.ts'], { admissionTier: 'interactive' } ) }) diff --git a/src/main/ipc/filesystem-path-containment.ts b/src/main/ipc/filesystem-path-containment.ts index c696028cbc85..7b89e0bccdd0 100644 --- a/src/main/ipc/filesystem-path-containment.ts +++ b/src/main/ipc/filesystem-path-containment.ts @@ -134,6 +134,7 @@ export async function normalizeExistingPath(resolvedPath: string): Promise