Skip to content
TypeScript

fix(server): keep the live search index when a refresh times out (#23182)

Victor Vogele03b6967a914
diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts
index 6bef5f04263..d861935fe17 100644
--- a/apps/server/src/workspace/WorkspaceEntries.test.ts
+++ b/apps/server/src/workspace/WorkspaceEntries.test.ts
@@ -1,6 +1,7 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeFSP from "node:fs/promises";
import * as NodeServices from "@effect/platform-node/NodeServices";
+import { FileFinder } from "@ff-labs/fff-node";
import { it, afterEach, describe, expect } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
@@ -673,6 +674,53 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
expect(match.lineContent.slice(range.start, range.end)).toBe("wörld");
}),
);
+
+ it.effect("keeps answering from the live index when a refresh outlasts the scan wait", () =>
+ Effect.gen(function* () {
+ const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-refresh-" });
+ yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n");
+ let scansOutlastWait = false;
+ let finders = 0;
+ let rescans = 0;
+ const createFinder = FileFinder.create.bind(FileFinder);
+ vi.spyOn(FileFinder, "create").mockImplementation((options) => {
+ const created = createFinder(options);
+ if (created.ok) {
+ const finder = created.value;
+ finders += 1;
+ const scanFiles = finder.scanFiles.bind(finder);
+ finder.scanFiles = () => {
+ rescans += 1;
+ return scanFiles();
+ };
+ const waitForIndexReady = finder.waitForIndexReady.bind(finder);
+ finder.waitForIndexReady = (timeoutMs) =>
+ scansOutlastWait
+ ? Promise.resolve({ ok: true, value: false })
+ : waitForIndexReady(timeoutMs);
+ }
+ return created;
+ });
+ const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
+ const searchNeedle = workspaceEntries.searchContents({
+ cwd,
+ query: "needle",
+ limit: 100,
+ caseSensitive: true,
+ wholeWord: false,
+ useRegex: false,
+ });
+ yield* searchNeedle;
+
+ scansOutlastWait = true;
+ yield* workspaceEntries.refresh(cwd);
+ const result = yield* searchNeedle;
+
+ expect(result.matches.map((match) => match.path)).toEqual(["src/notes.ts"]);
+ // The second search ran on the rescanned live finder, not a rebuild.
+ expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 });
+ }),
+ );
});
describe("browse", () => {
diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.ts
index 8f2b64cf26a..d5539e2f4e7 100644
--- a/apps/server/src/workspace/WorkspaceSearchIndex.ts
+++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts
@@ -117,10 +117,7 @@ export class WorkspaceSearchIndex extends Context.Service<
readonly searchContents: (
input: Omit<ProjectSearchContentsInput, "cwd">,
) => Effect.Effect<ProjectSearchContentsResult, WorkspaceSearchIndexSearchFailed>;
- readonly refresh: () => Effect.Effect<
- void,
- WorkspaceSearchIndexRefreshFailed | WorkspaceSearchIndexScanTimedOut
- >;
+ readonly refresh: () => Effect.Effect<void, WorkspaceSearchIndexRefreshFailed>;
}
>()("t3/workspace/WorkspaceSearchIndex") {}
@@ -419,6 +416,8 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
reason: result.error,
});
}
+ // A rescan that outlasts the wait keeps running, and the finder answers
+ // from its previous index until it finishes, so the index stays usable.
yield* waitForIndexReady(
cwd,
finder,
@@ -428,7 +427,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
reason,
cause,
}),
- );
+ ).pipe(Effect.catchTags({ WorkspaceSearchIndexScanTimedOut: () => Effect.void }));
});
const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(