The best way to control Claude Code, Codex and more from any computer or phone
fix(server): whole-word content searches no longer miss matching files (#39285)
Julius Marminge committed
Nov 19, 2026 at 11:59 UTC
9f4d25495d72c1768c000273f2fcca89b91b538f
2 files changed
+75
-4
apps/server/src/workspace/WorkspaceSearchIndex.test.ts
+43
@@ -290,6 +290,8 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
290
truncated: false,
291
});
292
expect(grep).toHaveBeenCalledTimes(2);
293
+ expect(grep.mock.calls[0]?.[0]).toBe(String.raw`(?-u)\bneedle\b`);
294
+ expect(grep.mock.calls[0]?.[1]?.mode).toBe("regex");
295
expect(grep.mock.calls[1]?.[1]?.cursor).toBe(nextCursor);
296
}),
297
).pipe(Effect.provide(testLayer)),
@@ -772,3 +774,44 @@ it.live("finds Git-ignored files that a root .ignore re-includes", () =>
774
expect(result.entries).toEqual([{ kind: "file", path: "notes/daily/standup.md" }]);
775
}).pipe(Effect.scoped, Effect.provide(testLayer)),
776
);
777
+
778
+// Real fff: the native whole-word pattern must return what the plain search and the JS word check did.
779
+it.live.each([
780
+ { query: "needle", caseSensitive: true, expected: ["notes.md:1:0-6", "words.ts:2:0-6,8-14"] },
781
+ { query: "NEEDLE", caseSensitive: false, expected: ["notes.md:1:0-6", "words.ts:2:0-6,8-14"] },
782
+ // fff reads `*.ts` as a file constraint, which a regex built around the whole query would lose.
783
+ { query: "*.ts needle", caseSensitive: true, expected: ["words.ts:2:0-6,8-14"] },
784
+ // Plain search folds ASCII case only: `ſ` is not an `s`, and the Kelvin sign is not a `k`.
785
+ { query: "s", caseSensitive: false, expected: ["fold.txt:2:0-1"] },
786
+ { query: "k", caseSensitive: false, expected: ["fold.txt:4:0-1"] },
787
+])("matches whole words in a real workspace for $query", ({ query, caseSensitive, expected }) =>
788
+ Effect.gen(function* () {
789
+ const fileSystem = yield* FileSystem.FileSystem;
790
+ const path = yield* Path.Path;
791
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-search-whole-word-" });
792
+ yield* fileSystem.writeFileString(
793
+ path.join(cwd, "words.ts"),
794
+ "needleSuffix\nneedle, needle.\n\u00e9needle\n",
795
+ );
796
+ yield* fileSystem.writeFileString(path.join(cwd, "notes.md"), "needle *.ts\n");
797
+ yield* fileSystem.writeFileString(path.join(cwd, "fold.txt"), "\u017f\ns\n\u212a\nk\n");
798
+
799
+ const searchIndex = yield* WorkspaceSearchIndex.make(cwd, "content");
800
+ const result = yield* searchIndex.searchContents({
801
+ query,
802
+ limit: 500,
803
+ caseSensitive,
804
+ wholeWord: true,
805
+ useRegex: false,
806
+ });
807
+
808
+ expect(
809
+ result.matches
810
+ .map(
811
+ (match) =>
812
+ `${match.path}:${match.lineNumber}:${match.matchRanges.map((range) => `${range.start}-${range.end}`).join(",")}`,
813
+ )
814
+ .toSorted(),
815
+ ).toEqual(expected);
816
+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
817
+);
apps/server/src/workspace/WorkspaceSearchIndex.ts
+32
-4
@@ -285,6 +285,28 @@ function buildContentSearchQuery(input: Omit<ProjectSearchContentsInput, "cwd">)
285
: { searchQuery: input.query.toLowerCase(), regexMode: false };
286
}
287
288
+const ASCII_WORD = /^\w+$/;
289
+
290
+// A whole-word search for an ASCII word asks fff for the word itself, so substring hits are dropped
291
+// natively instead of filling pages with lines the JS check then discards. `-u` keeps `\b` and `i`
292
+// to ASCII: plain search does not fold `ſ` or `K`, and any non-ASCII neighbour stays a candidate.
293
+function buildWholeWordPattern(query: string, caseSensitive: boolean): string {
294
+ return `(?${caseSensitive ? "" : "i"}-u)\\b${query}\\b`;
295
+}
296
+
297
+// fff's ranges for that pattern are not used: the JS word check decides, on every hit in the line.
298
+function findWordRanges(
299
+ line: string,
300
+ word: string,
301
+ caseSensitive: boolean,
302
+): Array<{ readonly start: number; readonly end: number }> {
303
+ // No `u` flag: with it, `i` would fold `ſ` and `K` onto ASCII letters.
304
+ return Array.from(line.matchAll(new RegExp(word, caseSensitive ? "g" : "gi")), (match) => ({
305
+ start: match.index,
306
+ end: match.index + match[0].length,
307
+ }));
308
+}
309
+
310
function mapContentMatchRanges(
311
line: string,
312
byteRanges: ReadonlyArray<readonly [number, number]>,
@@ -1318,7 +1340,11 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
1340
);
1341
contentIndexPending = false;
1342
}
1321
- let { searchQuery, regexMode } = buildContentSearchQuery(input);
1343
+ // fff reads glob and path syntax out of a query even in regex mode, so only a bare word qualifies.
1344
+ const wholeWordPrefilter = input.wholeWord && !input.useRegex && ASCII_WORD.test(input.query);
1345
+ let { searchQuery, regexMode } = wholeWordPrefilter
1346
+ ? { searchQuery: buildWholeWordPattern(input.query, input.caseSensitive), regexMode: true }
1347
+ : buildContentSearchQuery(input);
1348
const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
1349
// Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.
1350
const rawPageSize = input.wholeWord
@@ -1351,9 +1377,11 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
1377
1378
for (const match of result.items) {
1379
if (isIgnoredPath(ignoredPaths, match.relativePath, hostIsWindows)) continue;
1354
- const matchRanges = mapContentMatchRanges(match.lineContent, match.matchRanges).filter(
1355
- (range) => !input.wholeWord || isWholeWordRange(match.lineContent, range),
1356
- );
1380
+ const matchRanges = (
1381
+ wholeWordPrefilter
1382
+ ? findWordRanges(match.lineContent, input.query, input.caseSensitive)
1383
+ : mapContentMatchRanges(match.lineContent, match.matchRanges)
1384
+ ).filter((range) => !input.wholeWord || isWholeWordRange(match.lineContent, range));
1385
// fff reports hits past its 512-char lineContent cap with no ranges, so whole words can't be verified there.
1386
if (input.wholeWord && matchRanges.length === 0) continue;
1387
matches.push({