Skip to content
TypeScript

perf(server): expanding a wide ignored folder no longer classifies every child (#20062)

Julius Marmingeccca13dcf4a7
diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts
index 0d716b22375..10083f6c3f1 100644
--- a/apps/server/src/workspace/WorkspaceEntries.test.ts
+++ b/apps/server/src/workspace/WorkspaceEntries.test.ts
@@ -134,6 +134,76 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
}),
);
+ it.effect("labels the children of wide folders exactly as git does for each child", () =>
+ Effect.gen(function* () {
+ // Above 1,000 children, listing first asks whether the whole folder is ignored.
+ const cwd = yield* makeTempDir({ git: true });
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const platform = yield* HostProcess.Platform;
+ yield* writeTextFile(
+ cwd,
+ ".gitignore",
+ "node_modules/\ndist/\n\\[id\\]/\n*.map\nfoo\n!foo/\n",
+ );
+ yield* Effect.forEach(
+ [
+ "node_modules",
+ "dist",
+ "[id]",
+ "src",
+ ...(platform !== "win32" ? [":foo"] : []),
+ ].flatMap((directory) =>
+ Array.from({ length: 1001 }, (_, index) => `${directory}/pkg-${index}.js`),
+ ),
+ (relativePath) => writeTextFile(cwd, relativePath),
+ { concurrency: 32, discard: true },
+ );
+ for (const relativePath of [
+ "dist/kept.js",
+ "dist/vendored/index.js",
+ "i/page.tsx",
+ "[id]/page.tsx",
+ "src/bundle.map",
+ ]) {
+ yield* writeTextFile(cwd, relativePath);
+ }
+ yield* git(cwd, ["add", "-f", "dist/kept.js", "dist/vendored/index.js", "i/page.tsx"]);
+ if (platform !== "win32") {
+ yield* fileSystem.symlink(path.join(cwd, "src"), path.join(cwd, "node_modules/link"));
+ }
+
+ const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
+ for (const { directoryPath, ignored, except } of [
+ { directoryPath: "node_modules", ignored: true, except: [] },
+ // A force-tracked file and a folder holding tracked files stay unignored.
+ { directoryPath: "dist", ignored: true, except: ["dist/kept.js", "dist/vendored"] },
+ // git reads `[id]/page.tsx` as a pathspec, which matches the tracked `i/page.tsx`.
+ { directoryPath: "[id]", ignored: true, except: ["[id]/page.tsx"] },
+ { directoryPath: "src", ignored: false, except: ["src/bundle.map"] },
+ ...(platform !== "win32"
+ ? [
+ // git refuses to classify paths beyond a symlink, so nothing there is labelled.
+ { directoryPath: "node_modules/link", ignored: false, except: [] },
+ // A leading `:` is pathspec magic, so git reads `:foo/pkg-0.js` as `foo/pkg-0.js`,
+ // which `!foo/` keeps unignored.
+ { directoryPath: ":foo", ignored: false, except: [] },
+ ]
+ : []),
+ ]) {
+ const { entries } = yield* workspaceEntries.list({ cwd, directoryPath });
+ expect(entries.length, directoryPath).toBeGreaterThan(1000);
+ expect(
+ entries
+ .filter((entry) => (entry.ignored === true) !== ignored)
+ .map((entry) => entry.path)
+ .toSorted(),
+ directoryPath,
+ ).toEqual(except);
+ }
+ }),
+ );
+
it.effect("lists a host folder and its parent, each from its own root", () =>
// Folder links outside the workspace browse by passing the host folder as
// `cwd`; walking up passes the parent. Each stays bounded by its own root.
diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts
index 8f312223dae..607db665de7 100644
--- a/apps/server/src/workspace/WorkspaceEntries.ts
+++ b/apps/server/src/workspace/WorkspaceEntries.ts
@@ -106,6 +106,9 @@ export class WorkspaceEntries extends Context.Service<
}
>()("t3/workspace/WorkspaceEntries") {}
+/** Paths sent to one `git check-ignore` call when labelling a folder's children. */
+const IGNORE_CHECK_CHUNK_SIZE = 1000;
+
const resolveBrowseTarget = Effect.fn("WorkspaceEntries.resolveBrowseTarget")(function* (
input: FilesystemBrowseInput,
path: Path.Path,
@@ -291,7 +294,7 @@ export const make = Effect.gen(function* () {
relativePath: directoryPath,
})
.pipe(Effect.mapError(toError));
- const entries = yield* Effect.tryPromise({
+ const { entries, throughSymlink } = yield* Effect.tryPromise({
try: async () => {
const root = await NodeFSP.realpath(normalizedCwd);
const directory = await NodeFSP.realpath(target.absolutePath);
@@ -306,35 +309,61 @@ export const make = Effect.gen(function* () {
throw new Error("Directory must be inside the workspace and outside .git.");
}
const children = await NodeFSP.readdir(directory, { withFileTypes: true });
- return children.flatMap((child): ProjectEntry[] => {
- if (child.name === ".git" || (!child.isDirectory() && !child.isFile())) return [];
- return [
- {
- path: target.relativePath ? `${target.relativePath}/${child.name}` : child.name,
- kind: child.isDirectory() ? "directory" : "file",
- },
- ];
- });
+ return {
+ entries: children.flatMap((child): ProjectEntry[] => {
+ if (child.name === ".git" || (!child.isDirectory() && !child.isFile())) return [];
+ return [
+ {
+ path: target.relativePath ? `${target.relativePath}/${child.name}` : child.name,
+ kind: child.isDirectory() ? "directory" : "file",
+ },
+ ];
+ }),
+ // Git refuses to classify paths beyond a symbolic link.
+ throughSymlink: relative !== target.relativePath.split("/").join(path.sep),
+ };
},
catch: toError,
});
// Use stdin so large directories cannot exceed the command-line argument limit.
// Ignore classification is optional in non-git workspaces or when git is unavailable.
- const ignored = new Set<string>();
- for (let offset = 0; offset < entries.length; offset += 1000) {
- const chunk = entries.slice(offset, offset + 1000);
- const result = yield* vcsProcess
+ const checkIgnore = (paths: ReadonlyArray<string>) =>
+ vcsProcess
.run({
operation: "WorkspaceEntries.list",
command: "git",
args: ["-c", "core.fsmonitor=false", "check-ignore", "-z", "--stdin"],
cwd: normalizedCwd,
- stdin: `${chunk.map((entry) => entry.path).join("\0")}\0`,
+ stdin: `${paths.join("\0")}\0`,
allowNonZeroExit: true,
timeoutMs: 10_000,
maxOutputBytes: 16 * 1024 * 1024,
})
.pipe(Effect.orElseSucceed(() => undefined));
+ // Git consults the index for every path it classifies, which costs hundreds of
+ // milliseconds for a `node_modules`-sized folder. Asking about the folder itself answers
+ // for every child: `check-ignore` exits 0 only when the folder is ignored and nothing
+ // under it is tracked, and an ignored folder hides all of its children. Git reads paths as
+ // pathspecs, so a folder whose path has magic (`:foo`) or glob (`[id]`) characters, or
+ // one reached through a symlink, can get a different answer per child and still asks git
+ // about each.
+ if (
+ entries.length > IGNORE_CHECK_CHUNK_SIZE &&
+ target.relativePath !== "" &&
+ !throughSymlink &&
+ !/^:|[*?[\\]/.test(target.relativePath) &&
+ (yield* checkIgnore([target.relativePath]))?.exitCode === 0
+ ) {
+ return {
+ entries: entries.map((entry) => ({ ...entry, ignored: true })),
+ truncated: false,
+ };
+ }
+ const ignored = new Set<string>();
+ for (let offset = 0; offset < entries.length; offset += IGNORE_CHECK_CHUNK_SIZE) {
+ const result = yield* checkIgnore(
+ entries.slice(offset, offset + IGNORE_CHECK_CHUNK_SIZE).map((entry) => entry.path),
+ );
if (!result || (result.exitCode !== 0 && result.exitCode !== 1)) break;
for (const ignoredPath of result.stdout.split("\0")) ignored.add(ignoredPath);
}