Skip to content

Commit 54abdcb

Browse files
fix(windows): strip quotes from repaired PATH
1 parent 2daff8c commit 54abdcb

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ describe("DesktopShellEnvironment", () => {
320320
FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
321321
FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
322322
})
323-
: envOutput({ PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" });
323+
: envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
324324
},
325325
});
326326

@@ -337,6 +337,7 @@ describe("DesktopShellEnvironment", () => {
337337
"C:\\Users\\testuser\\.bun\\bin",
338338
"C:\\Users\\testuser\\scoop\\shims",
339339
"C:\\Custom\\Bin",
340+
"C:",
340341
].join(";"),
341342
);
342343
assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ const pathComparisonKey = (entry: string, platform: NodeJS.Platform) => {
151151
return platform === "win32" ? normalized.toLowerCase() : normalized;
152152
};
153153

154+
const sanitizePathEntry = (entry: string, platform: NodeJS.Platform) =>
155+
platform === "win32" ? entry.replaceAll('"', "") : entry;
156+
154157
const mergePaths = (
155158
platform: NodeJS.Platform,
156159
values: ReadonlyArray<Option.Option<string>>,
@@ -163,14 +166,14 @@ const mergePaths = (
163166
if (Option.isNone(value)) continue;
164167

165168
for (const entry of value.value.split(delimiter)) {
166-
const trimmed = entry.trim();
167-
if (trimmed.length === 0) continue;
169+
const sanitized = sanitizePathEntry(entry.trim(), platform);
170+
if (sanitized.length === 0) continue;
168171

169-
const key = pathComparisonKey(trimmed, platform);
172+
const key = pathComparisonKey(sanitized, platform);
170173
if (key.length === 0 || seen.has(key)) continue;
171174

172175
seen.add(key);
173-
entries.push(trimmed);
176+
entries.push(sanitized);
174177
}
175178
}
176179

‎packages/shared/src/shell.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,28 @@ describe("readEnvironmentFromWindowsShell", () => {
304304
});
305305

306306
describe("mergePathValues", () => {
307-
it("dedupes case-insensitively on Windows while preserving preferred order", () => {
307+
it("sanitizes and dedupes Windows entries while preserving preferred order", () => {
308308
expect(
309309
mergePathValues(
310310
'C:\\Users\\testuser\\AppData\\Roaming\\npm;"C:\\Program Files\\nodejs"',
311311
"c:\\users\\testuser\\appdata\\roaming\\npm;C:\\Windows\\System32",
312312
"win32",
313313
),
314314
).toBe(
315-
'C:\\Users\\testuser\\AppData\\Roaming\\npm;"C:\\Program Files\\nodejs";C:\\Windows\\System32',
315+
"C:\\Users\\testuser\\AppData\\Roaming\\npm;C:\\Program Files\\nodejs;C:\\Windows\\System32",
316316
);
317317
});
318318

319+
it("removes stray quotes from Windows entries", () => {
320+
expect(
321+
mergePathValues(
322+
'C:\\Windows\\System32;C:\\cloudflared.exe;C:";C:\\Program Files\\nodejs',
323+
undefined,
324+
"win32",
325+
),
326+
).toBe("C:\\Windows\\System32;C:\\cloudflared.exe;C:;C:\\Program Files\\nodejs");
327+
});
328+
319329
it("dedupes case-sensitively on POSIX", () => {
320330
expect(mergePathValues("/usr/local/bin:/usr/bin", "/usr/bin:/USR/BIN", "linux")).toBe(
321331
"/usr/local/bin:/usr/bin:/USR/BIN",

‎packages/shared/src/shell.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ function normalizePathEntryForComparison(entry: string, platform: NodeJS.Platfor
414414
return platform === "win32" ? normalized.toLowerCase() : normalized;
415415
}
416416

417+
function sanitizePathEntry(entry: string, platform: NodeJS.Platform): string {
418+
return platform === "win32" ? entry.replaceAll('"', "") : entry;
419+
}
420+
417421
export function mergePathValues(
418422
preferredPath: string | undefined,
419423
inheritedPath: string | undefined,
@@ -427,14 +431,14 @@ export function mergePathValues(
427431
if (!rawValue) continue;
428432

429433
for (const entry of rawValue.split(delimiter)) {
430-
const trimmed = entry.trim();
431-
if (trimmed.length === 0) continue;
434+
const sanitized = sanitizePathEntry(entry.trim(), platform);
435+
if (sanitized.length === 0) continue;
432436

433-
const normalized = normalizePathEntryForComparison(trimmed, platform);
437+
const normalized = normalizePathEntryForComparison(sanitized, platform);
434438
if (normalized.length === 0 || seen.has(normalized)) continue;
435439

436440
seen.add(normalized);
437-
merged.push(trimmed);
441+
merged.push(sanitized);
438442
}
439443
}
440444

0 commit comments

Comments
 (0)