Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/PathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import path from "node:path";

export function isAbsolutePathLike(value: string): boolean {
const trimmed = value.trim();
return path.isAbsolute(trimmed) || isWindowsAbsolutePath(trimmed);
return path.isAbsolute(trimmed)
|| isWindowsAbsolutePath(trimmed)
|| isWslWindowsMountPath(trimmed);
}

export function arePathsEqual(left: string, right: string): boolean {
Expand All @@ -29,6 +31,11 @@ export function normalizePathForComparison(value: string): string {
return trimTrailingPathSeparators(normalized).toLowerCase();
}

if (isWslWindowsMountPath(trimmed)) {
const normalized = path.posix.normalize(trimmed);
return trimTrailingPathSeparators(normalized).toLowerCase();
}

const pathForComparison = path.isAbsolute(trimmed)
? trimmed
: trimmed.replace(/\\/g, "/");
Expand All @@ -41,8 +48,15 @@ function isWindowsAbsolutePath(value: string): boolean {
return /^[A-Za-z]:\//.test(portableValue) || /^\/\/[^/]+\/[^/]+/.test(portableValue);
}

function isWslWindowsMountPath(value: string): boolean {
return /^\/mnt\/[A-Za-z](?:\/|$)/.test(value);
}

function shouldComparePathCaseInsensitive(value: string): boolean {
return isWindowsAbsolutePath(value) || /^[A-Za-z]:/.test(value) || value.includes("\\");
return isWindowsAbsolutePath(value)
|| isWslWindowsMountPath(value)
|| /^[A-Za-z]:/.test(value)
|| value.includes("\\");
}

function trimTrailingPathSeparators(value: string): string {
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/PathUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@ describe("PathUtils", () => {
expect(arePathsEqual("/repo/project", "/repo/Project")).toBe(false);
});

it("compares WSL Windows mount paths case-insensitively", () => {
expect(arePathsEqual(
"/mnt/c/Users/Me/Notes/MyVault/",
"/mnt/c/users/me/notes/myvault",
)).toBe(true);
expect(normalizePathForComparison("/mnt/c/Users/Me/Notes/MyVault/"))
.toBe("/mnt/c/users/me/notes/myvault");
expect(arePathBasenamesEqual(
"/mnt/c/Users/Me/Notes/MyVault",
"myvault",
)).toBe(true);
});

it("detects Windows absolute paths on any host platform", () => {
expect(isAbsolutePathLike("D:/workspace/sample-project")).toBe(true);
expect(isAbsolutePathLike("D:\\workspace\\sample-project")).toBe(true);
expect(isAbsolutePathLike("\\\\Server\\Share\\Project")).toBe(true);
expect(isAbsolutePathLike("/mnt/c/Users/Me/Notes/MyVault")).toBe(true);
expect(isAbsolutePathLike("sample-project")).toBe(false);
});

Expand Down