Skip to content

Commit b23d0f5

Browse files
committed
fix(shared): preserve literal backslashes in POSIX file links
1 parent 47f2c8d commit b23d0f5

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ describe("workspaceRelativeFilePath", () => {
106106
["/repo/project/../other/notes.md", "/repo/project", null],
107107
["C:\\repo\\project\\src\\..\\..\\notes.md", "C:\\repo\\project", null],
108108
["/repo/project/docs/a/../b.md", "/repo/project", "docs/a/../b.md"],
109+
["/repo/a\\b.txt", "/repo", "a\\b.txt"],
110+
["/repo/a/b.txt", "/repo", "a/b.txt"],
111+
["/repo/a\\", "/repo", "a\\"],
112+
["/repo/..\\notes.md", "/repo", "..\\notes.md"],
113+
["/repo/a\\/../../notes.md", "/repo", null],
114+
["/repo\\project/a.txt", "/repo\\project/", "a.txt"],
115+
["/repo/project/a.txt", "/repo\\project", null],
116+
["/repo\\project/a.txt", "/repo/project", null],
117+
["C:\\Repo\\src\\main.ts", "c:/repo/", "src/main.ts"],
118+
["/C:/Repo/src/main.ts", "c:\\repo", "src/main.ts"],
119+
["C:\\Repo\\src\\main.ts", "/c:/repo", "src/main.ts"],
120+
["\\\\SERVER\\Share\\Repo\\src\\main.ts", "\\\\server\\share\\repo", "src/main.ts"],
121+
["\\\\server\\share\\repo\\..\\notes.md", "\\\\server\\share\\repo", null],
109122
])("relates %s to %s", (path, workspaceRoot, relativePath) => {
110123
expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
111124
});

‎packages/shared/src/path.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,15 @@ export function workspaceRelativeFilePath(
9595
workspaceRoot: string | null | undefined,
9696
): string | null {
9797
if (!workspaceRoot) return null;
98-
const normalizedPath = stripSlashPrefixedWindowsDrive(path.replaceAll("\\", "/"));
99-
const normalizedRoot = stripSlashPrefixedWindowsDrive(
100-
workspaceRoot.replaceAll("\\", "/"),
101-
).replace(/\/+$/, "");
10298
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
99+
const normalizedPath = caseInsensitive
100+
? stripSlashPrefixedWindowsDrive(path.replaceAll("\\", "/"))
101+
: path;
102+
const normalizedRoot = (
103+
caseInsensitive
104+
? stripSlashPrefixedWindowsDrive(workspaceRoot.replaceAll("\\", "/"))
105+
: workspaceRoot
106+
).replace(/\/+$/, "");
103107
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
104108
const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
105109
if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";

0 commit comments

Comments
 (0)