Skip to content

Commit 9e12eca

Browse files
fix(shared): relative Windows file links open the file (pingdotgg#33643)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5636b56 commit 9e12eca

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe("parseMarkdownFileLink", () => {
5252
["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"],
5353
["+page.svelte", "+page.svelte"],
5454
["[id].tsx", "[id].tsx"],
55+
["/\\server\\share\\x.ts", "/\\server\\share\\x.ts"],
5556
])("recognizes %s as a file", (href, path) => {
5657
expect(parseMarkdownFileLink(href)?.path).toBe(path);
5758
});
@@ -75,6 +76,7 @@ describe("parseMarkdownFileLink", () => {
7576
7677
"foo(bar)",
7778
"$HOME",
79+
"\\foo.ts",
7880
])("does not treat %s as a file", (href) => {
7981
expect(parseMarkdownFileLink(href)).toBeNull();
8082
});
@@ -142,6 +144,20 @@ describe("resolveMarkdownFileLinkTarget", () => {
142144
expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
143145
});
144146

147+
// regression: #33625 backslash-separated relative links open the workspace file
148+
it.each([
149+
["src/Example.cs", "C:\\repo\\src\\Example.cs"],
150+
["src\\Example.cs", "C:\\repo\\src\\Example.cs"],
151+
["src%5CExample.cs", "C:\\repo\\src\\Example.cs"],
152+
["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"],
153+
])("resolves the relative link %s against a windows cwd", (href, target) => {
154+
expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target);
155+
});
156+
157+
it("keeps a backslash in a relative link that already names a file", () => {
158+
expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/./foo\\bar.ts");
159+
});
160+
145161
it("does not treat filename line references as external schemes", () => {
146162
expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
147163
"/Users/julius/project/script.ts:10",

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ export function parseMarkdownFileLink(href: string): FilePathPosition | null {
314314
const hash = safeDecodeURIComponent(source.hash.trim());
315315
if (path.length === 0 || hasExternalScheme(path)) return null;
316316

317+
const position = parseFilePathLink(path, hash);
318+
// `src\a.ts` is Windows-relative. A link that already parses keeps its spelling, since POSIX
319+
// names may contain `\`; a rooted `\\host` or `\foo` would change root if rewritten.
320+
if (position || /^[\\/]/.test(path) || isWindowsAbsolutePath(path)) return position;
321+
return path.includes("\\") ? parseFilePathLink(path.replaceAll("\\", "/"), hash) : null;
322+
}
323+
324+
function parseFilePathLink(path: string, hash: string): FilePathPosition | null {
317325
const position = splitFilePathPosition(path, hash);
318326
return looksLikeFilePath(position.path, path) ? position : null;
319327
}

0 commit comments

Comments
 (0)