@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 623 lines 25.3 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off
2 import * as NodeFSP from "node:fs/promises";
3
4 import * as Clock from "effect/Clock";
5 import * as Context from "effect/Context";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import * as Path from "effect/Path";
10 import * as RcMap from "effect/RcMap";
11 import * as Schema from "effect/Schema";
12
13 import type {
14 FilesystemBrowseInput,
15 FilesystemBrowseResult,
16 ProjectEntry,
17 ProjectListEntriesInput,
18 ProjectListEntriesResult,
19 ProjectListIgnoredFilesInput,
20 ProjectListIgnoredFilesResult,
21 ProjectSearchContentsInput,
22 ProjectSearchContentsResult,
23 ProjectSearchEntriesInput,
24 ProjectSearchEntriesResult,
25 } from "@t3tools/contracts";
26 import * as HostProcess from "@t3tools/shared/HostProcess";
27 import { isExplicitRelativePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
28 import { normalizeSearchQuery } from "@t3tools/shared/searchRanking";
29
30 import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
31 import * as VcsProcess from "../vcs/VcsProcess.ts";
32 import * as WorkspacePaths from "./WorkspacePaths.ts";
33 import * as WorkspaceSearchIndex from "./WorkspaceSearchIndex.ts";
34
35 export class WorkspaceEntriesWindowsPathUnsupportedError extends Schema.TaggedError<WorkspaceEntriesWindowsPathUnsupportedError>()(
36 "WorkspaceEntriesWindowsPathUnsupportedError",
37 {
38 cwd: Schema.optional(Schema.String),
39 partialPath: Schema.String,
40 platform: Schema.String,
41 },
42 ) {
43 override get message(): string {
44 const cwd = this.cwd ? ` from '${this.cwd}'` : "";
45 return `Windows-style workspace path '${this.partialPath}' is not supported on '${this.platform}'${cwd}.`;
46 }
47 }
48
49 export class WorkspaceEntriesCurrentProjectRequiredError extends Schema.TaggedError<WorkspaceEntriesCurrentProjectRequiredError>()(
50 "WorkspaceEntriesCurrentProjectRequiredError",
51 {
52 partialPath: Schema.String,
53 },
54 ) {
55 override get message(): string {
56 return `A current project is required to browse relative workspace path '${this.partialPath}'.`;
57 }
58 }
59
60 export class WorkspaceEntriesReadDirectoryError extends Schema.TaggedError<WorkspaceEntriesReadDirectoryError>()(
61 "WorkspaceEntriesReadDirectoryError",
62 {
63 cwd: Schema.optional(Schema.String),
64 partialPath: Schema.String,
65 parentPath: Schema.String,
66 cause: Schema.Defect(),
67 },
68 ) {
69 get pathMissing(): boolean {
70 return (this.cause as NodeJS.ErrnoException | undefined)?.code === "ENOENT";
71 }
72
73 override get message(): string {
74 const cwd = this.cwd ? ` from '${this.cwd}'` : "";
75 return `Failed to read workspace directory '${this.parentPath}' while browsing '${this.partialPath}'${cwd}.`;
76 }
77 }
78
79 export const WorkspaceEntriesBrowseError = Schema.Union([
80 WorkspaceEntriesWindowsPathUnsupportedError,
81 WorkspaceEntriesCurrentProjectRequiredError,
82 WorkspaceEntriesReadDirectoryError,
83 ]);
84 export type WorkspaceEntriesBrowseError = typeof WorkspaceEntriesBrowseError.Type;
85
86 export const WorkspaceEntriesError = Schema.Union([
87 WorkspaceEntriesReadDirectoryError,
88 WorkspacePaths.WorkspaceRootNotExistsError,
89 WorkspacePaths.WorkspaceRootCreateFailedError,
90 WorkspacePaths.WorkspaceRootStatFailedError,
91 WorkspacePaths.WorkspaceRootNotDirectoryError,
92 WorkspaceSearchIndex.WorkspaceSearchIndexCreateFailed,
93 WorkspaceSearchIndex.WorkspaceSearchIndexScanTimedOut,
94 WorkspaceSearchIndex.WorkspaceSearchIndexSearchFailed,
95 ]);
96 export type WorkspaceEntriesError = typeof WorkspaceEntriesError.Type;
97
98 export class WorkspaceEntries extends Context.Service<
99 WorkspaceEntries,
100 {
101 readonly browse: (
102 input: FilesystemBrowseInput,
103 ) => Effect.Effect<FilesystemBrowseResult, WorkspaceEntriesBrowseError>;
104 readonly list: (
105 input: ProjectListEntriesInput,
106 ) => Effect.Effect<ProjectListEntriesResult, WorkspaceEntriesError>;
107 readonly search: (
108 input: ProjectSearchEntriesInput,
109 ) => Effect.Effect<ProjectSearchEntriesResult, WorkspaceEntriesError>;
110 readonly listIgnoredFiles: (
111 input: ProjectListIgnoredFilesInput,
112 ) => Effect.Effect<ProjectListIgnoredFilesResult, WorkspaceEntriesError>;
113 readonly searchContents: (
114 input: ProjectSearchContentsInput,
115 ) => Effect.Effect<ProjectSearchContentsResult, WorkspaceEntriesError>;
116 readonly refresh: (cwd: string) => Effect.Effect<void>;
117 }
118 >()("t3/workspace/WorkspaceEntries") {}
119
120 /** Ignored files offered for copying; more than this is a build folder, not config. */
121 const IGNORED_FILES_LIMIT = 200;
122
123 /** Paths sent to one `git check-ignore` call when labelling a folder's children. */
124 const IGNORE_CHECK_CHUNK_SIZE = 1000;
125
126 /** Children classified at once when listing a folder; each symlink costs a realpath and a stat. */
127 const SYMLINK_RESOLVE_BATCH_SIZE = 64;
128
129 const resolveBrowseTarget = Effect.fn("WorkspaceEntries.resolveBrowseTarget")(function* (
130 input: FilesystemBrowseInput,
131 path: Path.Path,
132 ): Effect.fn.Return<string, WorkspaceEntriesBrowseError> {
133 const platform = yield* HostProcess.Platform;
134 if (platform !== "win32" && isWindowsAbsolutePath(input.partialPath)) {
135 return yield* new WorkspaceEntriesWindowsPathUnsupportedError({
136 cwd: input.cwd,
137 partialPath: input.partialPath,
138 platform,
139 });
140 }
141
142 if (!isExplicitRelativePath(input.partialPath)) {
143 return path.resolve(expandHomePath(input.partialPath, yield* HostProcess.HomeDirectory));
144 }
145
146 if (!input.cwd) {
147 return yield* new WorkspaceEntriesCurrentProjectRequiredError({
148 partialPath: input.partialPath,
149 });
150 }
151 return path.resolve(
152 expandHomePath(input.cwd, yield* HostProcess.HomeDirectory),
153 input.partialPath,
154 );
155 });
156
157 /** @public */
158 export const make = Effect.gen(function* () {
159 const path = yield* Path.Path;
160 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
161 const workspaceSearchIndexes = yield* WorkspaceSearchIndex.WorkspaceSearchIndexMap;
162 const vcsProcess = yield* VcsProcess.VcsProcess;
163
164 const normalizeWorkspaceRoot = Effect.fn("WorkspaceEntries.normalizeWorkspaceRoot")(function* (
165 cwd: string,
166 ): Effect.fn.Return<string, WorkspaceEntriesError> {
167 return yield* workspacePaths.normalizeWorkspaceRoot(cwd);
168 });
169
170 // Refreshing goes through `get`, which restarts the map's idle timer, so idleness is measured
171 // from the last search, not the last refresh.
172 const lastSearchedAt = new Map<string, number>();
173 const indexIdleMillis = Duration.toMillis(WorkspaceSearchIndex.WORKSPACE_INDEX_IDLE_TTL);
174 const markSearched = (indexKey: string, now: number) => {
175 // A record older than the timeout counts the same as none, so it can go.
176 for (const [key, searchedAt] of lastSearchedAt) {
177 if (now - searchedAt >= indexIdleMillis) lastSearchedAt.delete(key);
178 }
179 lastSearchedAt.set(indexKey, now);
180 };
181
182 const refresh: WorkspaceEntries["Service"]["refresh"] = Effect.fn("WorkspaceEntries.refresh")(
183 function* (cwd) {
184 const normalizedCwd = yield* normalizeWorkspaceRoot(cwd).pipe(
185 Effect.orElseSucceed(() => cwd),
186 );
187 for (const variant of WorkspaceSearchIndex.WORKSPACE_SEARCH_INDEX_VARIANTS) {
188 const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, variant);
189 if (!(yield* RcMap.has(workspaceSearchIndexes.rcMap, indexKey))) {
190 continue;
191 }
192 const searchedAt = lastSearchedAt.get(indexKey) ?? 0;
193 if ((yield* Clock.currentTimeMillis) - searchedAt >= indexIdleMillis) {
194 lastSearchedAt.delete(indexKey);
195 yield* workspaceSearchIndexes.invalidate(indexKey);
196 continue;
197 }
198 const recoverRefreshFailure = Effect.fn("WorkspaceEntries.recoverRefreshFailure")(
199 function* (
200 cause:
201 | WorkspaceSearchIndex.WorkspaceSearchIndexCreateFailed
202 | WorkspaceSearchIndex.WorkspaceSearchIndexRefreshFailed,
203 ) {
204 yield* Effect.logWarning("Failed to refresh workspace search index", {
205 cwd,
206 variant,
207 cause,
208 });
209 yield* workspaceSearchIndexes.invalidate(indexKey);
210 },
211 );
212 yield* Effect.gen(function* () {
213 const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
214 yield* searchIndex.refresh();
215 }).pipe(
216 Effect.provide(workspaceSearchIndexes.get(indexKey)),
217 Effect.catchTags({
218 WorkspaceSearchIndexCreateFailed: recoverRefreshFailure,
219 WorkspaceSearchIndexRefreshFailed: recoverRefreshFailure,
220 }),
221 );
222 }
223 },
224 );
225
226 const browse: WorkspaceEntries["Service"]["browse"] = Effect.fn("WorkspaceEntries.browse")(
227 function* (input) {
228 const resolvedInputPath = yield* resolveBrowseTarget(input, path);
229 const endsWithSeparator = /[\\/]$/.test(input.partialPath) || input.partialPath === "~";
230 const parentPath = endsWithSeparator ? resolvedInputPath : path.dirname(resolvedInputPath);
231 const prefix = endsWithSeparator ? "" : path.basename(resolvedInputPath);
232
233 const dirents = yield* Effect.tryPromise({
234 try: () => NodeFSP.readdir(parentPath, { withFileTypes: true }),
235 catch: (cause) =>
236 new WorkspaceEntriesReadDirectoryError({
237 cwd: input.cwd,
238 partialPath: input.partialPath,
239 parentPath,
240 cause,
241 }),
242 }).pipe(
243 Effect.catchIf(
244 (error) => {
245 const code = (error.cause as NodeJS.ErrnoException | undefined)?.code;
246 return code === "EACCES" || code === "EPERM";
247 },
248 () => Effect.succeed([]),
249 ),
250 );
251
252 const showHidden = endsWithSeparator || prefix.startsWith(".");
253 const lowerPrefix = prefix.toLowerCase();
254 const entries: Array<{ readonly name: string; readonly fullPath: string }> = [];
255 for (const dirent of dirents) {
256 if (
257 dirent.isDirectory() &&
258 dirent.name.toLowerCase().startsWith(lowerPrefix) &&
259 (showHidden || !dirent.name.startsWith("."))
260 ) {
261 entries.push({
262 name: dirent.name,
263 fullPath: path.join(parentPath, dirent.name),
264 });
265 }
266 }
267
268 return {
269 parentPath,
270 entries: entries.toSorted((left, right) => left.name.localeCompare(right.name)),
271 };
272 },
273 );
274
275 const search: WorkspaceEntries["Service"]["search"] = Effect.fn("WorkspaceEntries.search")(
276 function* (input) {
277 const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd);
278 const normalizedQuery = normalizeSearchQuery(input.query, {
279 trimLeadingPattern: /^[@./]+/,
280 });
281 const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths");
282 markSearched(indexKey, yield* Clock.currentTimeMillis);
283 const result = yield* Effect.gen(function* () {
284 const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
285 return yield* searchIndex.search(normalizedQuery, input.limit, input.kind, input.imageOnly);
286 }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey)));
287 if (input.kind === "directory" || input.imageOnly) return result;
288 const worktreeFiles = yield* findNestedWorktreeFiles(
289 normalizedCwd,
290 input.query,
291 result.entries,
292 );
293 if (worktreeFiles.length === 0) return result;
294 const entries = [...worktreeFiles, ...result.entries];
295 return {
296 entries: entries.slice(0, input.limit),
297 truncated: result.truncated || entries.length > input.limit,
298 };
299 },
300 );
301
302 // The index skips gitignored folders, where agents often put worktrees (`.worktrees/x`).
303 // A path relative to such a worktree is found only when it names an existing file exactly.
304 const findNestedWorktreeFiles = Effect.fn("WorkspaceEntries.findNestedWorktreeFiles")(function* (
305 cwd: string,
306 query: string,
307 indexed: ReadonlyArray<ProjectEntry>,
308 ) {
309 const relativePath = query.trim().replaceAll("\\", "/");
310 if (
311 !relativePath.includes("/") ||
312 relativePath.endsWith("/") ||
313 path.isAbsolute(relativePath) ||
314 isWindowsAbsolutePath(relativePath) ||
315 relativePath.split("/").some((segment) => segment === "..") ||
316 indexed.some(
317 (entry) =>
318 entry.kind === "file" &&
319 (entry.path === relativePath || entry.path.endsWith(`/${relativePath}`)),
320 )
321 ) {
322 return [];
323 }
324 const listWorktrees = (args: ReadonlyArray<string>) =>
325 vcsProcess
326 .run({
327 operation: "WorkspaceEntries.listWorktrees",
328 command: "git",
329 args: ["worktree", "list", "--porcelain", ...args],
330 cwd,
331 allowNonZeroExit: true,
332 timeoutMs: 5_000,
333 })
334 .pipe(
335 Effect.map((result) => (result.exitCode === 0 ? result.stdout : undefined)),
336 Effect.orElseSucceed(() => undefined),
337 );
338 // Only NUL framing keeps a newline in a worktree path. Pre-2.36 Git rejects `-z`.
339 const listed =
340 (yield* listWorktrees(["-z"])) ?? (yield* listWorktrees([]))?.replaceAll("\n", "\0");
341 if (listed === undefined) return [];
342 const realCwd = yield* Effect.promise(() => NodeFSP.realpath(cwd).catch(() => cwd));
343 const worktreeRoots = listed.split("\0\0").flatMap((record) => {
344 const fields = record.split("\0");
345 const worktree = fields.find((field) => field.startsWith("worktree "));
346 // A `prunable` record's directory is gone; another checkout may now sit there.
347 if (worktree === undefined || fields.some((field) => field.startsWith("prunable"))) return [];
348 const root = worktree.slice("worktree ".length);
349 const relativeRoot = path.relative(realCwd, root);
350 return relativeRoot === "" ||
351 relativeRoot === ".." ||
352 relativeRoot.startsWith(`..${path.sep}`) ||
353 path.isAbsolute(relativeRoot)
354 ? []
355 : [relativeRoot.split(path.sep).join("/")];
356 });
357 const matches = yield* Effect.promise(() =>
358 Promise.all(
359 worktreeRoots.map(async (relativeRoot) => {
360 const candidate = `${relativeRoot}/${relativePath}`;
361 const stat = await NodeFSP.stat(path.join(realCwd, candidate)).catch(() => null);
362 return stat?.isFile() ? candidate : null;
363 }),
364 ),
365 );
366 return matches
367 .filter((candidate) => candidate !== null)
368 .map((candidate): ProjectEntry => ({ path: candidate, kind: "file" }));
369 });
370
371 const searchContents: WorkspaceEntries["Service"]["searchContents"] = Effect.fn(
372 "WorkspaceEntries.searchContents",
373 )(function* (input) {
374 const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd);
375 const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "content");
376 markSearched(indexKey, yield* Clock.currentTimeMillis);
377 return yield* Effect.gen(function* () {
378 const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
379 return yield* searchIndex.searchContents(input);
380 }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey)));
381 });
382
383 const list: WorkspaceEntries["Service"]["list"] = Effect.fn("WorkspaceEntries.list")(
384 function* (input) {
385 const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd);
386 if (input.directoryPath !== undefined) {
387 const directoryPath = input.directoryPath;
388 const toError = (cause: unknown) =>
389 new WorkspaceEntriesReadDirectoryError({
390 cwd: normalizedCwd,
391 partialPath: directoryPath,
392 parentPath: path.resolve(normalizedCwd, directoryPath),
393 cause,
394 });
395 const target =
396 directoryPath === ""
397 ? { absolutePath: normalizedCwd, relativePath: "" }
398 : yield* workspacePaths
399 .resolveRelativePathWithinRoot({
400 workspaceRoot: normalizedCwd,
401 relativePath: directoryPath,
402 })
403 .pipe(Effect.mapError(toError));
404 const { entries, throughSymlink, nestedGitRoots } = yield* Effect.tryPromise({
405 try: async () => {
406 const root = await NodeFSP.realpath(normalizedCwd);
407 const directory = await NodeFSP.realpath(target.absolutePath);
408 const relative = path.relative(root, directory);
409 if (
410 relative === ".." ||
411 relative.startsWith(`..${path.sep}`) ||
412 path.isAbsolute(relative) ||
413 relative.split(path.sep).includes(".git") ||
414 target.relativePath.split("/").includes(".git")
415 ) {
416 throw new Error("Directory must be inside the workspace and outside .git.");
417 }
418 const children = await NodeFSP.readdir(directory, { withFileTypes: true });
419 const isSameOrInside = (parent: string, child: string) => {
420 const relativeChild = path.relative(parent, child);
421 return (
422 relativeChild !== ".." &&
423 !relativeChild.startsWith(`..${path.sep}`) &&
424 !path.isAbsolute(relativeChild)
425 );
426 };
427 // Reject canonical ancestors so "Expand all folders" cannot follow a cycle.
428 let visitedFolders: Promise<ReadonlyArray<string>> | undefined;
429 const loadVisitedFolders = () =>
430 (visitedFolders ??= (async () => {
431 const folders = [root];
432 let current = normalizedCwd;
433 for (const segment of target.relativePath.split("/").filter(Boolean)) {
434 current = path.join(current, segment);
435 folders.push(await NodeFSP.realpath(current));
436 }
437 return folders;
438 })());
439 const resolveSymlinkKind = async (
440 name: string,
441 ): Promise<ProjectEntry["kind"] | undefined> => {
442 const resolved = await NodeFSP.realpath(path.join(directory, name)).catch(
443 () => undefined,
444 );
445 if (
446 resolved === undefined ||
447 !isSameOrInside(root, resolved) ||
448 path.relative(root, resolved).split(path.sep).includes(".git")
449 ) {
450 return undefined;
451 }
452 const stat = await NodeFSP.stat(resolved).catch(() => undefined);
453 if (stat?.isFile()) return "file";
454 if (!stat?.isDirectory()) return undefined;
455 const folders = await loadVisitedFolders();
456 return folders.some((folder) => isSameOrInside(resolved, folder))
457 ? undefined
458 : "directory";
459 };
460 // Bound filesystem work for directories such as pnpm node_modules.
461 const kinds: Array<ProjectEntry["kind"] | undefined> = [];
462 for (let offset = 0; offset < children.length; offset += SYMLINK_RESOLVE_BATCH_SIZE) {
463 kinds.push(
464 ...(await Promise.all(
465 children
466 .slice(offset, offset + SYMLINK_RESOLVE_BATCH_SIZE)
467 .map(async (child): Promise<ProjectEntry["kind"] | undefined> => {
468 if (child.name === ".git") return undefined;
469 if (child.isDirectory()) return "directory";
470 if (child.isFile()) return "file";
471 return child.isSymbolicLink() ? resolveSymlinkKind(child.name) : undefined;
472 }),
473 )),
474 );
475 }
476 const nestedGitRoots: Array<string> = [];
477 const segments = target.relativePath.split("/").filter(Boolean);
478 for (let index = 1; index <= segments.length; index += 1) {
479 const candidate = segments.slice(0, index).join("/");
480 const dotGit = await NodeFSP.lstat(path.join(normalizedCwd, candidate, ".git")).catch(
481 () => null,
482 );
483 if (dotGit) nestedGitRoots.push(candidate);
484 }
485 return {
486 nestedGitRoots,
487 entries: children.flatMap((child, index): ProjectEntry[] => {
488 const kind = kinds[index];
489 if (kind === undefined) return [];
490 return [
491 {
492 path: target.relativePath ? `${target.relativePath}/${child.name}` : child.name,
493 kind,
494 },
495 ];
496 }),
497 // Git refuses to classify paths beyond a symbolic link.
498 throughSymlink: relative !== target.relativePath.split("/").join(path.sep),
499 };
500 },
501 catch: toError,
502 });
503 let gitRoot = "";
504 let gitPrefix = "";
505 // stdin avoids argument limits; ignore classification is optional without git/repositories.
506 const checkIgnore = (paths: ReadonlyArray<string>) =>
507 vcsProcess
508 .run({
509 operation: "WorkspaceEntries.list",
510 command: "git",
511 args: ["-c", "core.fsmonitor=false", "check-ignore", "-z", "--stdin"],
512 cwd: path.join(normalizedCwd, gitRoot),
513 stdin: `${paths.map((entryPath) => entryPath.slice(gitPrefix.length)).join("\0")}\0`,
514 allowNonZeroExit: true,
515 timeoutMs: 10_000,
516 maxOutputBytes: 16 * 1024 * 1024,
517 })
518 .pipe(Effect.orElseSucceed(() => undefined));
519 // Git refuses paths inside a submodule (128) but still answers for nested clones.
520 for (const nestedGitRoot of nestedGitRoots) {
521 if ((yield* checkIgnore([`${nestedGitRoot}/.git`]))?.exitCode !== 128) continue;
522 gitRoot = nestedGitRoot;
523 gitPrefix = `${gitRoot}/`;
524 }
525 const directoryGitPath = target.relativePath.slice(gitPrefix.length);
526 // Check ignored folders once to avoid child index lookups; exit 0 guarantees no tracked descendants.
527 // Symlinks and pathspec magic still need per-child checks.
528 if (
529 entries.length > IGNORE_CHECK_CHUNK_SIZE &&
530 directoryGitPath !== "" &&
531 !throughSymlink &&
532 !/^:|[*?[\\]/.test(directoryGitPath) &&
533 (yield* checkIgnore([target.relativePath]))?.exitCode === 0
534 ) {
535 return {
536 entries: entries.map((entry) => ({ ...entry, ignored: true })),
537 truncated: false,
538 };
539 }
540 const ignored = new Set<string>();
541 for (let offset = 0; offset < entries.length; offset += IGNORE_CHECK_CHUNK_SIZE) {
542 const result = yield* checkIgnore(
543 entries.slice(offset, offset + IGNORE_CHECK_CHUNK_SIZE).map((entry) => entry.path),
544 );
545 if (!result || (result.exitCode !== 0 && result.exitCode !== 1)) break;
546 for (const ignoredPath of result.stdout.split("\0")) ignored.add(gitPrefix + ignoredPath);
547 }
548 return {
549 entries: entries.map((entry) =>
550 ignored.has(entry.path) ? { ...entry, ignored: true } : entry,
551 ),
552 truncated: false,
553 };
554 }
555 const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths");
556 markSearched(indexKey, yield* Clock.currentTimeMillis);
557 return yield* Effect.gen(function* () {
558 const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
559 return yield* searchIndex.list();
560 }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey)));
561 },
562 );
563
564 const listIgnoredFiles: WorkspaceEntries["Service"]["listIgnoredFiles"] = Effect.fn(
565 "WorkspaceEntries.listIgnoredFiles",
566 )(function* (input) {
567 const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd);
568 // `--directory` reports a folder holding nothing but ignored files as one
569 // `name/` entry, which is how `node_modules` and build output drop out.
570 const result = yield* vcsProcess
571 .run({
572 operation: "WorkspaceEntries.listIgnoredFiles",
573 command: "git",
574 args: [
575 "-c",
576 "core.fsmonitor=false",
577 "ls-files",
578 "--others",
579 "--ignored",
580 "--exclude-standard",
581 "--directory",
582 "--no-empty-directory",
583 "-z",
584 ],
585 cwd: normalizedCwd,
586 allowNonZeroExit: true,
587 timeoutMs: 10_000,
588 maxOutputBytes: 4 * 1024 * 1024,
589 })
590 .pipe(Effect.orElseSucceed(() => undefined));
591 if (!result || result.exitCode !== 0) return { files: [], truncated: false };
592 // Filter before the cap so files sorting past it stay reachable by search.
593 const query = input.query?.toLowerCase() ?? "";
594 const paths = result.stdout
595 .split("\0")
596 .filter(
597 (entry) => entry.length > 0 && !entry.endsWith("/") && entry.toLowerCase().includes(query),
598 )
599 .toSorted();
600 const files = yield* Effect.promise(() =>
601 Promise.all(
602 paths.slice(0, IGNORED_FILES_LIMIT).map(async (relativePath) => {
603 // lstat: a symlink usually points outside the workspace, where reads refuse to go.
604 const stat = await NodeFSP.lstat(path.join(normalizedCwd, relativePath)).catch(
605 () => null,
606 );
607 return stat?.isFile() ? { path: relativePath, byteLength: stat.size } : null;
608 }),
609 ),
610 );
611 return {
612 files: files.filter((file) => file !== null),
613 truncated: paths.length > IGNORED_FILES_LIMIT,
614 };
615 });
616
617 return WorkspaceEntries.of({ browse, list, listIgnoredFiles, refresh, search, searchContents });
618 });
619
620 export const layer = Layer.effect(WorkspaceEntries, make).pipe(
621 Layer.provide(WorkspaceSearchIndex.WorkspaceSearchIndexMap.layer),
622 Layer.provide(VcsProcess.layer),
623 );