Skip to content

Commit f513369

Browse files
fix(web): terminal URLs like redis:// no longer become broken file links (#28189)
Co-authored-by: MarcoDotIO <[email protected]>
1 parent c77f755 commit f513369

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ describe("extractTerminalLinks", () => {
187187
]);
188188
});
189189

190+
it.each([
191+
"connected to redis://127.0.0.1:6379/0",
192+
"connected to postgres://localhost:5432/app",
193+
"uploaded s3://bucket/reports/today.csv",
194+
" at main (file:///Users/jane/app/src/index.mjs:3:7)",
195+
"+redis://localhost:6379/0",
196+
"-redis://localhost:6379/0",
197+
])("does not turn part of a non-web URL into an editor link: %s", (line) => {
198+
expect(extractTerminalLinks(line)).toEqual([]);
199+
});
200+
201+
it("keeps file links beside a non-web URL", () => {
202+
const line = "src/db.ts:4 redis://localhost:6379/0 C:/app/src/db.ts:9";
203+
expect(extractTerminalLinks(line)).toEqual([
204+
{ kind: "path", text: "src/db.ts:4", start: 0, end: 11 },
205+
{ kind: "path", text: "C:/app/src/db.ts:9", start: 37, end: 55 },
206+
]);
207+
});
208+
190209
it("keeps a question mark inside a filesystem path", () => {
191210
const path = "/tmp/report?draft";
192211
expect(extractTerminalLinks(`wrote ${path}`)).toEqual([

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const TRAILING_PATH_PUNCTUATION_PATTERN = /[.,;:!?]+$/;
4141
// A question mark is legal in filenames; only exclude query-bearing request targets.
4242
const REQUEST_ROUTE_PATTERN =
4343
/(?:^|[\s"'`])(?:GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|CONNECT|TRACE|proxying)\s+["'`]?(?:\/|[A-Za-z0-9_-]+\/)[^\s?]*\?\S*/g;
44+
// The tail of `redis://host/0` or `file:///a.mjs` is not a path. One-letter schemes stay drives,
45+
// a diff's `+` or `-` may lead, and the lookbehind keeps runs from being rescanned at every suffix.
46+
const SCHEME_URL_PATTERN = /(?<![A-Za-z\d+.-])[+-]?[A-Za-z][A-Za-z\d+.-]+:\/\/[^\s"'`<>]+/gu;
4447

4548
function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
4649
let output = value.replace(
@@ -110,10 +113,10 @@ export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
110113
const promptMatches: TerminalLinkMatch[] = promptPath
111114
? [{ kind: "path", text: promptPath, start: 3, end: 3 + promptPath.length }]
112115
: [];
113-
const requestRoutes = Array.from(line.matchAll(REQUEST_ROUTE_PATTERN), (match) => ({
114-
start: match.index,
115-
end: match.index + match[0].length,
116-
}));
116+
const nonPathRanges = Array.from(
117+
[...line.matchAll(REQUEST_ROUTE_PATTERN), ...line.matchAll(SCHEME_URL_PATTERN)],
118+
(match) => ({ start: match.index, end: match.index + match[0].length }),
119+
);
117120
const urlMatches = collectMatches(line, "url", URL_PATTERN, promptMatches);
118121
const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, [
119122
...promptMatches,
@@ -126,7 +129,7 @@ export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
126129
]);
127130
return [...promptMatches, ...urlMatches, ...quotedMatches, ...pathMatches]
128131
.filter(
129-
(match) => match.kind === "url" || !requestRoutes.some((route) => overlaps(match, route)),
132+
(match) => match.kind === "url" || !nonPathRanges.some((range) => overlaps(match, range)),
130133
)
131134
.toSorted((a, b) => a.start - b.start);
132135
}

0 commit comments

Comments
 (0)