Skip to content
TypeScript

fix(web): keep PowerShell prompt paths with spaces in one link (#23527)

Utkarsh Patild1f1650f09bc
diff --git a/apps/web/src/terminal-links.test.ts b/apps/web/src/terminal-links.test.ts
index 05ed26959be..f6df5912ecd 100644
--- a/apps/web/src/terminal-links.test.ts
+++ b/apps/web/src/terminal-links.test.ts
@@ -16,6 +16,36 @@ function createBufferLine(text: string, isWrapped = false): TerminalBufferLineLi
}
describe("extractTerminalLinks", () => {
+ it.each([
+ String.raw`C:\dev\ai-test\dir 1`,
+ String.raw`C:\dev\ai-test\dir 1\test`,
+ "C:\\",
+ String.raw`\\server\my share\dir 1`,
+ "/home/Jane Doe/project",
+ String.raw`C:\dev\folder (draft)\name!`,
+ ])("keeps the full PowerShell prompt path %s in one link", (path) => {
+ expect(extractTerminalLinks(`PS ${path}> `)).toEqual([
+ { kind: "path", text: path, start: 3, end: 3 + path.length },
+ ]);
+ });
+
+ it("keeps links in a command separate from the PowerShell prompt path", () => {
+ const path = String.raw`C:\dev\dir 1\test`;
+ const prompt = `PS ${path}> `;
+ const url = "https://example.com";
+ expect(extractTerminalLinks(`${prompt}echo ${url}`)).toEqual([
+ { kind: "path", text: path, start: 3, end: 3 + path.length },
+ { kind: "url", text: url, start: prompt.length + 5, end: prompt.length + 5 + url.length },
+ ]);
+ });
+
+ it("does not extend ordinary output paths through trailing prose", () => {
+ const path = String.raw`C:\dev\project`;
+ expect(extractTerminalLinks(`see ${path} for details`)).toEqual([
+ { kind: "path", text: path, start: 4, end: 4 + path.length },
+ ]);
+ });
+
it.each([
String.raw`src\index.ts:12`,
String.raw`.\src\index.ts:12:7`,
diff --git a/apps/web/src/terminal-links.ts b/apps/web/src/terminal-links.ts
index 48a7c16edbc..1c078d86cd2 100644
--- a/apps/web/src/terminal-links.ts
+++ b/apps/web/src/terminal-links.ts
@@ -27,6 +27,8 @@ export interface WrappedTerminalLinkLine {
}
const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/giu;
+// The default PowerShell prompt delimits its unquoted path with `PS ` and `>`.
+const POWERSHELL_PROMPT_PATH_PATTERN = /^PS ((?:[A-Za-z]:[\\/]|\\\\|\/)[^>\r\n]*)>/u;
// Bare paths start only at a segment boundary: hover detection runs this on
// every frame, so separator-free output must not be rescanned at every suffix.
const FILE_PATH_PATTERN =
@@ -104,17 +106,25 @@ function collectMatches(
}
export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
+ const promptPath = POWERSHELL_PROMPT_PATH_PATTERN.exec(line)?.[1];
+ const promptMatches: TerminalLinkMatch[] = promptPath
+ ? [{ kind: "path", text: promptPath, start: 3, end: 3 + promptPath.length }]
+ : [];
const requestRoutes = Array.from(line.matchAll(REQUEST_ROUTE_PATTERN), (match) => ({
start: match.index,
end: match.index + match[0].length,
}));
- const urlMatches = collectMatches(line, "url", URL_PATTERN, []);
- const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, urlMatches);
+ const urlMatches = collectMatches(line, "url", URL_PATTERN, promptMatches);
+ const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, [
+ ...promptMatches,
+ ...urlMatches,
+ ]);
const pathMatches = collectMatches(line, "path", FILE_PATH_PATTERN, [
+ ...promptMatches,
...urlMatches,
...quotedMatches,
]);
- return [...urlMatches, ...quotedMatches, ...pathMatches]
+ return [...promptMatches, ...urlMatches, ...quotedMatches, ...pathMatches]
.filter(
(match) => match.kind === "url" || !requestRoutes.some((route) => overlaps(match, route)),
)
diff --git a/apps/web/src/terminal/ghostty/surface.test.ts b/apps/web/src/terminal/ghostty/surface.test.ts
index 5473e3c4ffa..57018464df4 100644
--- a/apps/web/src/terminal/ghostty/surface.test.ts
+++ b/apps/web/src/terminal/ghostty/surface.test.ts
@@ -606,12 +606,20 @@ describe("terminalLinkAtPositionWithRange", () => {
row("com/reference", true),
row("~/project/file", false),
row("C:\\repo\\file.ts", false),
+ row("PS C:\\repo\\dir 1", false, true),
+ row("\\test> ", true),
];
expect(terminalLinkAtPositionWithRange(rows, 0, 8)?.text).toBe("https://example.com/reference");
expect(terminalLinkAtPositionWithRange(rows, 1, 4)?.text).toBe("https://example.com/reference");
expect(terminalLinkAtPositionWithRange(rows, 2, 2)?.text).toBe("~/project/file");
expect(terminalLinkAtPositionWithRange(rows, 3, 4)?.text).toBe("C:\\repo\\file.ts");
+ expect(terminalLinkAtPositionWithRange(rows, 4, 13)).toEqual({
+ text: "C:\\repo\\dir 1\\test",
+ range: { start: { x: 3, y: 4 }, end: { x: 4, y: 5 } },
+ });
+ expect(terminalLinkAtPositionWithRange(rows, 5, 2)?.text).toBe("C:\\repo\\dir 1\\test");
+ expect(terminalLinkAtPositionWithRange(rows, 5, 5)).toBeNull();
expect(terminalLinkAtPositionWithRange(rows, 1, 4)).toEqual({
text: "https://example.com/reference",
range: {