Skip to content

Commit 67e2b39

Browse files
committed
fix(server): show symlinked directories in the project picker
1 parent 3b22620 commit 67e2b39

2 files changed

Lines changed: 165 additions & 14 deletions

File tree

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @effect-diagnostics nodeBuiltinImport:off
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as NodeFSP from "node:fs/promises";
34
import { it, afterEach, describe, expect } from "@effect/vitest";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
@@ -568,6 +569,133 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
568569
});
569570

570571
describe("browse", () => {
572+
it.effect(
573+
"offers directory links in browsing and autocomplete while retaining their symlink paths",
574+
() =>
575+
Effect.gen(function* () {
576+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
577+
const path = yield* Path.Path;
578+
const cwd = yield* makeTempDir({ prefix: "t3code-picker-links-" });
579+
const target = yield* makeTempDir({ prefix: "t3code-picker-target-" });
580+
yield* writeTextFile(target, "nested-project/package.json", "{}");
581+
yield* writeTextFile(cwd, "ordinary/package.json", "{}");
582+
const names = ["linked-project", "Templates", "Recent", ".linked-hidden"];
583+
for (const name of names) {
584+
yield* Effect.tryPromise(() =>
585+
NodeFSP.symlink(target, path.join(cwd, name), "junction"),
586+
);
587+
}
588+
const root = yield* appendSeparator(cwd);
589+
const directory = yield* workspaceEntries.browse({ partialPath: root });
590+
expect(directory).toEqual({
591+
parentPath: cwd,
592+
entries: [...names, "ordinary"]
593+
.toSorted((a, b) => a.localeCompare(b))
594+
.map((name) => ({
595+
name,
596+
fullPath: path.join(cwd, name),
597+
})),
598+
});
599+
expect(yield* workspaceEntries.browse({ partialPath: `${root}LiNk` })).toEqual({
600+
parentPath: cwd,
601+
entries: [{ name: "linked-project", fullPath: path.join(cwd, "linked-project") }],
602+
});
603+
expect(yield* workspaceEntries.browse({ partialPath: `${root}.link` })).toEqual({
604+
parentPath: cwd,
605+
entries: [{ name: ".linked-hidden", fullPath: path.join(cwd, ".linked-hidden") }],
606+
});
607+
const alias = path.join(cwd, "linked-project");
608+
expect(
609+
yield* workspaceEntries.browse({ partialPath: yield* appendSeparator(alias) }),
610+
).toEqual({
611+
parentPath: alias,
612+
entries: [{ name: "nested-project", fullPath: path.join(alias, "nested-project") }],
613+
});
614+
}),
615+
);
616+
617+
it.effect(
618+
"omits file, broken, cyclic, and inaccessible link targets without losing other directories",
619+
() =>
620+
Effect.gen(function* () {
621+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
622+
const path = yield* Path.Path;
623+
const cwd = yield* makeTempDir({ prefix: "t3code-picker-link-errors-" });
624+
const target = yield* makeTempDir({ prefix: "t3code-picker-link-target-" });
625+
yield* writeTextFile(cwd, "plain-file.txt");
626+
yield* writeTextFile(cwd, "ordinary/package.json", "{}");
627+
for (const name of [
628+
"linked-good",
629+
"linked-file",
630+
"linked-broken",
631+
"linked-loop",
632+
"linked-denied",
633+
]) {
634+
yield* Effect.tryPromise(() =>
635+
NodeFSP.symlink(target, path.join(cwd, name), "junction"),
636+
);
637+
}
638+
const originalStat = NodeFSP.stat;
639+
const fileStat = yield* Effect.tryPromise(() =>
640+
originalStat(path.join(cwd, "plain-file.txt")),
641+
);
642+
const failures = new Map([
643+
[path.join(cwd, "linked-broken"), "ENOENT"],
644+
[path.join(cwd, "linked-loop"), "ELOOP"],
645+
[path.join(cwd, "linked-denied"), "EACCES"],
646+
]);
647+
// Inject target states so this also runs on Windows without file-symlink privileges.
648+
using _stat = vi.spyOn(NodeFSP, "stat").mockImplementation((file, options) => {
649+
const code = failures.get(String(file));
650+
if (code) return Promise.reject(Object.assign(new Error(code), { code }));
651+
if (String(file) === path.join(cwd, "linked-file")) return Promise.resolve(fileStat);
652+
return originalStat(file, options);
653+
});
654+
const result = yield* workspaceEntries.browse({
655+
partialPath: yield* appendSeparator(cwd),
656+
});
657+
expect(result.entries).toEqual([
658+
{ name: "linked-good", fullPath: path.join(cwd, "linked-good") },
659+
{ name: "ordinary", fullPath: path.join(cwd, "ordinary") },
660+
]);
661+
}),
662+
);
663+
664+
it.effect("omits direct and chained network links without statting their targets", () =>
665+
Effect.gen(function* () {
666+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
667+
const path = yield* Path.Path;
668+
const cwd = yield* makeTempDir({ prefix: "t3code-picker-network-" });
669+
const target = yield* makeTempDir({ prefix: "t3code-picker-local-" });
670+
const direct = path.join(cwd, "network");
671+
const chained = path.join(cwd, "chained");
672+
const local = path.join(cwd, "local");
673+
for (const link of [direct, local]) {
674+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
675+
}
676+
yield* Effect.tryPromise(() => NodeFSP.symlink(direct, chained, "junction"));
677+
const originalReadlink = NodeFSP.readlink;
678+
const networkTarget = String.raw`\\host\share`;
679+
using _readlink = vi
680+
.spyOn(NodeFSP, "readlink")
681+
.mockImplementation((file, options) =>
682+
String(file) === direct
683+
? Promise.resolve(networkTarget)
684+
: originalReadlink(file, options),
685+
);
686+
using stat = vi.spyOn(NodeFSP, "stat");
687+
using lstat = vi.spyOn(NodeFSP, "lstat");
688+
const result = yield* workspaceEntries.browse({
689+
partialPath: yield* appendSeparator(cwd),
690+
});
691+
expect(result.entries).toEqual([{ name: "local", fullPath: local }]);
692+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(direct);
693+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(chained);
694+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget);
695+
expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget);
696+
}),
697+
);
698+
571699
it.effect("shows dot directories in directory mode and hidden-prefix mode", () =>
572700
Effect.gen(function* () {
573701
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {
2222
ProjectSearchEntriesResult,
2323
} from "@t3tools/contracts";
2424
import * as HostProcess from "@t3tools/shared/HostProcess";
25-
import { isExplicitRelativePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
25+
import { isExplicitRelativePath, isNetworkPath, isWindowsAbsolutePath } from "@t3tools/shared/path";
2626
import { normalizeSearchQuery } from "@t3tools/shared/searchRanking";
2727

2828
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
@@ -198,6 +198,21 @@ export const make = Effect.gen(function* () {
198198
},
199199
);
200200

201+
const canFollowBrowseLink = Effect.fn("WorkspaceEntries.canFollowBrowseLink")(function* (
202+
fullPath: string,
203+
) {
204+
let currentPath = fullPath;
205+
for (let hop = 0; hop < 40; hop++) {
206+
const target = yield* Effect.tryPromise(() => NodeFSP.readlink(currentPath));
207+
// Inspect link targets before following them: UNC paths can trigger Windows NTLM sign-in.
208+
if (isNetworkPath(target)) return false;
209+
currentPath = path.resolve(path.dirname(currentPath), target);
210+
const stats = yield* Effect.tryPromise(() => NodeFSP.lstat(currentPath));
211+
if (!stats.isSymbolicLink()) return true;
212+
}
213+
return false;
214+
});
215+
201216
const browse: WorkspaceEntries["Service"]["browse"] = Effect.fn("WorkspaceEntries.browse")(
202217
function* (input) {
203218
const resolvedInputPath = yield* resolveBrowseTarget(input, path);
@@ -226,23 +241,31 @@ export const make = Effect.gen(function* () {
226241

227242
const showHidden = endsWithSeparator || prefix.startsWith(".");
228243
const lowerPrefix = prefix.toLowerCase();
229-
const entries: Array<{ readonly name: string; readonly fullPath: string }> = [];
230-
for (const dirent of dirents) {
231-
if (
232-
dirent.isDirectory() &&
244+
const candidates = dirents.filter(
245+
(dirent) =>
246+
(dirent.isDirectory() || dirent.isSymbolicLink()) &&
233247
dirent.name.toLowerCase().startsWith(lowerPrefix) &&
234-
(showHidden || !dirent.name.startsWith("."))
235-
) {
236-
entries.push({
237-
name: dirent.name,
238-
fullPath: path.join(parentPath, dirent.name),
239-
});
240-
}
241-
}
248+
(showHidden || !dirent.name.startsWith(".")),
249+
);
250+
const entries = yield* Effect.forEach(
251+
candidates,
252+
(dirent) => {
253+
const entry = { name: dirent.name, fullPath: path.join(parentPath, dirent.name) };
254+
if (dirent.isDirectory()) return Effect.succeed(entry);
255+
return Effect.gen(function* () {
256+
if (!(yield* canFollowBrowseLink(entry.fullPath))) return undefined;
257+
const stats = yield* Effect.tryPromise(() => NodeFSP.stat(entry.fullPath));
258+
return stats.isDirectory() ? entry : undefined;
259+
}).pipe(Effect.orElseSucceed(() => undefined));
260+
},
261+
{ concurrency: 16 },
262+
);
242263

243264
return {
244265
parentPath,
245-
entries: entries.toSorted((left, right) => left.name.localeCompare(right.name)),
266+
entries: entries
267+
.filter((entry) => entry !== undefined)
268+
.toSorted((left, right) => left.name.localeCompare(right.name)),
246269
};
247270
},
248271
);

0 commit comments

Comments
 (0)