Skip to content

Commit d1667e8

Browse files
MarcoDotIOclaude
andauthored
fix(server): an invalid regex in project search still finds its text when Match case is off (pingdotgg#30569)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3109638 commit d1667e8

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎apps/server/src/workspace/WorkspaceEntries.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,32 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
672672
}),
673673
);
674674

675+
it.effect("searches an invalid regex literally without matching case", () =>
676+
Effect.gen(function* () {
677+
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-invalid-regex-" });
678+
yield* writeTextFile(cwd, "src/calls.ts", "call(Foo(bar));\n");
679+
680+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
681+
const result = yield* workspaceEntries.searchContents({
682+
cwd,
683+
query: "foo(",
684+
limit: 100,
685+
caseSensitive: false,
686+
wholeWord: false,
687+
useRegex: true,
688+
});
689+
690+
expect(result.regexFallbackError).toBeDefined();
691+
expect(result.matches).toEqual([
692+
expect.objectContaining({
693+
path: "src/calls.ts",
694+
lineNumber: 1,
695+
matchRanges: [{ start: 5, end: 9 }],
696+
}),
697+
]);
698+
}),
699+
);
700+
675701
it.effect("maps multi-byte lines to string-indexed ranges", () =>
676702
Effect.gen(function* () {
677703
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-multibyte-" });

‎apps/server/src/workspace/WorkspaceSearchIndex.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
478478
const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn(
479479
"WorkspaceSearchIndex.searchContents",
480480
)(function* (input) {
481-
const { searchQuery, regexMode } = buildContentSearchQuery(input);
481+
let { searchQuery, regexMode } = buildContentSearchQuery(input);
482482
const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
483483
// Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.
484484
const rawPageSize = input.wholeWord
@@ -487,6 +487,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
487487
const matches: Array<ProjectSearchContentsResult["matches"][number]> = [];
488488
let nextCursor: GrepCursor | null = null;
489489
let regexFallbackError: string | undefined;
490+
let retryLiterally = false;
490491

491492
do {
492493
const remainingTimeBudgetMs = Math.max(1, Math.ceil(deadline - performance.now()));
@@ -500,6 +501,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
500501
timeBudgetMs: remainingTimeBudgetMs,
501502
}),
502503
);
504+
// fff searches an invalid pattern as a literal, which would keep the `(?i)` prefix.
505+
retryLiterally = regexMode && !input.caseSensitive && result.regexFallbackError !== undefined;
506+
if (retryLiterally) {
507+
regexFallbackError = result.regexFallbackError;
508+
({ searchQuery, regexMode } = buildContentSearchQuery({ ...input, useRegex: false }));
509+
continue;
510+
}
503511

504512
for (const match of result.items) {
505513
const matchRanges = mapContentMatchRanges(match.lineContent, match.matchRanges).filter(
@@ -516,7 +524,10 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
516524
}
517525
nextCursor = result.nextCursor;
518526
regexFallbackError ??= result.regexFallbackError;
519-
} while (matches.length < input.limit && nextCursor !== null && performance.now() < deadline);
527+
} while (
528+
retryLiterally ||
529+
(matches.length < input.limit && nextCursor !== null && performance.now() < deadline)
530+
);
520531

521532
return {
522533
matches: matches.slice(0, input.limit),

0 commit comments

Comments
 (0)