Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/server/src/workspace/Layers/WorkspaceEntries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,27 @@ it.layer(TestLayer)("WorkspaceEntriesLive", (it) => {
}),
);

it.effect("includes directory symlinks when browsing", () =>
Effect.gen(function* () {
const workspaceEntries = yield* WorkspaceEntries;
const path = yield* Path.Path;
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-browse-symlink-" });
const target = path.join(cwd, "target");
const link = path.join(cwd, "linked");
yield* writeTextFile(cwd, "target/index.ts", "export {};\n");
yield* Effect.promise(() =>
fsPromises.symlink(target, link, process.platform === "win32" ? "junction" : "dir"),
);

const result = yield* workspaceEntries.browse({
partialPath: appendSeparator(cwd),
});

expect(result.entries).toContainEqual({ name: "linked", fullPath: link });
expect(result.entries).toContainEqual({ name: "target", fullPath: target });
}),
);

it.effect("supports relative paths when cwd is provided", () =>
Effect.gen(function* () {
const workspaceEntries = yield* WorkspaceEntries;
Expand Down
69 changes: 57 additions & 12 deletions apps/server/src/workspace/Layers/WorkspaceEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";

import { type FilesystemBrowseInput, type ProjectEntry } from "@t3tools/contracts";
import {
type FilesystemBrowseEntry,
type FilesystemBrowseInput,
type ProjectEntry,
} from "@t3tools/contracts";
import { isExplicitRelativePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
import {
insertRankedSearchResult,
Expand Down Expand Up @@ -44,6 +48,18 @@ const IGNORED_DIRECTORY_NAMES = new Set([
"out",
".cache",
]);
const WINDOWS_LEGACY_PROFILE_JUNCTION_NAMES = new Set([
"Application Data",
"Cookies",
"Local Settings",
"My Documents",
"NetHood",
"PrintHood",
"Recent",
"SendTo",
"Start Menu",
"Templates",
]);

interface WorkspaceIndex {
scannedAt: number;
Expand All @@ -62,6 +78,25 @@ function toPosixPath(input: string): string {
return input.replaceAll("\\", "/");
}

async function isDirectoryEntry(dirent: Dirent, fullPath: string): Promise<boolean> {
if (dirent.isDirectory()) {
return true;
}
if (!dirent.isSymbolicLink()) {
return false;
}
if (process.platform === "win32" && WINDOWS_LEGACY_PROFILE_JUNCTION_NAMES.has(dirent.name)) {
return false;
}

try {
const stat = await fsPromises.stat(fullPath);
return stat.isDirectory();
} catch {
return false;
}
}

function expandHomePath(input: string, path: Path.Path): string {
if (input === "~") {
return OS.homedir();
Expand Down Expand Up @@ -456,20 +491,30 @@ export const makeWorkspaceEntries = Effect.gen(function* () {

const showHidden = endsWithSeparator || prefix.startsWith(".");
const lowerPrefix = prefix.toLowerCase();
const directoryEntries = yield* Effect.forEach(
dirents,
(dirent) =>
Effect.promise(async (): Promise<FilesystemBrowseEntry | null> => {
const fullPath = path.join(parentPath, dirent.name);
if (
!dirent.name.toLowerCase().startsWith(lowerPrefix) ||
(!showHidden && dirent.name.startsWith(".")) ||
!(await isDirectoryEntry(dirent, fullPath))
) {
return null;
}
return {
name: dirent.name,
fullPath,
};
}),
{ concurrency: 16 },
);

return {
parentPath,
entries: dirents
.filter(
(dirent) =>
dirent.isDirectory() &&
dirent.name.toLowerCase().startsWith(lowerPrefix) &&
(showHidden || !dirent.name.startsWith(".")),
)
.map((dirent) => ({
name: dirent.name,
fullPath: path.join(parentPath, dirent.name),
}))
entries: directoryEntries
.filter((entry): entry is FilesystemBrowseEntry => entry !== null)
.toSorted((left, right) => left.name.localeCompare(right.name)),
};
},
Expand Down
Loading