From 9468077340db1f5a5111edbc262a493f4ff49be6 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Fri, 27 Nov 2026 01:15:18 +0000 Subject: [PATCH] fix(server): content search while indexing checks long lines in full --- .../workspace/WorkspaceSearchIndex.test.ts | 93 +++++++++++++++++++ .../src/workspace/WorkspaceSearchIndex.ts | 50 +++++----- 2 files changed, 120 insertions(+), 23 deletions(-) diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.test.ts b/apps/server/src/workspace/WorkspaceSearchIndex.test.ts index a406f3cb8a4a3..82bb0259844f2 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.test.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.test.ts @@ -362,6 +362,7 @@ it.live("answers content searches with git grep while a slow content index build .mockReturnValueOnce({ ok: true, value: finder }) .mockReturnValueOnce({ ok: true, value: noPcreFinder }) .mockReturnValueOnce({ ok: true, value: noPcreFinder }) + .mockReturnValueOnce({ ok: true, value: noPcreFinder }) .mockReturnValueOnce({ ok: true, value: noPcreFinder }); const searchIndex = yield* WorkspaceSearchIndex.make(cwd, "content"); @@ -401,6 +402,21 @@ it.live("answers content searches with git grep while a slow content index build (match) => match.matchRanges, ), ).toEqual([[{ start: 6, end: 12 }], [{ start: 0, end: 7 }]]); + // Git's match text can end on the `\r` of a CRLF line. + expect( + (yield* search({ query: "needle|1;.*", useRegex: true })).matches.map( + (match) => match.matchRanges, + ), + ).toEqual([ + [ + { start: 6, end: 12 }, + { start: 15, end: 17 }, + ], + [{ start: 0, end: 6 }], + ]); + expect( + (yield* search({ query: "\\r", useRegex: true })).matches.map((match) => match.matchRanges), + ).toEqual([[]]); expect( (yield* search({ query: "[a-z]e", useRegex: true })).matches.map( (match) => match.matchRanges, @@ -481,6 +497,83 @@ it.live("answers content searches with git grep while a slow content index build ], truncated: true, }); + // A column that does not point at its text (a file edited between the two runs, or invalid + // UTF-8 before the match) still lands on that text, never on other text. + const staleRangesIndex = yield* failingGitIndex("-o", "", { + exitCode: 0, + stdout: "src/a.ts\u00001\u00001\u0000Needle\n", + }); + expect( + (yield* staleRangesIndex.searchContents(regexInput)).matches.map( + (match) => match.matchRanges, + ), + ).toEqual([[{ start: 6, end: 12 }]]); + yield* fileSystem.writeFile( + path.join(cwd, "src/latin1.ts"), + new Uint8Array([...Buffer.from("caf"), 0xe9, ...Buffer.from(" needle needle\n")]), + ); + expect( + (yield* search({ query: "needle", wholeWord: true, useRegex: true })).matches.map((match) => [ + match.path, + match.matchRanges, + ]), + ).toEqual([ + ["src/a.ts", [{ start: 6, end: 12 }]], + [ + "src/latin1.ts", + [ + { start: 5, end: 11 }, + { start: 12, end: 18 }, + ], + ], + ]); + yield* fileSystem.remove(path.join(cwd, "src/latin1.ts")); + // Outside a UTF-8 locale, git's `.` matches one byte of `中`. + yield* fileSystem.writeFileString(path.join(cwd, "src/cjk.ts"), "x中 y\n"); + vi.stubEnv("LC_ALL", "C"); + const partialCharacter = yield* search({ query: "x.", wholeWord: true, useRegex: true }).pipe( + Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())), + ); + expect(partialCharacter.matches.map((match) => [match.path, match.matchRanges])).toEqual([ + ["src/cjk.ts", [{ start: 0, end: 2 }]], + ]); + yield* fileSystem.remove(path.join(cwd, "src/cjk.ts")); + + // Lines longer than the 512-character cap are matched whole, then show only ranges that fit. + yield* fileSystem.writeFileString( + path.join(cwd, "src/long.ts"), + [ + `${" ".repeat(600)}token`, + `${" ".repeat(507)}tokenized`, + `é token ${"d".repeat(600)} token`, + `é tokenized${" ".repeat(600)}token`, + ].join("\n"), + ); + const longLines = (input: Partial[0]>) => + search({ query: "token", ...input }).pipe( + Effect.map((result) => + result.matches.map((match) => [ + match.lineNumber, + match.lineContent.length, + match.matchRanges, + ]), + ), + ); + const wholeWordLines = [ + [1, 512, []], + [3, 512, [{ start: 2, end: 7 }]], + [4, 512, []], + ]; + expect(yield* longLines({ wholeWord: true })).toEqual(wholeWordLines); + expect(yield* longLines({ query: "tok+en", wholeWord: true, useRegex: true })).toEqual( + wholeWordLines, + ); + expect(yield* longLines({ query: "tok+en\\w*", useRegex: true })).toEqual([ + [1, 512, []], + [2, 512, []], + [3, 512, [{ start: 2, end: 7 }]], + [4, 512, [{ start: 2, end: 11 }]], + ]); isWarmupComplete = true; yield* search({}); diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.ts index ddef21beae0a8..ab9dad49d3180 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts @@ -328,16 +328,25 @@ function escapeRegExp(input: string): string { function locateRegexMatches( line: string, matches: { readonly column: number; readonly texts: ReadonlyArray } | undefined, -): Array { +): Array<{ readonly start: number; readonly end: number }> { if (!matches) return []; const lineBytes = Buffer.from(line); - const ranges: Array = []; - let start = matches.column - 1; + const toStringIndex = (byteOffset: number) => lineBytes.subarray(0, byteOffset).toString().length; + const ranges: Array<{ readonly start: number; readonly end: number }> = []; + // The column counts the file's bytes; invalid UTF-8 decodes to U+FFFD, never fewer bytes, so the + // column can only land early and the first match is found by its text from there too. + const firstStart = toStringIndex(matches.column - 1); for (const text of matches.texts) { - const textBytes = Buffer.from(text); - if (ranges.length > 0) start = lineBytes.indexOf(textBytes, ranges.at(-1)![1]); - if (start < 0) break; - ranges.push([start, start + textBytes.length]); + const start = line.indexOf(text, ranges.length > 0 ? ranges.at(-1)!.end : firstStart); + if (start >= 0) { + ranges.push({ start, end: start + text.length }); + } else if (ranges.length === 0) { + // Git outside a UTF-8 locale can match part of a character, whose bytes decode to U+FFFD. + const endByte = matches.column - 1 + Buffer.byteLength(text); + ranges.push({ start: firstStart, end: toStringIndex(endByte) }); + } else { + break; + } } return ranges; } @@ -866,31 +875,26 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( rangesCutOff = true; break; } - const lineContent = (rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine).slice( - 0, - CONTENT_LINE_MAX_LENGTH, - ); - const matchRanges = ( + const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine; + const lineContent = line.slice(0, CONTENT_LINE_MAX_LENGTH); + const ranges = ( literal - ? Array.from(lineContent.matchAll(literal), (match) => ({ + ? Array.from((input.wholeWord ? line : lineContent).matchAll(literal), (match) => ({ start: match.index, end: match.index + match[0].length, })) - : mapContentMatchRanges( - lineContent, - locateRegexMatches(lineContent, regexMatches.get(key)), + : // A match can take in the `\r` of a CRLF line, which `line` leaves out. + locateRegexMatches(rawLine, regexMatches.get(key)).flatMap(({ start, end }) => + start < line.length ? [{ start, end: Math.min(end, line.length) }] : [], ) - ).filter( - (range) => - range.end <= lineContent.length && - (!input.wholeWord || isWholeWordRange(lineContent, range)), - ); - if (input.wholeWord && matchRanges.length === 0) continue; + ).filter((range) => !input.wholeWord || isWholeWordRange(line, range)); + if (input.wholeWord && ranges.length === 0) continue; matches.push({ path: relativePath, lineNumber: Number(lineNumber), lineContent, - matchRanges, + // Like fff, a match that does not fit in the shown line keeps the line without its range. + matchRanges: ranges.filter((range) => range.end <= lineContent.length), }); if (matches.length > input.limit) break; }