Skip to content

Commit be1a4a1

Browse files
tiliakoosMarcoDotIO
andcommitted
fix(web): terminal URLs like redis:// no longer become broken file links
The Windows drive rule matched the last letter of a URL scheme, so redis://, postgres://, s3:// and file:// output linked paths that can't open. Co-authored-by: MarcoDotIO <[email protected]>
1 parent 15fe283 commit be1a4a1

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ 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+
])("does not turn part of a non-web URL into an editor link: %s", (line) => {
196+
expect(extractTerminalLinks(line)).toEqual([]);
197+
});
198+
199+
it("keeps file links beside a non-web URL", () => {
200+
const line = "src/db.ts:4 redis://localhost:6379/0 C:/app/src/db.ts:9";
201+
expect(extractTerminalLinks(line)).toEqual([
202+
{ kind: "path", text: "src/db.ts:4", start: 0, end: 11 },
203+
{ kind: "path", text: "C:/app/src/db.ts:9", start: 37, end: 55 },
204+
]);
205+
});
206+
190207
it("keeps a question mark inside a filesystem path", () => {
191208
const path = "/tmp/report?draft";
192209
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+
// and the lookbehind keeps runs of letters 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)