fix(windows): strip quotes from repaired PATH (#8746)
This commit is contained in:
1 parent
6e324b9bbf
commit
12fe2d6d03
4 files changed
+29
-11
No files matched your search
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user