Fix Windows import session path matching (#1012)

This commit is contained in:
accx
2026-05-17 20:32:28 +08:00
committed by GitHub
parent 0e3a78b308
commit 686a25fb65
6 changed files with 157 additions and 7 deletions
+22
View File
@@ -0,0 +1,22 @@
import { describe, expect, test } from "vitest";
import { areEquivalentPaths, createPathEquivalenceMatcher } from "./path.js";
describe("path equivalence", () => {
test.each([
["C:/Users/Administrator/GhostFactory", "C:\\Users\\Administrator\\GhostFactory"],
["d:\\Projects\\paseo", "D:\\Projects\\paseo"],
["C:\\Users\\Administrator\\GhostFactory\\", "C:\\Users\\Administrator\\GhostFactory"],
[String.raw`\\?\C:\Users\Administrator\GhostFactory`, "C:\\Users\\Administrator\\GhostFactory"],
[String.raw`\\?\UNC\server\share\GhostFactory`, String.raw`\\server\share\GhostFactory`],
])("matches Windows-equivalent cwd forms", (left, right) => {
expect(areEquivalentPaths(left, right)).toBe(true);
expect(createPathEquivalenceMatcher(left)(right)).toBe(true);
});
test("keeps POSIX path casing significant", () => {
expect(
areEquivalentPaths("/Users/Administrator/GhostFactory", "/users/administrator/ghostfactory"),
).toBe(false);
});
});
+84 -1
View File
@@ -1,4 +1,5 @@
import os from "os";
import os from "node:os";
import nodePath from "node:path";
/**
* Expand tilde in path to home directory
@@ -13,3 +14,85 @@ export function expandTilde(path: string): string {
}
return path;
}
/**
* Compare two path strings as filesystem-equivalent for cwd filtering.
*
* This is a string-only comparison: it normalizes separators and dot segments,
* ignores trailing separators, strips Windows namespace prefixes, and
* case-folds only when comparing as Windows. It does not resolve symlinks or
* check whether either path exists.
*/
export function areEquivalentPaths(left: string, right: string): boolean {
const compareAsWindows = shouldCompareAsWindows(left, right);
return (
normalizePathForComparison(left, compareAsWindows) ===
normalizePathForComparison(right, compareAsWindows)
);
}
export function createPathEquivalenceMatcher(target: string): (candidate: string) => boolean {
const targetLooksWindows = looksLikeDefiniteWindowsPath(target);
const compareAsWindows = targetLooksWindows;
const normalizedTarget = normalizePathForComparison(target, compareAsWindows);
return (candidate) => {
const candidateCompareAsWindows = compareAsWindows || looksLikeDefiniteWindowsPath(candidate);
const comparableTarget =
candidateCompareAsWindows === compareAsWindows
? normalizedTarget
: normalizePathForComparison(target, candidateCompareAsWindows);
return normalizePathForComparison(candidate, candidateCompareAsWindows) === comparableTarget;
};
}
function shouldCompareAsWindows(left: string, right: string): boolean {
return looksLikeDefiniteWindowsPath(left) || looksLikeDefiniteWindowsPath(right);
}
function looksLikeDefiniteWindowsPath(value: string): boolean {
return (
/^[a-zA-Z]:[\\/]/u.test(value) ||
/^[/\\]{2}\?[/\\]/u.test(value) ||
/^\\{2}[^/\\]+[/\\][^/\\]+/u.test(value)
);
}
function normalizePathForComparison(value: string, compareAsWindows: boolean): string {
const platformPath = compareAsWindows ? nodePath.win32 : nodePath.posix;
const comparableValue = compareAsWindows ? stripWindowsNamespacePrefix(value) : value;
const platformNormalized = platformPath.normalize(comparableValue);
const normalized = stripTrailingSeparators(
platformNormalized,
platformPath.parse(platformNormalized).root,
compareAsWindows,
);
return compareAsWindows ? normalized.toLowerCase() : normalized;
}
function stripWindowsNamespacePrefix(value: string): string {
const driveMatch = value.match(/^[/\\]{2}\?[/\\]([a-zA-Z]:)[/\\](.*)$/u);
const drivePrefix = driveMatch?.[1];
if (drivePrefix) {
return `${drivePrefix}\\${driveMatch[2] ?? ""}`;
}
const uncMatch = value.match(/^[/\\]{2}\?[/\\]UNC[/\\]([^/\\]+)[/\\]([^/\\]+)(?:[/\\](.*))?$/iu);
const uncServer = uncMatch?.[1];
const uncShare = uncMatch?.[2];
if (uncServer && uncShare) {
const uncRest = uncMatch[3];
return `\\\\${uncServer}\\${uncShare}${uncRest !== undefined ? `\\${uncRest}` : ""}`;
}
return value;
}
function stripTrailingSeparators(value: string, root: string, compareAsWindows: boolean): string {
const separatorPattern = compareAsWindows ? /[\\/]/u : /\//u;
let result = value;
while (result.length > root.length && separatorPattern.test(result.at(-1) ?? "")) {
result = result.slice(0, -1);
}
return result;
}