fix(cli): preserve WSL cwd through the Windows bridge (#6965) (#7640)

* fix(cli): preserve WSL cwd through the Windows bridge (#6965)

# Conflicts:
#	src/cli/index.test.ts
#	src/cli/index.ts

* fix(cli): preserve bridge exit codes (#6965)

* fix(cli): harden WSL cwd bridge compatibility

* chore(cli): align cwd tests with main

* fix(cli): repair deleted WSL cwd before path conversion

* chore: preserve main formatting after merge

---------

Co-authored-by: Brennan Benson <brennanbenson@Brennans-MacBook-Pro.local>
Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
This commit is contained in:
Rod Boev
2026-07-13 15:44:32 -07:00
committed by GitHub
co-authored by Brennan Benson Brennan Benson
parent 53a09afbef
commit f90cd6ebc9
5 changed files with 110 additions and 28 deletions
+33
View File
@@ -29,6 +29,39 @@ describe('cross-platform path containment', () => {
expect(isPathInsideOrEqual('\\\\Server\\Share\\Repo', '\\\\server\\share\\repo2')).toBe(false)
})
it('treats WSL UNC aliases as the same case-sensitive filesystem', () => {
expect(
isPathInsideOrEqual(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\Alice\\repo\\src'
)
).toBe(true)
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\Alice\\repo\\Src'
)
).toBe('Src')
expect(
isPathInsideOrEqual(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\alice\\repo\\src'
)
).toBe(false)
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\alice\\repo\\src'
)
).toBeNull()
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\Alice\\repo\\line\nbreak'
)
).toBe('line\nbreak')
})
it('resolves POSIX relative paths without using the process cwd', () => {
expect(resolveRuntimePath('/repos/app/repo', '../worktrees/feature')).toBe(
'/repos/app/worktrees/feature'
+13 -8
View File
@@ -12,6 +12,12 @@ export function normalizeRuntimePathSeparators(value: string): string {
export function normalizeRuntimePathForComparison(value: string): string {
const normalized = trimRuntimePathTrailingSlash(normalizeRuntimePathSeparators(value))
const wslUnc = normalized.match(/^\/\/(?:wsl\.localhost|wsl\$)\/([^/]+)(\/[\s\S]*)?$/i)
if (wslUnc) {
// Why: Windows exposes the same case-sensitive WSL filesystem through two
// UNC aliases, while the distro/server portion remains case-insensitive.
return `//wsl/${wslUnc[1].toLowerCase()}${wslUnc[2] ?? ''}`
}
return isWindowsAbsolutePathLike(value) ? normalized.toLowerCase() : normalized
}
@@ -57,16 +63,11 @@ export function isPathInsideOrEqual(rootPath: string, candidatePath: string): bo
}
export function relativePathInsideRoot(rootPath: string, candidatePath: string): string | null {
const normalizedRoot = trimRuntimePathTrailingSlash(normalizeRuntimePathSeparators(rootPath))
const normalizedCandidate = trimRuntimePathTrailingSlash(
normalizeRuntimePathSeparators(candidatePath)
)
const comparisonRoot = isWindowsAbsolutePathLike(rootPath)
? normalizedRoot.toLowerCase()
: normalizedRoot
const comparisonCandidate = isWindowsAbsolutePathLike(rootPath)
? normalizedCandidate.toLowerCase()
: normalizedCandidate
const comparisonRoot = normalizeRuntimePathForComparison(rootPath)
const comparisonCandidate = normalizeRuntimePathForComparison(candidatePath)
if (comparisonCandidate === comparisonRoot) {
return ''
@@ -76,7 +77,11 @@ export function relativePathInsideRoot(rootPath: string, candidatePath: string):
if (!comparisonCandidate.startsWith(comparisonPrefix)) {
return null
}
return normalizedCandidate.slice(comparisonPrefix.length)
// WSL comparison keys fold the UNC alias but preserve Linux path casing, so
// their suffix is both aligned across aliases and safe to return directly.
return comparisonRoot.startsWith('//wsl/')
? comparisonCandidate.slice(comparisonPrefix.length)
: normalizedCandidate.slice(comparisonPrefix.length)
}
function trimRuntimePathTrailingSlash(value: string): string {