Skip to content

Commit 786dc08

Browse files
riley-wvclaude
andcommitted
fix(server): keep lone content-search tokens literal under exclusions
fff treats a single-token grep query as the pattern but parses constraint syntax (`src/`, `*.ts`, `!x`) once other tokens follow. Appending exclusion constraints turned such queries into filters, so searching for `src/` or `!important` returned nothing. Single plain tokens now go through multiGrep, whose separate `constraints` field keeps exclusions native. Single regex tokens run unconstrained, as upstream, and drop excluded paths from results. Multi-token queries, which fff already parses, are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 66d2ed4 commit 786dc08

3 files changed

Lines changed: 93 additions & 30 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,38 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
510510
}),
511511
);
512512

513+
it.effect("keeps a lone constraint-like query as text when exclusions apply", () =>
514+
Effect.gen(function* () {
515+
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-token-", git: true });
516+
yield* writeTextFile(cwd, ".git/info/exclude", ".worktrees/\n");
517+
const line = "see src/ and *.ts and !important\n";
518+
yield* writeTextFile(cwd, "lib/notes.txt", line);
519+
yield* writeTextFile(cwd, ".worktrees/copy/lib/notes.txt", line);
520+
521+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
522+
for (const { query, useRegex } of [
523+
{ query: "src/", useRegex: false },
524+
{ query: "*.ts", useRegex: false },
525+
{ query: "!important", useRegex: false },
526+
{ query: "src/", useRegex: true },
527+
{ query: "!important", useRegex: true },
528+
]) {
529+
const result = yield* workspaceEntries.searchContents({
530+
cwd,
531+
query,
532+
limit: 10,
533+
caseSensitive: true,
534+
wholeWord: false,
535+
useRegex,
536+
});
537+
expect(
538+
result.matches.map((match) => match.path),
539+
`${query} (regex: ${useRegex})`,
540+
).toEqual(["lib/notes.txt"]);
541+
}
542+
}),
543+
);
544+
513545
it.effect("finds later whole-word matches in a file after rejected raw matches", () =>
514546
Effect.gen(function* () {
515547
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-late-whole-word-" });

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
FileFinder,
33
type FileItem,
44
type GrepCursor,
5-
type GrepOptions,
5+
type MultiGrepOptions,
66
type GrepResult,
77
} from "@ff-labs/fff-node";
88
import { afterEach, expect, it } from "@effect/vitest";
@@ -116,8 +116,9 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
116116
filteredFileCount: 1,
117117
nextCursor: cursor,
118118
});
119-
const grep = vi.fn((_query: string, options?: GrepOptions) =>
120-
options?.cursor
119+
// A single plain token runs through multiGrep so exclusions stay out of the pattern.
120+
const multiGrep = vi.fn((options: MultiGrepOptions) =>
121+
options.cursor
121122
? { ok: true as const, value: grepResult("needle", [[0, 6]], null) }
122123
: {
123124
ok: true as const,
@@ -127,7 +128,7 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
127128
const finder = {
128129
destroy: vi.fn(),
129130
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
130-
grep,
131+
multiGrep,
131132
} as unknown as FileFinder;
132133
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
133134

@@ -153,8 +154,12 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
153154
],
154155
truncated: false,
155156
});
156-
expect(grep).toHaveBeenCalledTimes(2);
157-
expect(grep.mock.calls[1]?.[1]?.cursor).toBe(nextCursor);
157+
expect(multiGrep).toHaveBeenCalledTimes(2);
158+
expect(multiGrep.mock.calls[0]?.[0]).toMatchObject({
159+
patterns: ["needle"],
160+
constraints: "!**/.git",
161+
});
162+
expect(multiGrep.mock.calls[1]?.[0]?.cursor).toBe(nextCursor);
158163
}),
159164
),
160165
);

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

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
FileItem,
77
FileFinder as FileFinderType,
88
GrepCursor,
9+
GrepResult,
910
MixedItem,
1011
MixedSearchResult,
1112
Result,
@@ -298,13 +299,13 @@ const QUERY_SAFE_DIRECTORY = /^[^\s*?[\]{}!\\]+\/$/;
298299
/**
299300
* fff applies `.gitignore` files but not `.git/info/exclude` or `core.excludesFile`, where tools
300301
* commonly hide nested worktrees. Directories git excludes through those sources become
301-
* root-anchored `!dir/**` constraints appended to every query.
302+
* root-anchored `!dir/**` query constraints.
302303
*/
303-
const loadExclusionConstraints = Effect.fn("WorkspaceSearchIndex.loadExclusionConstraints")(
304+
const loadExcludedDirectories = Effect.fn("WorkspaceSearchIndex.loadExcludedDirectories")(
304305
function* (vcsProcess: VcsProcess.VcsProcess["Service"], cwd: string) {
305306
const runGit = (args: ReadonlyArray<string>, stdin?: string) =>
306307
vcsProcess.run({
307-
operation: "WorkspaceSearchIndex.loadExclusionConstraints",
308+
operation: "WorkspaceSearchIndex.loadExcludedDirectories",
308309
command: "git",
309310
args: ["-c", "core.fsmonitor=false", ...args],
310311
cwd,
@@ -323,27 +324,27 @@ const loadExclusionConstraints = Effect.fn("WorkspaceSearchIndex.loadExclusionCo
323324
"--no-empty-directory",
324325
"-z",
325326
]);
326-
if (listed.exitCode !== 0) return "";
327+
if (listed.exitCode !== 0) return [];
327328
const directories = listed.stdout
328329
.split("\0")
329330
.filter((entry) => QUERY_SAFE_DIRECTORY.test(entry));
330-
if (directories.length === 0) return "";
331+
if (directories.length === 0) return [];
331332
const classified = yield* runGit(
332333
["check-ignore", "--verbose", "-z", "--stdin"],
333334
`${directories.join("\0")}\0`,
334335
);
335-
if (classified.exitCode !== 0) return "";
336+
if (classified.exitCode !== 0) return [];
336337
// Records are `source NUL line NUL pattern NUL path NUL`.
337338
const fields = classified.stdout.split("\0");
338-
const constraints: string[] = [];
339+
const excluded: string[] = [];
339340
for (let index = 0; index + 3 < fields.length; index += 4) {
340341
if (!GITIGNORE_SOURCE.test(fields[index]!)) {
341-
constraints.push(`!${fields[index + 3]}**`);
342+
excluded.push(fields[index + 3]!);
342343
}
343344
}
344-
return constraints.join(" ");
345+
return excluded;
345346
},
346-
Effect.orElseSucceed(() => ""),
347+
Effect.orElseSucceed((): ReadonlyArray<string> => []),
347348
);
348349

349350
const createFinder = Effect.fn("WorkspaceSearchIndex.createFinder")(function* (
@@ -433,14 +434,20 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
433434
const hasIncompleteInitialScan = () => initialScanTimedOut && finder.isScanning();
434435

435436
const vcsProcess = yield* VcsProcess.VcsProcess;
436-
let exclusionConstraints = yield* loadExclusionConstraints(vcsProcess, cwd);
437+
let excludedDirectories = yield* loadExcludedDirectories(vcsProcess, cwd);
438+
const exclusionConstraints = () =>
439+
[GITLINK_CONSTRAINT, ...excludedDirectories.map((directory) => `!${directory}**`)].join(" ");
437440
const withExclusions = (query: string) =>
438-
[query, GITLINK_CONSTRAINT, exclusionConstraints].filter(Boolean).join(" ");
441+
[query, exclusionConstraints()].filter(Boolean).join(" ");
442+
const isExcludedPath = (path: string) =>
443+
path === ".git" ||
444+
path.endsWith("/.git") ||
445+
excludedDirectories.some((directory) => path.startsWith(directory));
439446

440447
const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* <A>(
441448
query: string,
442449
pageSize: number,
443-
operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch",
450+
operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch" | "multiGrep",
444451
execute: () => Result<A>,
445452
): Effect.fn.Return<A, WorkspaceSearchIndexSearchFailed> {
446453
const result = yield* Effect.try({
@@ -493,7 +500,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
493500
cause,
494501
}),
495502
);
496-
exclusionConstraints = yield* loadExclusionConstraints(vcsProcess, cwd);
503+
excludedDirectories = yield* loadExcludedDirectories(vcsProcess, cwd);
497504
});
498505

499506
const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(
@@ -546,6 +553,10 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
546553
"WorkspaceSearchIndex.searchContents",
547554
)(function* (input) {
548555
const { searchQuery, regexMode } = buildContentSearchQuery(input);
556+
// fff reads a lone token as the pattern but parses constraint syntax (`src/`, `*.ts`, `!x`)
557+
// once other tokens follow, so exclusions must not share a single-token query.
558+
const pattern = searchQuery.trim();
559+
const singleToken = !/\s/.test(pattern);
549560
const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
550561
// Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.
551562
const rawPageSize = input.wholeWord
@@ -557,18 +568,33 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
557568

558569
do {
559570
const remainingTimeBudgetMs = Math.max(1, Math.ceil(deadline - performance.now()));
560-
const result = yield* runSearch(input.query, input.limit, "grep", () =>
561-
finder.grep(withExclusions(searchQuery), {
562-
mode: regexMode ? "regex" : "plain",
563-
smartCase: !input.caseSensitive && !regexMode,
564-
maxMatchesPerFile: Math.min(CONTENT_SEARCH_MAX_MATCHES_PER_FILE, rawPageSize),
565-
pageSize: rawPageSize,
566-
cursor: nextCursor,
567-
timeBudgetMs: remainingTimeBudgetMs,
568-
}),
569-
);
571+
const options = {
572+
smartCase: !input.caseSensitive && !regexMode,
573+
maxMatchesPerFile: Math.min(CONTENT_SEARCH_MAX_MATCHES_PER_FILE, rawPageSize),
574+
pageSize: rawPageSize,
575+
cursor: nextCursor,
576+
timeBudgetMs: remainingTimeBudgetMs,
577+
};
578+
// Plain tokens keep exclusions native via multiGrep's separate constraints; regex has no such
579+
// API, so a lone regex token runs unconstrained and excluded paths are dropped below.
580+
const result: GrepResult =
581+
singleToken && !regexMode
582+
? yield* runSearch(input.query, input.limit, "multiGrep", () =>
583+
finder.multiGrep({
584+
...options,
585+
patterns: [pattern],
586+
constraints: exclusionConstraints(),
587+
}),
588+
)
589+
: yield* runSearch(input.query, input.limit, "grep", () =>
590+
finder.grep(singleToken ? searchQuery : withExclusions(searchQuery), {
591+
...options,
592+
mode: regexMode ? "regex" : "plain",
593+
}),
594+
);
570595

571596
for (const match of result.items) {
597+
if (isExcludedPath(toPosixPath(match.relativePath))) continue;
572598
const matchRanges = mapContentMatchRanges(match.lineContent, match.matchRanges).filter(
573599
(range) => !input.wholeWord || isWholeWordRange(match.lineContent, range),
574600
);

0 commit comments

Comments
 (0)