Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions apps/server/src/workspace/WorkspaceSearchIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ it.effect("fetches past a first page of Git-ignored results", () =>
ok: true as const,
value: { isScanning: false },
}),
watch: () => ({ ok: true as const, value: () => undefined }),
watchAsync: async () => ({ ok: true as const, value: () => undefined }),
fileSearchAsync: fileSearch,
} as unknown as FileFinder;
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
Expand All @@ -197,7 +197,7 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
ok: true as const,
value: { isScanning: false },
}),
watch: () => ({ ok: true as const, value: () => undefined }),
watchAsync: async () => ({ ok: true as const, value: () => undefined }),
fileSearchAsync: vi.fn(async () => ({
ok: true as const,
value: { items, scores: [], totalMatched: 1, totalFiles: 1 },
Expand Down Expand Up @@ -264,7 +264,7 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
ok: true as const,
value: { isScanning: false },
}),
watch: () => ({ ok: true as const, value: () => undefined }),
watchAsync: async () => ({ ok: true as const, value: () => undefined }),
grepAsync: grep,
} as unknown as FileFinder;
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
Expand Down Expand Up @@ -333,7 +333,7 @@ it.live("answers content searches with git grep while a slow content index build
ok: true as const,
value: { isScanning: false, isWarmupComplete },
}),
watch: () => ({ ok: true as const, value: () => undefined }),
watchAsync: async () => ({ ok: true as const, value: () => undefined }),
grepAsync: grep,
} as unknown as FileFinder;
const noPcreFinder = {
Expand Down Expand Up @@ -512,10 +512,14 @@ function fakeSymlinkFinder(
ok: true as const,
value: { isScanning: false },
}),
watch: (callback: (events: ReadonlyArray<WatchEvent>) => void) => {
// fff's watch waits on the picker lock on the JS thread.
watch: () => {
throw new Error("watch must not run on the JS thread");
},
watchAsync: async (callback: (events: ReadonlyArray<WatchEvent>) => void) => {
if (watchFailures > 0) {
watchFailures -= 1;
throw new Error("File system watcher is not ready");
return { ok: false as const, error: "File system watcher is not ready" };
}
watchers.push(callback);
return { ok: true as const, value: () => undefined };
Expand Down
44 changes: 21 additions & 23 deletions apps/server/src/workspace/WorkspaceSearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,38 +1030,36 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
if (!editsOnly) invalidateSymlinkScan();
};
// fff refuses subscriptions until its watcher is up, which can trail the initial scan, so
// each read retries until one is in place.
// each read retries until one is in place. Subscribing waits on fff's picker lock, which its
// watcher holds while it indexes a new folder, so it runs off the JS thread.
let unwatch: WatchUnsubscribe | undefined;
yield* Effect.addFinalizer(() => Effect.sync(() => unwatch?.()));
const ensureWatching = () => {
const ensureWatching = Effect.fn("WorkspaceSearchIndex.ensureWatching")(function* () {
if (unwatch) return true;
try {
const result = finder.watch(onWatchBatch);
if (result.ok) unwatch = result.value;
} catch {
// Not ready yet; the next read tries again.
}
const result = yield* Effect.promise(() =>
finder.watchAsync(onWatchBatch).catch(() => undefined),
);
if (result?.ok) unwatch = result.value;
return unwatch !== undefined;
};
});
const indexScope = yield* Effect.scope;
const startSymlinkScan = Effect.fn("WorkspaceSearchIndex.startSymlinkScan")(function* () {
symlinkScanStale = false;
// Subscribing before the scan starts means no change during it goes unseen.
const watched = ensureWatching();
const done = yield* Deferred.make<SymlinkScan>();
pendingSymlinkScan = done;
yield* scanSymlinkEntries().pipe(
Effect.tap((result) =>
Effect.gen(function* () {
lastSymlinkScan = result;
knownLinkPaths = result.linkPaths;
// Without a watcher, or before fff has seen every folder, the result is kept only briefly.
lastSymlinkScanExpiresAt =
watched && result.indexComplete
? undefined
: (yield* Clock.currentTimeMillis) + UNWATCHED_SYMLINK_SCAN_TTL_MS;
}),
),
yield* Effect.gen(function* () {
// Subscribing before the scan starts means no change during it goes unseen.
const watched = yield* ensureWatching();
const result = yield* scanSymlinkEntries();
lastSymlinkScan = result;
knownLinkPaths = result.linkPaths;
// Without a watcher, or before fff has seen every folder, the result is kept only briefly.
lastSymlinkScanExpiresAt =
watched && result.indexComplete
? undefined
: (yield* Clock.currentTimeMillis) + UNWATCHED_SYMLINK_SCAN_TTL_MS;
return result;
}).pipe(
Effect.onExit((exit) =>
Effect.sync(() => {
pendingSymlinkScan = undefined;
Expand Down
Loading
Loading