fix(pty): preserve WSL worktree cwd in terminals (#1808)

This commit is contained in:
zhang.bin
2026-05-14 01:57:31 -07:00
committed by GitHub
parent 63bd56c770
commit bf9af63090
3 changed files with 70 additions and 1 deletions
+28
View File
@@ -501,6 +501,34 @@ describe('createPtySubprocess', () => {
)
})
it('keeps WSL UNC worktree terminals in the matching distro cwd on Windows', () => {
const proc = mockPtyProcess()
spawnMock.mockReturnValue(proc)
const platform = Object.getOwnPropertyDescriptor(process, 'platform')
Object.defineProperty(process, 'platform', { value: 'win32' })
try {
createPtySubprocess({
sessionId: 'test',
cols: 80,
rows: 24,
cwd: '\\\\wsl.localhost\\Ubuntu\\home\\alice\\repo',
shellOverride: 'wsl.exe'
})
} finally {
if (platform) {
Object.defineProperty(process, 'platform', platform)
}
}
expect(spawnMock).toHaveBeenCalledWith(
'wsl.exe',
['-d', 'Ubuntu', '--', 'bash', '-c', "cd '/home/alice/repo' && exec bash -l"],
expect.objectContaining({ cwd: expect.any(String) })
)
})
// Why: node-pty's UnixTerminal.destroy() registers _socket.once('close', () =>
// this.kill('SIGHUP')), and the socket 'close' event can fire concurrently
// with onExit. If kill is not neutralized by the time close fires, SIGHUP
@@ -73,6 +73,37 @@ describe('resolveWindowsShellLaunchArgs', () => {
expect(result.shellArgs[3]).toBe("cd '/mnt/c' && exec bash -l")
})
it('keeps WSL UNC worktree cwd inside the matching distro', () => {
const originalPlatform = process.platform
Object.defineProperty(process, 'platform', {
configurable: true,
value: 'win32'
})
try {
const result = resolveWindowsShellLaunchArgs(
'wsl.exe',
'\\\\wsl.localhost\\Ubuntu\\home\\alice\\repo',
'C:\\Users\\alice'
)
expect(result.shellArgs).toEqual([
'-d',
'Ubuntu',
'--',
'bash',
'-c',
"cd '/home/alice/repo' && exec bash -l"
])
expect(result.effectiveCwd).toBe('C:\\Users\\alice')
expect(result.validationCwd).toBe('\\\\wsl.localhost\\Ubuntu\\home\\alice\\repo')
} finally {
Object.defineProperty(process, 'platform', {
configurable: true,
value: originalPlatform
})
}
})
it('falls back to empty args + same cwd for unknown shells', () => {
const result = resolveWindowsShellLaunchArgs(
'C:\\tools\\fish.exe',
+11 -1
View File
@@ -1,4 +1,5 @@
import { win32 as pathWin32 } from 'path'
import { parseWslPath, toLinuxPath } from '../wsl'
/** Result of resolving a Windows shell to its launch args + effective cwd.
*
@@ -62,8 +63,17 @@ export function resolveWindowsShellLaunchArgs(
}
if (shellBasename === 'wsl.exe') {
const wslInfo = parseWslPath(cwd)
if (wslInfo) {
const escapedLinuxCwd = wslInfo.linuxPath.replace(/'/g, "'\\''")
return {
shellArgs: ['-d', wslInfo.distro, '--', 'bash', '-c', `cd '${escapedLinuxCwd}' && exec bash -l`],
effectiveCwd: defaultCwd,
validationCwd: cwd
}
}
const driveMatch = cwd.replace(/\\/g, '/').match(/^([A-Za-z]):\/?(.*)$/)
const linuxCwd = driveMatch ? `/mnt/${driveMatch[1].toLowerCase()}/${driveMatch[2]}` : '/mnt/c'
const linuxCwd = driveMatch ? toLinuxPath(cwd) : '/mnt/c'
const escapedLinuxCwd = linuxCwd.replace(/'/g, "'\\''")
return {
shellArgs: ['--', 'bash', '-c', `cd '${escapedLinuxCwd}' && exec bash -l`],