From 77996f17e769e57a5e034a17dce9d85e4bd5dfde Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 17 Nov 2026 20:12:33 +0800 Subject: [PATCH] fix(terminal): open file:// links in the editor (#3996) Compiler output such as Kotlin's "file:///develop/app/src/A.kt:15:56 Unresolved reference" was linked as a URL and handed to the system opener: in a remote window it failed with "Not allowed to open url", and locally it opened the file in another app. A file:// URL is now a file location with its line and column, opened in the editor on the machine the terminal runs on, as the Run console already does. Refs #3447 --- src/features/terminal/links.ts | 26 +++++++++++++++++++++++++- src/features/terminal/terminal.test.ts | 13 +++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/features/terminal/links.ts b/src/features/terminal/links.ts index 0d60c1934..7073ca2c9 100644 --- a/src/features/terminal/links.ts +++ b/src/features/terminal/links.ts @@ -34,11 +34,35 @@ export function trimUrl(url: string): string { } } +/** + * A `file://` URL (compiler output such as Kotlin's `file:///src/A.kt:15:56`) as a file location: + * it opens in the editor, on the machine the terminal runs on, like a plain path. + */ +function fileUrlTarget(url: string): { path: string; line?: number; col?: number } | null { + const m = /^file:\/\/[^/]*(\/.*?)(?::(\d+)(?::(\d+))?)?$/i.exec(url); + if (!m) return null; + let path: string; + try { + path = decodeURIComponent(m[1]!); + } catch { + return null; + } + // `file:///C:/x` is the Windows path `C:/x`. + if (/^\/[A-Za-z]:\//.test(path)) path = path.slice(1); + return { path, line: m[2] ? Number(m[2]) : undefined, col: m[3] ? Number(m[3]) : undefined }; +} + export function findTerminalLinks(text: string): TerminalLink[] { const links: TerminalLink[] = []; for (const m of text.matchAll(URL_RE)) { const url = trimUrl(m[0]); - links.push({ kind: "url", start: m.index, end: m.index + url.length, text: url, url }); + const end = m.index + url.length; + const file = fileUrlTarget(url); + links.push( + file + ? { kind: "file", start: m.index, end, text: url, ...file } + : { kind: "url", start: m.index, end, text: url, url }, + ); } const overlaps = (s: number, e: number) => links.some((l) => s < l.end && e > l.start); for (const m of text.matchAll(FILE_RE)) { diff --git a/src/features/terminal/terminal.test.ts b/src/features/terminal/terminal.test.ts index c7451a4e9..c572e5863 100644 --- a/src/features/terminal/terminal.test.ts +++ b/src/features/terminal/terminal.test.ts @@ -99,6 +99,19 @@ describe("terminal links", () => { expect(findTerminalLinks("12:30 http://localhost:3000").map((l) => l.kind)).toEqual(["url"]); }); + it("opens file:// URLs as file locations", () => { + const line = "[ERROR] file:///develop/mc/src/main/kotlin/top/A%20B.kt:15:56 Unresolved reference 'x'."; + expect(findTerminalLinks(line)).toEqual([ + expect.objectContaining({ kind: "file", path: "/develop/mc/src/main/kotlin/top/A B.kt", line: 15, col: 56 }), + ]); + expect(findTerminalLinks("see file:///C:/work/a.kt:3")).toEqual([ + expect.objectContaining({ kind: "file", path: "C:/work/a.kt", line: 3, col: undefined }), + ]); + expect(findTerminalLinks("file://host/srv/x.txt")).toEqual([ + expect.objectContaining({ kind: "file", path: "/srv/x.txt", line: undefined }), + ]); + }); + it("trims trailing punctuation from URLs", () => { expect(trimUrl("https://a.dev/x.")).toBe("https://a.dev/x"); expect(trimUrl("https://a.dev/(x)")).toBe("https://a.dev/(x)");