Skip to content
TypeScript

fix(windows): strip quotes from repaired PATH (#8746)

Utkarsh Patil12fe2d6d0306
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
index 28955debf7b..5a76402b1d3 100644
--- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
+++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
@@ -320,7 +320,7 @@ describe("DesktopShellEnvironment", () => {
FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
})
- : envOutput({ PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" });
+ : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
},
});
@@ -337,6 +337,7 @@ describe("DesktopShellEnvironment", () => {
"C:\\Users\\testuser\\.bun\\bin",
"C:\\Users\\testuser\\scoop\\shims",
"C:\\Custom\\Bin",
+ "C:",
].join(";"),
);
assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts
index e065bf55d04..b4610eee5c8 100644
--- a/apps/desktop/src/shell/DesktopShellEnvironment.ts
+++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts
@@ -151,6 +151,9 @@ const pathComparisonKey = (entry: string, platform: NodeJS.Platform) => {
return platform === "win32" ? normalized.toLowerCase() : normalized;
};
+const sanitizePathEntry = (entry: string, platform: NodeJS.Platform) =>
+ platform === "win32" ? entry.replaceAll('"', "") : entry;
+
const mergePaths = (
platform: NodeJS.Platform,
values: ReadonlyArray<Option.Option<string>>,
@@ -163,14 +166,14 @@ const mergePaths = (
if (Option.isNone(value)) continue;
for (const entry of value.value.split(delimiter)) {
- const trimmed = entry.trim();
- if (trimmed.length === 0) continue;
+ const sanitized = sanitizePathEntry(entry.trim(), platform);
+ if (sanitized.length === 0) continue;
- const key = pathComparisonKey(trimmed, platform);
+ const key = pathComparisonKey(sanitized, platform);
if (key.length === 0 || seen.has(key)) continue;
seen.add(key);
- entries.push(trimmed);
+ entries.push(sanitized);
}
}
diff --git a/packages/shared/src/shell.test.ts b/packages/shared/src/shell.test.ts
index 89ec055f599..e032af50ca5 100644
--- a/packages/shared/src/shell.test.ts
+++ b/packages/shared/src/shell.test.ts
@@ -304,7 +304,7 @@ describe("readEnvironmentFromWindowsShell", () => {
});
describe("mergePathValues", () => {
- it("dedupes case-insensitively on Windows while preserving preferred order", () => {
+ it("sanitizes and dedupes Windows entries while preserving preferred order", () => {
expect(
mergePathValues(
'C:\\Users\\testuser\\AppData\\Roaming\\npm;"C:\\Program Files\\nodejs"',
@@ -312,10 +312,20 @@ describe("mergePathValues", () => {
"win32",
),
).toBe(
- 'C:\\Users\\testuser\\AppData\\Roaming\\npm;"C:\\Program Files\\nodejs";C:\\Windows\\System32',
+ "C:\\Users\\testuser\\AppData\\Roaming\\npm;C:\\Program Files\\nodejs;C:\\Windows\\System32",
);
});
+ it("removes stray quotes from Windows entries", () => {
+ expect(
+ mergePathValues(
+ 'C:\\Windows\\System32;C:\\cloudflared.exe;C:";C:\\Program Files\\nodejs',
+ undefined,
+ "win32",
+ ),
+ ).toBe("C:\\Windows\\System32;C:\\cloudflared.exe;C:;C:\\Program Files\\nodejs");
+ });
+
it("dedupes case-sensitively on POSIX", () => {
expect(mergePathValues("/usr/local/bin:/usr/bin", "/usr/bin:/USR/BIN", "linux")).toBe(
"/usr/local/bin:/usr/bin:/USR/BIN",
diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.ts
index da6db216676..1fdd35cfd0a 100644
--- a/packages/shared/src/shell.ts
+++ b/packages/shared/src/shell.ts
@@ -414,6 +414,10 @@ function normalizePathEntryForComparison(entry: string, platform: NodeJS.Platfor
return platform === "win32" ? normalized.toLowerCase() : normalized;
}
+function sanitizePathEntry(entry: string, platform: NodeJS.Platform): string {
+ return platform === "win32" ? entry.replaceAll('"', "") : entry;
+}
+
export function mergePathValues(
preferredPath: string | undefined,
inheritedPath: string | undefined,
@@ -427,14 +431,14 @@ export function mergePathValues(
if (!rawValue) continue;
for (const entry of rawValue.split(delimiter)) {
- const trimmed = entry.trim();
- if (trimmed.length === 0) continue;
+ const sanitized = sanitizePathEntry(entry.trim(), platform);
+ if (sanitized.length === 0) continue;
- const normalized = normalizePathEntryForComparison(trimmed, platform);
+ const normalized = normalizePathEntryForComparison(sanitized, platform);
if (normalized.length === 0 || seen.has(normalized)) continue;
seen.add(normalized);
- merged.push(trimmed);
+ merged.push(sanitized);
}
}