From bf9af63090a0eb35fa9cb1101892c3e4d198683e Mon Sep 17 00:00:00 2001 From: "zhang.bin" Date: Thu, 14 May 2026 16:57:31 +0800 Subject: [PATCH] fix(pty): preserve WSL worktree cwd in terminals (#1808) --- src/main/daemon/pty-subprocess.test.ts | 28 +++++++++++++++++ src/main/providers/windows-shell-args.test.ts | 31 +++++++++++++++++++ src/main/providers/windows-shell-args.ts | 12 ++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/main/daemon/pty-subprocess.test.ts b/src/main/daemon/pty-subprocess.test.ts index c2a99aa12..2505c60bb 100644 --- a/src/main/daemon/pty-subprocess.test.ts +++ b/src/main/daemon/pty-subprocess.test.ts @@ -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 diff --git a/src/main/providers/windows-shell-args.test.ts b/src/main/providers/windows-shell-args.test.ts index 9836a7636..bc0cf9feb 100644 --- a/src/main/providers/windows-shell-args.test.ts +++ b/src/main/providers/windows-shell-args.test.ts @@ -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', diff --git a/src/main/providers/windows-shell-args.ts b/src/main/providers/windows-shell-args.ts index e13e2895f..89cec69e1 100644 --- a/src/main/providers/windows-shell-args.ts +++ b/src/main/providers/windows-shell-args.ts @@ -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`],