fix: track bare drive switches (d:) for cwd memory

PowerShell/cmd users hop drives with a bare 'd:' — no cd keyword — so the
typed-command tracker never saw it, the pane's cwd stayed at its creation
directory, and the session snapshot restored it to home. Treat a bare
drive-letter line as a cd to the drive root; the main-process existence
check drops it on platforms without drive letters.
This commit is contained in:
Bill committed 2026-09-14 19:27:20 +08:00
1 parent 3c193a48a7
commit 9a330499de
1 file changed
+18 -5
+18 -5
View File
@@ -4,8 +4,9 @@
* Two signals feed the same store: * Two signals feed the same store:
* 1. the shell itself, over OSC 7 (`file://host/path`) or ConEmu's OSC 9;9 — * 1. the shell itself, over OSC 7 (`file://host/path`) or ConEmu's OSC 9;9 —
* exact, but only present when shell integration is on; * exact, but only present when shell integration is on;
* 2. the command the user typed, matched against the `cd` family — the * 2. the command the user typed, matched against the `cd` family plus the
* fallback that works with no shell cooperation at all. * bare drive switch (`d:`) of cmd.exe / PowerShell — the fallback that
* works with no shell cooperation at all.
* *
* Both are platform-neutral: path resolution and existence checks happen in the * Both are platform-neutral: path resolution and existence checks happen in the
* main process (`src/main/cwd.ts`) where node's `path` lives, so nothing here * main process (`src/main/cwd.ts`) where node's `path` lives, so nothing here
@@ -21,14 +22,26 @@
*/ */
const CD_COMMAND = /^\s*(?:cd|chdir|sl|set-location|pushd|push-location)(?:\s+([\s\S]*))?$/i const CD_COMMAND = /^\s*(?:cd|chdir|sl|set-location|pushd|push-location)(?:\s+([\s\S]*))?$/i
/**
* Bare drive switch (`d:`, `D:`) — cmd.exe and PowerShell change the current
* drive with no `cd` keyword at all, and it is the common way to land on
* another drive on Windows. Treated as a cd to the drive root; the main
* process's existence check quietly drops it on platforms where drive
* letters mean nothing (POSIX resolves it to a nonexistent relative path).
*/
const DRIVE_SWITCH = /^\s*([a-zA-Z]):\s*$/
/** /**
* Return the argument of a cd-style command line, or null when the line is not * Return the argument of a cd-style command line, or null when the line is not
* one. An empty string means "bare cd" (the shell's home directory). * one. An empty string means "bare cd" (the shell's home directory). A bare
* drive switch yields the drive root (`D:\`).
*/ */
export function cdArgument(line: string): string | null { export function cdArgument(line: string): string | null {
const match = line.match(CD_COMMAND) const match = line.match(CD_COMMAND)
if (!match) return null if (match) return (match[1] ?? '').trim()
return (match[1] ?? '').trim() const drive = line.match(DRIVE_SWITCH)
if (drive) return `${drive[1].toUpperCase()}:\\`
return null
} }
/** Payload of an OSC 9 sequence that follows the ConEmu `9;<path>` convention. */ /** Payload of an OSC 9 sequence that follows the ConEmu `9;<path>` convention. */