Skip to content

Commit 3a51bab

Browse files
committed
🤖 fix: handle quoted Windows paths with spaces in translation
Fixed regex to properly handle paths like "C:\Users\John Doe\repo": - Double-quoted paths: match content up to closing quote (allows spaces) - Single-quoted paths: match content up to closing quote (allows spaces) - Unquoted paths: match up to whitespace (no spaces allowed) Added 3 new tests for paths with spaces.
1 parent 1eaf990 commit 3a51bab

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/node/utils/main/bashPath.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ describe("bashPath", () => {
173173
translateWindowsPathsInCommand('git -C "C:\\Users\\micha\\source\\mux" worktree list')
174174
).toBe('git -C "/mnt/c/Users/micha/source/mux" worktree list');
175175
});
176+
177+
it("should handle double-quoted paths with spaces", () => {
178+
expect(translateWindowsPathsInCommand('cd "C:\\Users\\John Doe\\My Documents"')).toBe(
179+
'cd "/mnt/c/Users/John Doe/My Documents"'
180+
);
181+
});
182+
183+
it("should handle single-quoted paths with spaces", () => {
184+
expect(translateWindowsPathsInCommand("cd 'D:\\Program Files\\My App'")).toBe(
185+
"cd '/mnt/d/Program Files/My App'"
186+
);
187+
});
188+
189+
it("should handle mixed quoted paths with and without spaces", () => {
190+
expect(
191+
translateWindowsPathsInCommand('cp "C:\\Users\\John Doe\\file.txt" C:\\dest')
192+
).toBe('cp "/mnt/c/Users/John Doe/file.txt" /mnt/c/dest');
193+
});
176194
});
177195

178196
describe("getSpawnConfig with WSL path translation", () => {

src/node/utils/main/bashPath.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,34 @@ export function windowsToWslPath(windowsPath: string): string {
8484
* Finds patterns like C:\... or "C:\..." and converts them
8585
*/
8686
export function translateWindowsPathsInCommand(command: string): string {
87-
// Match Windows paths (with or without quotes)
88-
// Handles: C:\path, "C:\path", 'C:\path'
87+
// Match Windows paths with different quote styles:
88+
// 1. Double-quoted: "C:\Users\John Doe\repo" - can contain spaces
89+
// 2. Single-quoted: 'C:\Users\John Doe\repo' - can contain spaces
90+
// 3. Unquoted: C:\Users\name\repo - no spaces allowed
8991
return command.replace(
90-
/(["']?)([a-zA-Z]):[/\\]([^"'\s]*)\1/g,
91-
(_match: string, quote: string, drive: string, rest: string) => {
92-
const wslPath = `/mnt/${drive.toLowerCase()}/${rest.replace(/\\/g, "/")}`;
93-
return quote ? `${quote}${wslPath}${quote}` : wslPath;
92+
/"([a-zA-Z]):[/\\]([^"]*)"|'([a-zA-Z]):[/\\]([^']*)'|([a-zA-Z]):[/\\]([^\s]*)/g,
93+
(
94+
_match: string,
95+
dqDrive: string | undefined,
96+
dqRest: string | undefined,
97+
sqDrive: string | undefined,
98+
sqRest: string | undefined,
99+
uqDrive: string | undefined,
100+
uqRest: string | undefined
101+
) => {
102+
if (dqDrive !== undefined) {
103+
// Double-quoted path
104+
const wslPath = `/mnt/${dqDrive.toLowerCase()}/${dqRest!.replace(/\\/g, "/")}`;
105+
return `"${wslPath}"`;
106+
} else if (sqDrive !== undefined) {
107+
// Single-quoted path
108+
const wslPath = `/mnt/${sqDrive.toLowerCase()}/${sqRest!.replace(/\\/g, "/")}`;
109+
return `'${wslPath}'`;
110+
} else {
111+
// Unquoted path
112+
const wslPath = `/mnt/${uqDrive!.toLowerCase()}/${uqRest!.replace(/\\/g, "/")}`;
113+
return wslPath;
114+
}
94115
}
95116
);
96117
}

0 commit comments

Comments
 (0)