Skip to content

Commit f6037e2

Browse files
committed
fix(web): clicking a logged request route no longer opens it in the editor
Terminal path detection treated request-log routes such as /api/trpc/post.list?batch=1&input=... as file paths, so a plain click to focus the terminal opened them in the preferred editor. Skip path matches whose token carries a query string. Refs #15757
1 parent ad5178a commit f6037e2

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎apps/web/src/terminal-links.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,45 @@ describe("extractTerminalLinks", () => {
115115
{ kind: "path", text, start, end: start + text.length },
116116
]);
117117
});
118+
119+
it.each([
120+
" GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
121+
"GET /api/users?id=42 200",
122+
"proxying api/trpc/post.list?batch=1 to the backend",
123+
])("skips request routes carrying a query string in %s", (line) => {
124+
expect(extractTerminalLinks(line)).toEqual([]);
125+
});
126+
127+
it("keeps a URL with a query string as a URL link", () => {
128+
const line = "open https://example.com/api/trpc/post.list?batch=1";
129+
expect(extractTerminalLinks(line)).toEqual([
130+
{ kind: "url", text: "https://example.com/api/trpc/post.list?batch=1", start: 5, end: 51 },
131+
]);
132+
});
133+
134+
it("still treats a bare route without a query string as a path", () => {
135+
// Without a query string a route is indistinguishable from a file such as `post.list`.
136+
expect(extractTerminalLinks("GET /api/trpc/post.list 200")).toEqual([
137+
{ kind: "path", text: "/api/trpc/post.list", start: 4, end: 23 },
138+
]);
139+
});
140+
141+
it.each([
142+
["src/main.ts:12", "src/main.ts:12"],
143+
["/Users/me/project/file.ts", "/Users/me/project/file.ts"],
144+
["~/project/file", "~/project/file"],
145+
["./a/b", "./a/b"],
146+
["C:\\repo\\file.ts", "C:\\repo\\file.ts"],
147+
["tail -f /tmp/foo.log", "/tmp/foo.log"],
148+
["see /var/log/x", "/var/log/x"],
149+
["/app/bin/server:3:7", "/app/bin/server:3:7"],
150+
["did you mean src/main.ts?", "src/main.ts"],
151+
])("keeps detecting the real path in %s", (line, text) => {
152+
const start = line.indexOf(text);
153+
expect(extractTerminalLinks(line)).toEqual([
154+
{ kind: "path", text, start, end: start + text.length },
155+
]);
156+
});
118157
});
119158

120159
describe("collectWrappedTerminalLinkLine", () => {

‎apps/web/src/terminal-links.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const FILE_PATH_PATTERN =
3737
const TRAILING_PUNCTUATION_PATTERN = /[.,;!?]+$/;
3838
// Paths also drop a trailing colon: compilers end `file:line:col:` with one.
3939
const TRAILING_PATH_PUNCTUATION_PATTERN = /[.,;:!?]+$/;
40+
// The whitespace-delimited token a path match sits in, and a query string inside it.
41+
// Request logs print routes like `/api/trpc/post.list?batch=1`; files don't carry
42+
// queries, while prose like `src/main.ts?` still trims to a path.
43+
const TOKEN_PATTERN = /[^\s"'`<>]*/y;
44+
const QUERY_STRING_PATTERN = /\?[A-Za-z0-9_%-]/;
4045

4146
function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
4247
let output = value.replace(
@@ -60,6 +65,12 @@ function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
6065
return output;
6166
}
6267

68+
function hasQueryString(line: string, start: number): boolean {
69+
TOKEN_PATTERN.lastIndex = start;
70+
const token = TOKEN_PATTERN.exec(line)?.[0] ?? "";
71+
return QUERY_STRING_PATTERN.test(token);
72+
}
73+
6374
function overlaps(a: { start: number; end: number }, b: { start: number; end: number }): boolean {
6475
return a.start < b.end && b.start < a.end;
6576
}
@@ -80,7 +91,9 @@ function collectMatches(
8091

8192
const trimmed = trimClosingDelimiters(raw, kind);
8293
if (trimmed.length === 0) continue;
83-
if (kind === "path" && isTerminalUrl(trimmed)) continue;
94+
if (kind === "path" && (isTerminalUrl(trimmed) || hasQueryString(line, start))) {
95+
continue;
96+
}
8497

8598
const candidate: TerminalLinkMatch = {
8699
kind,

0 commit comments

Comments
 (0)