Skip to content

Commit a3cf3f0

Browse files
fix(server): the Files tree lists symlinked files and folders (pingdotgg#21920)
Co-authored-by: maria-rcks <[email protected]>
1 parent ff55c19 commit a3cf3f0

2 files changed

Lines changed: 172 additions & 3 deletions

File tree

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeFSP from "node:fs/promises";
23
import * as NodeServices from "@effect/platform-node/NodeServices";
34
import { it, afterEach, describe, expect } from "@effect/vitest";
45
import * as Effect from "effect/Effect";
@@ -132,6 +133,113 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
132133
}),
133134
);
134135

136+
it.effect("lists symlinks as what they point to, hiding ones that cannot be opened", () =>
137+
Effect.gen(function* () {
138+
const platform = yield* HostProcess.Platform;
139+
if (platform === "win32") return;
140+
const cwd = yield* makeTempDir({ git: true });
141+
const outside = yield* makeTempDir();
142+
const fileSystem = yield* FileSystem.FileSystem;
143+
const path = yield* Path.Path;
144+
yield* writeTextFile(cwd, "real.txt");
145+
yield* writeTextFile(cwd, "realdir/nested.txt");
146+
yield* writeTextFile(outside, "outside.txt");
147+
yield* fileSystem.symlink("real.txt", path.join(cwd, "link.txt"));
148+
yield* fileSystem.symlink("realdir", path.join(cwd, "linkdir"));
149+
yield* fileSystem.symlink(outside, path.join(cwd, "outside-link"));
150+
yield* fileSystem.symlink(".git", path.join(cwd, "git-link"));
151+
yield* fileSystem.symlink("missing", path.join(cwd, "dangling"));
152+
153+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
154+
const root = yield* workspaceEntries.list({ cwd, directoryPath: "" });
155+
expect(root.entries.toSorted((a, b) => a.path.localeCompare(b.path))).toEqual([
156+
{ path: "link.txt", kind: "file" },
157+
{ path: "linkdir", kind: "directory" },
158+
{ path: "real.txt", kind: "file" },
159+
{ path: "realdir", kind: "directory" },
160+
]);
161+
expect(yield* workspaceEntries.list({ cwd, directoryPath: "linkdir" })).toEqual({
162+
entries: [{ path: "linkdir/nested.txt", kind: "file" }],
163+
truncated: false,
164+
});
165+
}),
166+
);
167+
168+
it.effect("hides folder links that lead back to a folder on the listed path", () =>
169+
// "Expand all folders" loads every folder it expands, so a link that loops would
170+
// keep nesting the same entries without end.
171+
Effect.gen(function* () {
172+
const platform = yield* HostProcess.Platform;
173+
if (platform === "win32") return;
174+
const cwd = yield* makeTempDir();
175+
const fileSystem = yield* FileSystem.FileSystem;
176+
const path = yield* Path.Path;
177+
yield* writeTextFile(cwd, "a/file.txt");
178+
yield* writeTextFile(cwd, "b/file.txt");
179+
yield* fileSystem.symlink(".", path.join(cwd, "self"));
180+
yield* fileSystem.symlink("..", path.join(cwd, "a/up"));
181+
yield* fileSystem.symlink("../b", path.join(cwd, "a/to-b"));
182+
yield* fileSystem.symlink("../a", path.join(cwd, "b/to-a"));
183+
184+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
185+
const paths = (directoryPath: string) =>
186+
workspaceEntries
187+
.list({ cwd, directoryPath })
188+
.pipe(Effect.map(({ entries }) => entries.map((entry) => entry.path).toSorted()));
189+
expect(yield* paths("")).toEqual(["a", "b"]);
190+
// A link into a sibling folder is fine on its own...
191+
expect(yield* paths("a")).toEqual(["a/file.txt", "a/to-b"]);
192+
// ...but once followed, the sibling's link back is hidden.
193+
expect(yield* paths("a/to-b")).toEqual(["a/to-b/file.txt"]);
194+
}),
195+
);
196+
197+
it.effect("resolves a folder of many symlinks in bounded batches", () =>
198+
Effect.gen(function* () {
199+
const platform = yield* HostProcess.Platform;
200+
if (platform === "win32") return;
201+
const cwd = yield* makeTempDir();
202+
const fileSystem = yield* FileSystem.FileSystem;
203+
const path = yield* Path.Path;
204+
// Shaped like a pnpm `node_modules`: one link per package into `.pnpm`.
205+
const directoryPath = [...Array.from({ length: 80 }, () => "nested"), "node_modules"].join(
206+
"/",
207+
);
208+
const packages = Array.from({ length: 200 }, (_, index) => `pkg-${index}`);
209+
yield* Effect.forEach(
210+
packages,
211+
(name) =>
212+
Effect.gen(function* () {
213+
yield* writeTextFile(cwd, `${directoryPath}/.pnpm/${name}/index.js`);
214+
yield* fileSystem.symlink(`.pnpm/${name}`, path.join(cwd, directoryPath, name));
215+
}),
216+
{ concurrency: 32, discard: true },
217+
);
218+
const realpath = NodeFSP.realpath;
219+
let inFlight = 0;
220+
let maxInFlight = 0;
221+
vi.spyOn(NodeFSP, "realpath").mockImplementation((async (target: string) => {
222+
maxInFlight = Math.max(maxInFlight, ++inFlight);
223+
try {
224+
return await realpath(target);
225+
} finally {
226+
inFlight--;
227+
}
228+
}) as typeof NodeFSP.realpath);
229+
230+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
231+
const { entries } = yield* workspaceEntries.list({ cwd, directoryPath });
232+
expect(entries.map((entry) => entry.path).toSorted()).toEqual(
233+
[
234+
`${directoryPath}/.pnpm`,
235+
...packages.map((name) => `${directoryPath}/${name}`),
236+
].toSorted(),
237+
);
238+
expect(entries.every((entry) => entry.kind === "directory")).toBe(true);
239+
expect(maxInFlight).toBeLessThanOrEqual(64);
240+
}),
241+
);
242+
135243
it.effect("labels the children of wide folders exactly as git does for each child", () =>
136244
Effect.gen(function* () {
137245
// Above 1,000 children, listing first asks whether the whole folder is ignored.

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ const IGNORED_FILES_LIMIT = 200;
117117
/** Paths sent to one `git check-ignore` call when labelling a folder's children. */
118118
const IGNORE_CHECK_CHUNK_SIZE = 1000;
119119

120+
/** Children classified at once when listing a folder; each symlink costs a realpath and a stat. */
121+
const SYMLINK_RESOLVE_BATCH_SIZE = 64;
122+
120123
const resolveBrowseTarget = Effect.fn("WorkspaceEntries.resolveBrowseTarget")(function* (
121124
input: FilesystemBrowseInput,
122125
path: Path.Path,
@@ -318,13 +321,71 @@ export const make = Effect.gen(function* () {
318321
throw new Error("Directory must be inside the workspace and outside .git.");
319322
}
320323
const children = await NodeFSP.readdir(directory, { withFileTypes: true });
324+
const isSameOrInside = (parent: string, child: string) => {
325+
const relativeChild = path.relative(parent, child);
326+
return (
327+
relativeChild !== ".." &&
328+
!relativeChild.startsWith(`..${path.sep}`) &&
329+
!path.isAbsolute(relativeChild)
330+
);
331+
};
332+
// Reject canonical ancestors so "Expand all folders" cannot follow a cycle.
333+
let visitedFolders: Promise<ReadonlyArray<string>> | undefined;
334+
const loadVisitedFolders = () =>
335+
(visitedFolders ??= (async () => {
336+
const folders = [root];
337+
let current = normalizedCwd;
338+
for (const segment of target.relativePath.split("/").filter(Boolean)) {
339+
current = path.join(current, segment);
340+
folders.push(await NodeFSP.realpath(current));
341+
}
342+
return folders;
343+
})());
344+
const resolveSymlinkKind = async (
345+
name: string,
346+
): Promise<ProjectEntry["kind"] | undefined> => {
347+
const resolved = await NodeFSP.realpath(path.join(directory, name)).catch(
348+
() => undefined,
349+
);
350+
if (
351+
resolved === undefined ||
352+
!isSameOrInside(root, resolved) ||
353+
path.relative(root, resolved).split(path.sep).includes(".git")
354+
) {
355+
return undefined;
356+
}
357+
const stat = await NodeFSP.stat(resolved).catch(() => undefined);
358+
if (stat?.isFile()) return "file";
359+
if (!stat?.isDirectory()) return undefined;
360+
const folders = await loadVisitedFolders();
361+
return folders.some((folder) => isSameOrInside(resolved, folder))
362+
? undefined
363+
: "directory";
364+
};
365+
// Bound filesystem work for directories such as pnpm node_modules.
366+
const kinds: Array<ProjectEntry["kind"] | undefined> = [];
367+
for (let offset = 0; offset < children.length; offset += SYMLINK_RESOLVE_BATCH_SIZE) {
368+
kinds.push(
369+
...(await Promise.all(
370+
children
371+
.slice(offset, offset + SYMLINK_RESOLVE_BATCH_SIZE)
372+
.map(async (child): Promise<ProjectEntry["kind"] | undefined> => {
373+
if (child.name === ".git") return undefined;
374+
if (child.isDirectory()) return "directory";
375+
if (child.isFile()) return "file";
376+
return child.isSymbolicLink() ? resolveSymlinkKind(child.name) : undefined;
377+
}),
378+
)),
379+
);
380+
}
321381
return {
322-
entries: children.flatMap((child): ProjectEntry[] => {
323-
if (child.name === ".git" || (!child.isDirectory() && !child.isFile())) return [];
382+
entries: children.flatMap((child, index): ProjectEntry[] => {
383+
const kind = kinds[index];
384+
if (kind === undefined) return [];
324385
return [
325386
{
326387
path: target.relativePath ? `${target.relativePath}/${child.name}` : child.name,
327-
kind: child.isDirectory() ? "directory" : "file",
388+
kind,
328389
},
329390
];
330391
}),

0 commit comments

Comments
 (0)