Skip to content

Commit bc3c1fe

Browse files
riley-wvclaude
andcommitted
fix(server): honor .git/info/exclude in workspace search
fff-node applies .gitignore files but not .git/info/exclude or core.excludesFile. Repos that hide nested worktrees there had their index flooded (100k+ files), pushing initialized submodule files past the 25k entry cap and below worktree copies in search ranking. Load the directories git excludes through those sources when an index is created or refreshed and append them as root-anchored `!dir/**` constraints to every file, directory, mixed, and grep query. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b396b5b commit bc3c1fe

3 files changed

Lines changed: 146 additions & 11 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,44 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
388388
}),
389389
);
390390

391+
it.effect("excludes directories hidden by .git/info/exclude from every query", () =>
392+
Effect.gen(function* () {
393+
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-info-exclude-", git: true });
394+
yield* writeTextFile(cwd, ".git/info/exclude", ".worktrees/\n/build/\n");
395+
yield* writeTextFile(cwd, ".worktrees/feature/src/needle.ts", "export const needle = 1;");
396+
yield* writeTextFile(cwd, "build/needle.js", "export const needle = 2;");
397+
yield* writeTextFile(cwd, "src/build/needle.ts", "export const needle = 3;");
398+
yield* writeTextFile(cwd, "src/needle.ts", "export const needle = 4;");
399+
400+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
401+
const searched = yield* searchWorkspaceEntries({ cwd, query: "needle", limit: 100 });
402+
const listed = yield* workspaceEntries.list({ cwd });
403+
const contents = yield* workspaceEntries.searchContents({
404+
cwd,
405+
query: "needle",
406+
limit: 100,
407+
caseSensitive: false,
408+
wholeWord: false,
409+
useRegex: false,
410+
});
411+
412+
const expected = ["src/build/needle.ts", "src/needle.ts"];
413+
expect(
414+
searched.entries.map((entry) => entry.path).filter((path) => path.endsWith(".ts")),
415+
).toEqual(expect.arrayContaining(expected));
416+
expect(
417+
listed.entries.map((entry) => entry.path).filter((path) => /needle\.[jt]s$/.test(path)),
418+
).toEqual(expected);
419+
expect(contents.matches.map((match) => match.path).toSorted()).toEqual(expected);
420+
for (const paths of [searched.entries, listed.entries].map((result) =>
421+
result.map((entry) => entry.path),
422+
)) {
423+
expect(paths.some((path) => path.startsWith(".worktrees"))).toBe(false);
424+
expect(paths.some((path) => path.startsWith("build"))).toBe(false);
425+
}
426+
}),
427+
);
428+
391429
it.effect("excludes tracked paths that match ignore rules", () =>
392430
Effect.gen(function* () {
393431
const cwd = yield* makeTempDir({

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@ import {
66
type GrepResult,
77
} from "@ff-labs/fff-node";
88
import { afterEach, expect, it } from "@effect/vitest";
9+
import { VcsProcessSpawnError } from "@t3tools/contracts";
910
import * as Effect from "effect/Effect";
11+
import * as Layer from "effect/Layer";
1012
import { vi } from "vite-plus/test";
1113

14+
import * as VcsProcess from "../vcs/VcsProcess.ts";
1215
import * as WorkspaceSearchIndex from "./WorkspaceSearchIndex.ts";
1316

17+
const gitUnavailable = Layer.succeed(VcsProcess.VcsProcess, {
18+
run: (input) =>
19+
Effect.fail(
20+
new VcsProcessSpawnError({
21+
operation: input.operation,
22+
command: input.command,
23+
cwd: input.cwd,
24+
cause: new Error("git is unavailable in unit tests"),
25+
}),
26+
),
27+
});
28+
1429
afterEach(() => {
1530
vi.restoreAllMocks();
1631
});
@@ -51,7 +66,9 @@ it.effect("filters image searches before applying the result limit", () =>
5166
} as unknown as FileFinder;
5267
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
5368

54-
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
69+
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project").pipe(
70+
Effect.provide(gitUnavailable),
71+
);
5572
const resultWithoutKind = yield* searchIndex.search("", 200, undefined, true);
5673
const resultWithDirectoryKind = yield* searchIndex.search("", 200, "directory", true);
5774

@@ -114,7 +131,9 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
114131
} as unknown as FileFinder;
115132
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
116133

117-
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content");
134+
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content").pipe(
135+
Effect.provide(gitUnavailable),
136+
);
118137
const result = yield* searchIndex.searchContents({
119138
query: "needle",
120139
limit: 1,

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

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import type {
2727
} from "@t3tools/contracts";
2828
import { isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
2929

30+
import * as VcsProcess from "../vcs/VcsProcess.ts";
31+
3032
// fff-node's native library stays external; single-executables require disk packages instead of importing them.
3133
const requireForFff = NodeModule.createRequire(import.meta.url);
3234
const { FileFinder } = requireForFff("@ff-labs/fff-node") as typeof import("@ff-labs/fff-node");
@@ -289,6 +291,61 @@ function withDirectoryAncestors(entries: ReadonlyArray<ProjectEntry>): ProjectEn
289291
return [...entryByPath.values()];
290292
}
291293

294+
const GITIGNORE_SOURCE = /(^|[\\/])\.gitignore$/;
295+
// A collapsed `dir/` that fits in one fff query token: no whitespace, glob, or negation syntax.
296+
const QUERY_SAFE_DIRECTORY = /^[^\s*?[\]{}!\\]+\/$/;
297+
298+
/**
299+
* fff applies `.gitignore` files but not `.git/info/exclude` or `core.excludesFile`, where tools
300+
* commonly hide nested worktrees. Directories git excludes through those sources become
301+
* root-anchored `!dir/**` constraints appended to every query.
302+
*/
303+
const loadExclusionConstraints = Effect.fn("WorkspaceSearchIndex.loadExclusionConstraints")(
304+
function* (vcsProcess: VcsProcess.VcsProcess["Service"], cwd: string) {
305+
const runGit = (args: ReadonlyArray<string>, stdin?: string) =>
306+
vcsProcess.run({
307+
operation: "WorkspaceSearchIndex.loadExclusionConstraints",
308+
command: "git",
309+
args: ["-c", "core.fsmonitor=false", ...args],
310+
cwd,
311+
...(stdin !== undefined ? { stdin } : {}),
312+
allowNonZeroExit: true,
313+
timeoutMs: 10_000,
314+
maxOutputBytes: 4 * 1024 * 1024,
315+
});
316+
// `--directory` collapses a wholly ignored folder into one `name/` entry.
317+
const listed = yield* runGit([
318+
"ls-files",
319+
"--others",
320+
"--ignored",
321+
"--exclude-standard",
322+
"--directory",
323+
"--no-empty-directory",
324+
"-z",
325+
]);
326+
if (listed.exitCode !== 0) return "";
327+
const directories = listed.stdout
328+
.split("\0")
329+
.filter((entry) => QUERY_SAFE_DIRECTORY.test(entry));
330+
if (directories.length === 0) return "";
331+
const classified = yield* runGit(
332+
["check-ignore", "--verbose", "-z", "--stdin"],
333+
`${directories.join("\0")}\0`,
334+
);
335+
if (classified.exitCode !== 0) return "";
336+
// Records are `source NUL line NUL pattern NUL path NUL`.
337+
const fields = classified.stdout.split("\0");
338+
const constraints: string[] = [];
339+
for (let index = 0; index + 3 < fields.length; index += 4) {
340+
if (!GITIGNORE_SOURCE.test(fields[index]!)) {
341+
constraints.push(`!${fields[index + 3]}**`);
342+
}
343+
}
344+
return constraints.join(" ");
345+
},
346+
Effect.orElseSucceed(() => ""),
347+
);
348+
292349
const createFinder = Effect.fn("WorkspaceSearchIndex.createFinder")(function* (
293350
cwd: string,
294351
variant: WorkspaceSearchIndexVariant,
@@ -375,6 +432,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
375432

376433
const hasIncompleteInitialScan = () => initialScanTimedOut && finder.isScanning();
377434

435+
const vcsProcess = yield* VcsProcess.VcsProcess;
436+
let exclusionConstraints = yield* loadExclusionConstraints(vcsProcess, cwd);
437+
const withExclusions = (query: string) =>
438+
exclusionConstraints && query
439+
? `${query} ${exclusionConstraints}`
440+
: query || exclusionConstraints;
441+
378442
const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* <A>(
379443
query: string,
380444
pageSize: number,
@@ -431,15 +495,27 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
431495
cause,
432496
}),
433497
);
498+
exclusionConstraints = yield* loadExclusionConstraints(vcsProcess, cwd);
434499
});
435500

436501
const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(
437502
function* () {
438503
const incompleteBeforeQuery = hasIncompleteInitialScan();
439-
const result = yield* runSearch("", WORKSPACE_INDEX_PAGE_SIZE, "mixedSearch", () =>
440-
finder.mixedSearch("", { pageSize: WORKSPACE_INDEX_PAGE_SIZE }),
441-
);
442-
const mapped = mapMixedSearchResult(result, WORKSPACE_INDEX_MAX_ENTRIES);
504+
// fff's mixed search returns only directories for a constraint-only query; list files and
505+
// derive their folders instead.
506+
const mapped = exclusionConstraints
507+
? mapFileSearchResult(
508+
yield* runSearch("", WORKSPACE_INDEX_PAGE_SIZE, "fileSearch", () =>
509+
finder.fileSearch(exclusionConstraints, { pageSize: WORKSPACE_INDEX_PAGE_SIZE }),
510+
),
511+
WORKSPACE_INDEX_MAX_ENTRIES,
512+
)
513+
: mapMixedSearchResult(
514+
yield* runSearch("", WORKSPACE_INDEX_PAGE_SIZE, "mixedSearch", () =>
515+
finder.mixedSearch("", { pageSize: WORKSPACE_INDEX_PAGE_SIZE }),
516+
),
517+
WORKSPACE_INDEX_MAX_ENTRIES,
518+
);
443519
const sortedEntries = withDirectoryAncestors(mapped.entries).toSorted((left, right) =>
444520
left.path.localeCompare(right.path),
445521
);
@@ -457,22 +533,24 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
457533
)(function* (query, limit, kind, imageOnly) {
458534
const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, limit + 1);
459535
const incompleteBeforeQuery = hasIncompleteInitialScan();
460-
if (kind === "file" || imageOnly) {
536+
const constrainedQuery = withExclusions(query);
537+
// Mixed search drops files when the query holds only exclusion constraints.
538+
if (kind === "file" || imageOnly || (kind === undefined && !query && exclusionConstraints)) {
461539
const result = yield* runSearch(query, pageSize, "fileSearch", () =>
462-
finder.fileSearch(query, { pageSize }),
540+
finder.fileSearch(constrainedQuery, { pageSize }),
463541
);
464542
const mapped = mapFileSearchResult(result, limit, imageOnly);
465543
return { ...mapped, truncated: incompleteBeforeQuery || mapped.truncated };
466544
}
467545
if (kind === "directory") {
468546
const result = yield* runSearch(query, pageSize, "directorySearch", () =>
469-
finder.directorySearch(query, { pageSize }),
547+
finder.directorySearch(constrainedQuery, { pageSize }),
470548
);
471549
const mapped = mapDirectorySearchResult(result, limit);
472550
return { ...mapped, truncated: incompleteBeforeQuery || mapped.truncated };
473551
}
474552
const result = yield* runSearch(query, pageSize, "mixedSearch", () =>
475-
finder.mixedSearch(query, { pageSize }),
553+
finder.mixedSearch(constrainedQuery, { pageSize }),
476554
);
477555
const mapped = mapMixedSearchResult(result, limit);
478556
return { ...mapped, truncated: incompleteBeforeQuery || mapped.truncated };
@@ -494,7 +572,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
494572
do {
495573
const remainingTimeBudgetMs = Math.max(1, Math.ceil(deadline - performance.now()));
496574
const result = yield* runSearch(input.query, input.limit, "grep", () =>
497-
finder.grep(searchQuery, {
575+
finder.grep(withExclusions(searchQuery), {
498576
mode: regexMode ? "regex" : "plain",
499577
smartCase: !input.caseSensitive && !regexMode,
500578
maxMatchesPerFile: Math.min(CONTENT_SEARCH_MAX_MATCHES_PER_FILE, rawPageSize),

0 commit comments

Comments
 (0)