Skip to content

Commit efcb937

Browse files
authored
fix(shared): Linux file links preserve literal backslashes (pingdotgg#29245)
1 parent c3cfb5e commit efcb937

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ 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\\src\\main.ts", "//server/share/repo", "src/main.ts"],
122+
["//server/share/a\\b.txt", "//server/share", "a\\b.txt"],
123+
["\\\\server\\share\\repo\\..\\notes.md", "\\\\server\\share\\repo", null],
109124
])("relates %s to %s", (path, workspaceRoot, relativePath) => {
110125
expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
111126
});

‎packages/shared/src/path.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ 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("\\", "/"),
98+
const caseInsensitive =
99+
isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot)) ||
100+
(isNetworkPath(workspaceRoot) && isUncPath(path));
101+
const normalizedPath = caseInsensitive
102+
? stripSlashPrefixedWindowsDrive(path.replaceAll("\\", "/"))
103+
: path;
104+
const normalizedRoot = (
105+
caseInsensitive
106+
? stripSlashPrefixedWindowsDrive(workspaceRoot.replaceAll("\\", "/"))
107+
: workspaceRoot
101108
).replace(/\/+$/, "");
102-
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
103109
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
104110
const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
105111
if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";

0 commit comments

Comments
 (0)