TypeScript
fix(server): an invalid regex in project search still finds its text when Match case is off (#30569)
MarcoDotIOd1667e833c81
diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.tsindex b96b7b5df55..b0633dac04c 100644--- a/apps/server/src/workspace/WorkspaceEntries.test.ts+++ b/apps/server/src/workspace/WorkspaceEntries.test.ts@@ -672,6 +672,32 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {}),);+ it.effect("searches an invalid regex literally without matching case", () =>+ Effect.gen(function* () {+ const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-invalid-regex-" });+ yield* writeTextFile(cwd, "src/calls.ts", "call(Foo(bar));\n");++ const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;+ const result = yield* workspaceEntries.searchContents({+ cwd,+ query: "foo(",+ limit: 100,+ caseSensitive: false,+ wholeWord: false,+ useRegex: true,+ });++ expect(result.regexFallbackError).toBeDefined();+ expect(result.matches).toEqual([+ expect.objectContaining({+ path: "src/calls.ts",+ lineNumber: 1,+ matchRanges: [{ start: 5, end: 9 }],+ }),+ ]);+ }),+ );+it.effect("maps multi-byte lines to string-indexed ranges", () =>Effect.gen(function* () {const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-multibyte-" });diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.tsindex 8b8782b2005..ecb8c6ba87a 100644--- a/apps/server/src/workspace/WorkspaceSearchIndex.ts+++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts@@ -478,7 +478,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn("WorkspaceSearchIndex.searchContents",)(function* (input) {- const { searchQuery, regexMode } = buildContentSearchQuery(input);+ let { searchQuery, regexMode } = buildContentSearchQuery(input);const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;// Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.const rawPageSize = input.wholeWord@@ -487,6 +487,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (const matches: Array<ProjectSearchContentsResult["matches"][number]> = [];let nextCursor: GrepCursor | null = null;let regexFallbackError: string | undefined;+ let retryLiterally = false;do {const remainingTimeBudgetMs = Math.max(1, Math.ceil(deadline - performance.now()));@@ -500,6 +501,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (timeBudgetMs: remainingTimeBudgetMs,}),);+ // fff searches an invalid pattern as a literal, which would keep the `(?i)` prefix.+ retryLiterally = regexMode && !input.caseSensitive && result.regexFallbackError !== undefined;+ if (retryLiterally) {+ regexFallbackError = result.regexFallbackError;+ ({ searchQuery, regexMode } = buildContentSearchQuery({ ...input, useRegex: false }));+ continue;+ }for (const match of result.items) {const matchRanges = mapContentMatchRanges(match.lineContent, match.matchRanges).filter(@@ -516,7 +524,10 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (}nextCursor = result.nextCursor;regexFallbackError ??= result.regexFallbackError;- } while (matches.length < input.limit && nextCursor !== null && performance.now() < deadline);+ } while (+ retryLiterally ||+ (matches.length < input.limit && nextCursor !== null && performance.now() < deadline)+ );return {matches: matches.slice(0, input.limit),