Skip to content

Commit b8da7cb

Browse files
JRobzsPengLxclaude
committed
fix(server): show symlinked directories in the project picker
The Add Project browser and path autocomplete kept only real directories, so directory symlinks (for example a link to a project on another volume) never appeared. Directory links now appear with their link path. Dangling, file, cyclic and network targets stay hidden; each link component is inspected before it is followed so a UNC redirect is never touched, and a chosen link is resolved before its folder is read. Co-authored-by: PengLx <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7c22e24 commit b8da7cb

2 files changed

Lines changed: 332 additions & 15 deletions

File tree

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

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,271 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
747747
});
748748

749749
describe("browse", () => {
750+
it.effect(
751+
"offers directory links in browsing and autocomplete while retaining their symlink paths",
752+
() =>
753+
Effect.gen(function* () {
754+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
755+
const path = yield* Path.Path;
756+
const cwd = yield* makeTempDir({ prefix: "t3code-picker-links-" });
757+
const target = yield* makeTempDir({ prefix: "t3code-picker-target-" });
758+
yield* writeTextFile(target, "nested-project/package.json", "{}");
759+
yield* writeTextFile(cwd, "ordinary/package.json", "{}");
760+
const names = ["linked-project", "Templates", "Recent", ".linked-hidden"];
761+
for (const name of names) {
762+
yield* Effect.tryPromise(() =>
763+
NodeFSP.symlink(target, path.join(cwd, name), "junction"),
764+
);
765+
}
766+
const root = yield* appendSeparator(cwd);
767+
const directory = yield* workspaceEntries.browse({ partialPath: root });
768+
expect(directory).toEqual({
769+
parentPath: cwd,
770+
entries: [...names, "ordinary"]
771+
.toSorted((a, b) => a.localeCompare(b))
772+
.map((name) => ({
773+
name,
774+
fullPath: path.join(cwd, name),
775+
})),
776+
});
777+
expect(yield* workspaceEntries.browse({ partialPath: `${root}LiNk` })).toEqual({
778+
parentPath: cwd,
779+
entries: [{ name: "linked-project", fullPath: path.join(cwd, "linked-project") }],
780+
});
781+
expect(yield* workspaceEntries.browse({ partialPath: `${root}.link` })).toEqual({
782+
parentPath: cwd,
783+
entries: [{ name: ".linked-hidden", fullPath: path.join(cwd, ".linked-hidden") }],
784+
});
785+
const alias = path.join(cwd, "linked-project");
786+
expect(
787+
yield* workspaceEntries.browse({ partialPath: yield* appendSeparator(alias) }),
788+
).toEqual({
789+
parentPath: alias,
790+
entries: [{ name: "nested-project", fullPath: path.join(alias, "nested-project") }],
791+
});
792+
}),
793+
);
794+
795+
it.effect(
796+
"omits file, broken, cyclic, and inaccessible link targets without losing other directories",
797+
() =>
798+
Effect.gen(function* () {
799+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
800+
const path = yield* Path.Path;
801+
const cwd = yield* makeTempDir({ prefix: "t3code-picker-link-errors-" });
802+
const fs = yield* FileSystem.FileSystem;
803+
const target = yield* fs.realPath(
804+
yield* makeTempDir({ prefix: "t3code-picker-link-target-" }),
805+
);
806+
yield* writeTextFile(cwd, "plain-file.txt");
807+
yield* writeTextFile(cwd, "ordinary/package.json", "{}");
808+
for (const name of [
809+
"linked-good",
810+
"linked-file",
811+
"linked-broken",
812+
"linked-loop",
813+
"linked-denied",
814+
]) {
815+
yield* fs.makeDirectory(path.join(target, name));
816+
yield* Effect.tryPromise(() =>
817+
NodeFSP.symlink(path.join(target, name), path.join(cwd, name), "junction"),
818+
);
819+
}
820+
const originalStat = NodeFSP.stat;
821+
const fileStat = yield* Effect.tryPromise(() =>
822+
originalStat(path.join(cwd, "plain-file.txt")),
823+
);
824+
const failures = new Map([
825+
[path.join(target, "linked-broken"), "ENOENT"],
826+
[path.join(target, "linked-loop"), "ELOOP"],
827+
[path.join(target, "linked-denied"), "EACCES"],
828+
]);
829+
// Inject target states so this also runs on Windows without file-symlink privileges.
830+
using _stat = vi.spyOn(NodeFSP, "stat").mockImplementation((file, options) => {
831+
const code = failures.get(String(file));
832+
if (code) return Promise.reject(Object.assign(new Error(code), { code }));
833+
if (String(file) === path.join(target, "linked-file")) return Promise.resolve(fileStat);
834+
return originalStat(file, options);
835+
});
836+
const result = yield* workspaceEntries.browse({
837+
partialPath: yield* appendSeparator(cwd),
838+
});
839+
expect(result.entries).toEqual([
840+
{ name: "linked-good", fullPath: path.join(cwd, "linked-good") },
841+
{ name: "ordinary", fullPath: path.join(cwd, "ordinary") },
842+
]);
843+
}),
844+
);
845+
846+
it.effect("omits direct and chained network links without statting their targets", () =>
847+
Effect.gen(function* () {
848+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
849+
const path = yield* Path.Path;
850+
const fs = yield* FileSystem.FileSystem;
851+
const cwd = yield* fs.realPath(yield* makeTempDir({ prefix: "t3code-picker-network-" }));
852+
const target = yield* makeTempDir({ prefix: "t3code-picker-local-" });
853+
const direct = path.join(cwd, "network");
854+
const chained = path.join(cwd, "chained");
855+
const local = path.join(cwd, "local");
856+
for (const link of [direct, local]) {
857+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
858+
}
859+
yield* Effect.tryPromise(() => NodeFSP.symlink(direct, chained, "junction"));
860+
const originalReadlink = NodeFSP.readlink;
861+
const networkTarget = String.raw`\\host\share`;
862+
using _readlink = vi
863+
.spyOn(NodeFSP, "readlink")
864+
.mockImplementation((file, options) =>
865+
String(file) === direct
866+
? Promise.resolve(networkTarget)
867+
: originalReadlink(file, options),
868+
);
869+
using stat = vi.spyOn(NodeFSP, "stat");
870+
using lstat = vi.spyOn(NodeFSP, "lstat");
871+
const result = yield* workspaceEntries.browse({
872+
partialPath: yield* appendSeparator(cwd),
873+
});
874+
expect(result.entries).toEqual([{ name: "local", fullPath: local }]);
875+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(direct);
876+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(chained);
877+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget);
878+
expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget);
879+
}),
880+
);
881+
882+
it.effect(
883+
"rejects a network redirect in an intermediate target component before probing it",
884+
() =>
885+
Effect.gen(function* () {
886+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
887+
const path = yield* Path.Path;
888+
const fs = yield* FileSystem.FileSystem;
889+
const root = yield* fs.realPath(
890+
yield* makeTempDir({ prefix: "t3code-picker-component-" }),
891+
);
892+
const cwd = path.join(root, "projects");
893+
const target = path.join(root, "target");
894+
yield* fs.makeDirectory(cwd);
895+
yield* fs.makeDirectory(path.join(target, "project"), { recursive: true });
896+
const redirect = path.join(root, "redirect");
897+
const localRedirect = path.join(root, "local-redirect");
898+
for (const link of [redirect, localRedirect]) {
899+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
900+
}
901+
const blocked = path.join(cwd, "blocked");
902+
const local = path.join(cwd, "local");
903+
const unsafeTarget = path.join(redirect, "project");
904+
yield* Effect.tryPromise(() => NodeFSP.symlink(unsafeTarget, blocked, "junction"));
905+
yield* Effect.tryPromise(() =>
906+
NodeFSP.symlink(path.join(localRedirect, "project"), local, "junction"),
907+
);
908+
const originalReadlink = NodeFSP.readlink;
909+
const networkTarget = String.raw`\\host\share`;
910+
using _readlink = vi
911+
.spyOn(NodeFSP, "readlink")
912+
.mockImplementation((file, options) =>
913+
String(file) === redirect
914+
? Promise.resolve(networkTarget)
915+
: originalReadlink(file, options),
916+
);
917+
using stat = vi.spyOn(NodeFSP, "stat");
918+
using lstat = vi.spyOn(NodeFSP, "lstat");
919+
const result = yield* workspaceEntries.browse({
920+
partialPath: yield* appendSeparator(cwd),
921+
});
922+
expect(result.entries).toEqual([{ name: "local", fullPath: local }]);
923+
expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(unsafeTarget);
924+
expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget);
925+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(blocked);
926+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(local);
927+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(unsafeTarget);
928+
expect(stat.mock.calls.map(([file]) => String(file))).toContain(
929+
path.join(target, "project"),
930+
);
931+
}),
932+
);
933+
934+
it.effect(
935+
"reads a chosen link's resolved folder and skips one retargeted to a network share",
936+
() =>
937+
Effect.gen(function* () {
938+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
939+
const path = yield* Path.Path;
940+
const fs = yield* FileSystem.FileSystem;
941+
const root = yield* fs.realPath(yield* makeTempDir({ prefix: "t3code-picker-parent-" }));
942+
const target = path.join(root, "target");
943+
yield* fs.makeDirectory(path.join(target, "child"), { recursive: true });
944+
const local = path.join(root, "local");
945+
const network = path.join(root, "network");
946+
for (const link of [local, network]) {
947+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
948+
}
949+
const originalReadlink = NodeFSP.readlink;
950+
const networkTarget = String.raw`\\host\share`;
951+
using _readlink = vi
952+
.spyOn(NodeFSP, "readlink")
953+
.mockImplementation((file, options) =>
954+
String(file) === network
955+
? Promise.resolve(networkTarget)
956+
: originalReadlink(file, options),
957+
);
958+
using readdir = vi.spyOn(NodeFSP, "readdir");
959+
960+
const localResult = yield* workspaceEntries.browse({
961+
partialPath: yield* appendSeparator(local),
962+
});
963+
expect(localResult.entries).toEqual([
964+
{ name: "child", fullPath: path.join(local, "child") },
965+
]);
966+
const networkResult = yield* workspaceEntries.browse({
967+
partialPath: yield* appendSeparator(network),
968+
});
969+
expect(networkResult.entries).toEqual([]);
970+
const readPaths = readdir.mock.calls.map(([file]) => String(file));
971+
expect(readPaths).toContain(target);
972+
expect(readPaths).not.toContain(local);
973+
expect(readPaths).not.toContain(network);
974+
}),
975+
);
976+
977+
it.effect("resolves parent components after local links and bounds cyclic links", () =>
978+
Effect.gen(function* () {
979+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
980+
const path = yield* Path.Path;
981+
const fs = yield* FileSystem.FileSystem;
982+
const root = yield* fs.realPath(yield* makeTempDir({ prefix: "t3code-picker-relative-" }));
983+
const cwd = path.join(root, "projects");
984+
const target = path.join(root, "physical", "sibling");
985+
const via = path.join(root, "via");
986+
yield* fs.makeDirectory(cwd);
987+
yield* fs.makeDirectory(target, { recursive: true });
988+
yield* fs.makeDirectory(path.join(root, "physical", "child"));
989+
yield* Effect.tryPromise(() =>
990+
NodeFSP.symlink(path.join(root, "physical", "child"), via, "junction"),
991+
);
992+
const relative = path.join(cwd, "relative");
993+
const cycle = path.join(cwd, "cycle");
994+
for (const link of [relative, cycle]) {
995+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
996+
}
997+
const originalReadlink = NodeFSP.readlink;
998+
// Junction fixtures avoid Windows symlink privileges; inject relative and cyclic targets.
999+
using readlink = vi.spyOn(NodeFSP, "readlink").mockImplementation((file, options) => {
1000+
if (String(file) === relative)
1001+
return Promise.resolve(["..", "via", "..", "sibling"].join(path.sep));
1002+
if (String(file) === cycle) return Promise.resolve(cycle);
1003+
return originalReadlink(file, options);
1004+
});
1005+
using stat = vi.spyOn(NodeFSP, "stat");
1006+
const result = yield* workspaceEntries.browse({ partialPath: yield* appendSeparator(cwd) });
1007+
expect(result.entries).toEqual([{ name: "relative", fullPath: relative }]);
1008+
expect(stat.mock.calls.map(([file]) => String(file))).toContain(target);
1009+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(relative);
1010+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(cycle);
1011+
expect(readlink.mock.calls.filter(([file]) => String(file) === cycle)).toHaveLength(40);
1012+
}),
1013+
);
1014+
7501015
it.effect("shows dot directories in directory mode and hidden-prefix mode", () =>
7511016
Effect.gen(function* () {
7521017
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;

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

Lines changed: 67 additions & 15 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";
@@ -201,15 +201,58 @@ export const make = Effect.gen(function* () {
201201
},
202202
);
203203

204+
const resolveBrowseLink = Effect.fn("WorkspaceEntries.resolveBrowseLink")(function* (
205+
fullPath: string,
206+
) {
207+
if (isNetworkPath(fullPath)) return undefined;
208+
const separators = path.sep === "\\" ? /[\\/]+/ : /\/+/;
209+
let currentPath = path.parse(fullPath).root;
210+
const remaining = fullPath.slice(currentPath.length).split(separators);
211+
let hops = 0;
212+
while (remaining.length > 0) {
213+
const component = remaining.shift()!;
214+
if (component === "" || component === ".") continue;
215+
if (component === "..") {
216+
currentPath = path.dirname(currentPath);
217+
continue;
218+
}
219+
const nextPath = path.join(currentPath, component);
220+
const stats = yield* Effect.tryPromise(() => NodeFSP.lstat(nextPath));
221+
if (!stats.isSymbolicLink()) {
222+
if (remaining.length > 0 && !stats.isDirectory()) return undefined;
223+
currentPath = nextPath;
224+
continue;
225+
}
226+
if (++hops > 40) return undefined;
227+
const target = yield* Effect.tryPromise(() => NodeFSP.readlink(nextPath));
228+
// Inspect every link before following it: UNC paths can trigger Windows NTLM sign-in.
229+
if (isNetworkPath(target)) return undefined;
230+
const root = path.parse(target).root;
231+
if (root) {
232+
if (!path.isAbsolute(target)) return undefined;
233+
currentPath = path.parse(path.resolve(currentPath, root)).root;
234+
}
235+
// Expand before processing '..' so it applies to the resolved directory.
236+
remaining.unshift(...target.slice(root.length).split(separators));
237+
}
238+
return currentPath;
239+
});
240+
204241
const browse: WorkspaceEntries["Service"]["browse"] = Effect.fn("WorkspaceEntries.browse")(
205242
function* (input) {
206243
const resolvedInputPath = yield* resolveBrowseTarget(input, path);
207244
const endsWithSeparator = /[\\/]$/.test(input.partialPath) || input.partialPath === "~";
208245
const parentPath = endsWithSeparator ? resolvedInputPath : path.dirname(resolvedInputPath);
209246
const prefix = endsWithSeparator ? "" : path.basename(resolvedInputPath);
247+
// Choosing a listed link sends it back as the parent, so resolve it before
248+
// readdir follows it; a typed network path is still read as before.
249+
const readPath = isNetworkPath(parentPath)
250+
? parentPath
251+
: yield* resolveBrowseLink(parentPath).pipe(Effect.orElseSucceed(() => parentPath));
252+
if (readPath === undefined) return { parentPath, entries: [] };
210253

211254
const dirents = yield* Effect.tryPromise({
212-
try: () => NodeFSP.readdir(parentPath, { withFileTypes: true }),
255+
try: () => NodeFSP.readdir(readPath, { withFileTypes: true }),
213256
catch: (cause) =>
214257
new WorkspaceEntriesReadDirectoryError({
215258
cwd: input.cwd,
@@ -229,23 +272,32 @@ export const make = Effect.gen(function* () {
229272

230273
const showHidden = endsWithSeparator || prefix.startsWith(".");
231274
const lowerPrefix = prefix.toLowerCase();
232-
const entries: Array<{ readonly name: string; readonly fullPath: string }> = [];
233-
for (const dirent of dirents) {
234-
if (
235-
dirent.isDirectory() &&
275+
const candidates = dirents.filter(
276+
(dirent) =>
277+
(dirent.isDirectory() || dirent.isSymbolicLink()) &&
236278
dirent.name.toLowerCase().startsWith(lowerPrefix) &&
237-
(showHidden || !dirent.name.startsWith("."))
238-
) {
239-
entries.push({
240-
name: dirent.name,
241-
fullPath: path.join(parentPath, dirent.name),
242-
});
243-
}
244-
}
279+
(showHidden || !dirent.name.startsWith(".")),
280+
);
281+
const entries = yield* Effect.forEach(
282+
candidates,
283+
(dirent) => {
284+
const entry = { name: dirent.name, fullPath: path.join(parentPath, dirent.name) };
285+
if (dirent.isDirectory()) return Effect.succeed(entry);
286+
return Effect.gen(function* () {
287+
const target = yield* resolveBrowseLink(path.join(readPath, dirent.name));
288+
if (target === undefined) return undefined;
289+
const stats = yield* Effect.tryPromise(() => NodeFSP.stat(target));
290+
return stats.isDirectory() ? entry : undefined;
291+
}).pipe(Effect.orElseSucceed(() => undefined));
292+
},
293+
{ concurrency: SYMLINK_RESOLVE_BATCH_SIZE },
294+
);
245295

246296
return {
247297
parentPath,
248-
entries: entries.toSorted((left, right) => left.name.localeCompare(right.name)),
298+
entries: entries
299+
.filter((entry) => entry !== undefined)
300+
.toSorted((left, right) => left.name.localeCompare(right.name)),
249301
};
250302
},
251303
);

0 commit comments

Comments
 (0)