Skip to content
Merged
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
179 changes: 179 additions & 0 deletions apps/desktop/src/lib/trpc/routers/filesystem/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,147 @@
import fs from "node:fs/promises";
import path from "node:path";
import { shell } from "electron";
import fg from "fast-glob";
import Fuse from "fuse.js";
import type { DirectoryEntry } from "shared/file-tree-types";
import { z } from "zod";
import { publicProcedure, router } from "../..";

const SEARCH_INDEX_TTL_MS = 30_000;
const MAX_SEARCH_RESULTS = 500;
const DEFAULT_IGNORE_PATTERNS = [
"**/node_modules/**",
"**/.git/**",
"**/dist/**",
"**/build/**",
"**/.next/**",
"**/.turbo/**",
"**/coverage/**",
];

interface FileSearchItem {
id: string;
name: string;
relativePath: string;
path: string;
isDirectory: boolean;
}

interface FileSearchIndex {
items: FileSearchItem[];
fuse: Fuse<FileSearchItem>;
}

interface FileSearchCacheEntry {
index: FileSearchIndex;
builtAt: number;
}

const searchIndexCache = new Map<string, FileSearchCacheEntry>();
const searchIndexBuilds = new Map<string, Promise<FileSearchIndex>>();

function getSearchCacheKey({
rootPath,
includeHidden,
}: {
rootPath: string;
includeHidden: boolean;
}) {
return `${rootPath}::${includeHidden ? "hidden" : "visible"}`;
}

async function buildSearchIndex({
rootPath,
includeHidden,
}: {
rootPath: string;
includeHidden: boolean;
}): Promise<FileSearchIndex> {
const entries = await fg("**/*", {
cwd: rootPath,
onlyFiles: true,
dot: includeHidden,
followSymbolicLinks: false,
unique: true,
suppressErrors: true,
ignore: DEFAULT_IGNORE_PATTERNS,
});
Comment on lines +60 to +68
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

cat -n apps/desktop/src/lib/trpc/routers/filesystem/index.ts | head -100

Repository: superset-sh/superset

Length of output: 2915


🏁 Script executed:

wc -l apps/desktop/src/lib/trpc/routers/filesystem/index.ts

Repository: superset-sh/superset

Length of output: 121


🏁 Script executed:

# Get context around lines 60-68
sed -n '40,90p' apps/desktop/src/lib/trpc/routers/filesystem/index.ts

Repository: superset-sh/superset

Length of output: 1134


🏁 Script executed:

# Check what function contains this code and its error handling
sed -n '1,150p' apps/desktop/src/lib/trpc/routers/filesystem/index.ts | cat -n

Repository: superset-sh/superset

Length of output: 4435


🏁 Script executed:

sed -n '90,250p' apps/desktop/src/lib/trpc/routers/filesystem/index.ts | grep -A 20 -B 5 "searchFiles\|catch"

Repository: superset-sh/superset

Length of output: 2913


🏁 Script executed:

# Check if there's logging around these procedures
rg -A 10 "searchFiles|buildSearchIndex" apps/desktop/src/lib/trpc/routers/filesystem/index.ts | head -60

Repository: superset-sh/superset

Length of output: 1496


Remove suppressErrors: true to surface glob errors.

suppressErrors: true silently skips permission and IO errors during file globbing, resulting in incomplete search indexes with no indication of failure. Even though searchFiles has error handling, it won't catch suppressed errors since they're never thrown. Remove this option so errors propagate to the existing error logging path.

Suggested fix
 	const entries = await fg("**/*", {
 		cwd: rootPath,
 		onlyFiles: true,
 		dot: includeHidden,
 		followSymbolicLinks: false,
 		unique: true,
-		suppressErrors: true,
 		ignore: DEFAULT_IGNORE_PATTERNS,
 	});

Per coding guidelines: Never swallow errors silently; at minimum log them with context.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const entries = await fg("**/*", {
cwd: rootPath,
onlyFiles: true,
dot: includeHidden,
followSymbolicLinks: false,
unique: true,
suppressErrors: true,
ignore: DEFAULT_IGNORE_PATTERNS,
});
const entries = await fg("**/*", {
cwd: rootPath,
onlyFiles: true,
dot: includeHidden,
followSymbolicLinks: false,
unique: true,
ignore: DEFAULT_IGNORE_PATTERNS,
});
🤖 Prompt for AI Agents
In `@apps/desktop/src/lib/trpc/routers/filesystem/index.ts` around lines 60 - 68,
The glob call that builds `entries` currently sets `suppressErrors: true`, which
swallows permission/IO errors; remove the `suppressErrors: true` option from the
fg(...) call in the filesystem router so globbing errors are allowed to throw
and propagate to the existing `searchFiles` error handling/logging path (locate
the fg invocation that assigns `entries` and delete the `suppressErrors`
property).


const items = entries.map((relativePath) => ({
id: relativePath,
name: path.basename(relativePath),
relativePath,
path: path.join(rootPath, relativePath),
isDirectory: false,
}));

const fuse = new Fuse(items, {
keys: [
{ name: "name", weight: 2 },
{ name: "relativePath", weight: 1 },
],
threshold: 0.4,
includeScore: true,
ignoreLocation: true,
});

return { items, fuse };
}

async function getSearchIndex({
rootPath,
includeHidden,
}: {
rootPath: string;
includeHidden: boolean;
}): Promise<FileSearchIndex> {
const cacheKey = getSearchCacheKey({ rootPath, includeHidden });
const cached = searchIndexCache.get(cacheKey);
const now = Date.now();
const inFlight = searchIndexBuilds.get(cacheKey);

if (cached && now - cached.builtAt < SEARCH_INDEX_TTL_MS) {
return cached.index;
}

if (cached && !inFlight) {
const buildPromise = buildSearchIndex({ rootPath, includeHidden })
.then((index) => {
searchIndexCache.set(cacheKey, { index, builtAt: Date.now() });
searchIndexBuilds.delete(cacheKey);
return index;
})
.catch((error) => {
searchIndexBuilds.delete(cacheKey);
throw error;
});
searchIndexBuilds.set(cacheKey, buildPromise);
return cached.index;
}

if (cached) {
return cached.index;
}

if (inFlight) {
return await inFlight;
}

const buildPromise = buildSearchIndex({ rootPath, includeHidden })
.then((index) => {
searchIndexCache.set(cacheKey, { index, builtAt: Date.now() });
searchIndexBuilds.delete(cacheKey);
return index;
})
.catch((error) => {
searchIndexBuilds.delete(cacheKey);
throw error;
});
searchIndexBuilds.set(cacheKey, buildPromise);

return await buildPromise;
}

export const createFilesystemRouter = () => {
return router({
readDirectory: publicProcedure
Expand Down Expand Up @@ -49,6 +186,48 @@ export const createFilesystemRouter = () => {
}
}),

searchFiles: publicProcedure
.input(
z.object({
rootPath: z.string(),
query: z.string(),
includeHidden: z.boolean().default(false),
limit: z.number().default(200),
}),
)
.query(async ({ input }) => {
const { rootPath, query, includeHidden, limit } = input;
const trimmedQuery = query.trim();

if (!trimmedQuery) {
return [];
}

try {
const index = await getSearchIndex({ rootPath, includeHidden });
const safeLimit = Math.max(1, Math.min(limit, MAX_SEARCH_RESULTS));
const results = index.fuse.search(trimmedQuery, {
limit: safeLimit,
});

return results.map((result) => ({
id: result.item.id,
name: result.item.name,
relativePath: result.item.relativePath,
path: result.item.path,
isDirectory: false,
score: 1 - (result.score ?? 0),
}));
} catch (error) {
console.error("[filesystem/searchFiles] Failed:", {
rootPath,
query,
error,
});
return [];
}
}),

createFile: publicProcedure
.input(
z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ export function ChangesView({ onFileOpen, isExpandedView }: ChangesViewProps) {
new Set(),
);

// Reset expanded commits when workspace changes to avoid querying
// old commit hashes against the new worktree
// biome-ignore lint/correctness/useExhaustiveDependencies: intentionally resets on worktreePath change
// biome-ignore lint/correctness/useExhaustiveDependencies: reset on workspace change
useEffect(() => {
setExpandedCommits(new Set());
}, [worktreePath]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import type {
} from "shared/file-tree-types";
import useResizeObserver from "use-resize-observer";
import { DeleteConfirmDialog } from "./components/DeleteConfirmDialog";
import { FileSearchResultNode } from "./components/FileSearchResultNode";
import { FileTreeContextMenu } from "./components/FileTreeContextMenu";
import { FileTreeNode } from "./components/FileTreeNode";
import { FileTreeToolbar } from "./components/FileTreeToolbar";
import { NewItemInput } from "./components/NewItemInput";
import { OVERSCAN_COUNT, ROW_HEIGHT, TREE_INDENT } from "./constants";
import {
OVERSCAN_COUNT,
ROW_HEIGHT,
SEARCH_RESULT_ROW_HEIGHT,
TREE_INDENT,
} from "./constants";
import { useFileSearch } from "./hooks/useFileSearch";
import { useFileTreeActions } from "./hooks/useFileTreeActions";
import type { NewItemMode } from "./types";

Expand All @@ -31,6 +38,7 @@ export function FilesView() {
const { ref: containerRef, height: treeHeight = 400 } = useResizeObserver();

const {
expandedFolders,
searchTerm,
showHiddenFiles,
toggleFolder,
Expand All @@ -41,6 +49,10 @@ export function FilesView() {
} = useFileExplorerStore();

const currentSearchTerm = worktreePath ? searchTerm[worktreePath] || "" : "";
const currentExpandedFolders = useMemo(
() => new Set(worktreePath ? expandedFolders[worktreePath] || [] : []),
[worktreePath, expandedFolders],
);

const [childrenCache, setChildrenCache] = useState<
Record<string, DirectoryEntry[]>
Expand Down Expand Up @@ -71,8 +83,10 @@ export function FilesView() {
return { ...entry, children: undefined };
}

const isExpanded = currentExpandedFolders.has(entry.id);
const cachedChildren = childrenCache[entry.path];
if (cachedChildren) {

if (isExpanded && cachedChildren) {
return {
...entry,
children: entriesToNodes(cachedChildren),
Expand All @@ -82,7 +96,7 @@ export function FilesView() {
return { ...entry, children: null };
});
},
[childrenCache],
[childrenCache, currentExpandedFolders],
);

const treeData = useMemo((): FileTreeNodeType[] => {
Expand Down Expand Up @@ -129,7 +143,7 @@ export function FilesView() {
[worktreePath, childrenCache, loadingFolders, showHiddenFiles, trpcUtils],
);

// biome-ignore lint/correctness/useExhaustiveDependencies: intentionally reset on these changes
// biome-ignore lint/correctness/useExhaustiveDependencies: reset cache on workspace/visibility change
useEffect(() => {
setChildrenCache({});
}, [worktreePath, showHiddenFiles]);
Expand All @@ -140,6 +154,17 @@ export function FilesView() {
onRefresh: () => refetch(),
});

const {
searchResults,
isFetching: isSearchFetching,
hasQuery,
} = useFileSearch({
worktreePath,
searchTerm: currentSearchTerm,
includeHidden: showHiddenFiles,
});
const isSearching = hasQuery;

const addFileViewerPane = useTabsStore((s) => s.addFileViewerPane);

const [newItemMode, setNewItemMode] = useState<NewItemMode>(null);
Expand Down Expand Up @@ -186,12 +211,12 @@ export function FilesView() {

const handleRename = useCallback(
({ id, name }: { id: string; name: string }) => {
const node = treeData.find((n) => n.id === id);
const node = treeRef.current?.get(id)?.data;
if (node) {
rename(node.path, name);
}
},
[treeData, rename],
[rename],
);

const handleNewFile = useCallback((parentPath: string) => {
Expand Down Expand Up @@ -320,30 +345,54 @@ export function FilesView() {
/>
)}

<Tree<FileTreeNodeType>
ref={treeRef}
data={treeData}
width="100%"
height={treeHeight}
rowHeight={ROW_HEIGHT}
indent={TREE_INDENT}
overscanCount={OVERSCAN_COUNT}
idAccessor="id"
childrenAccessor="children"
openByDefault={false}
disableMultiSelection={false}
searchTerm={currentSearchTerm}
searchMatch={(node, term) =>
node.data.name.toLowerCase().includes(term.toLowerCase())
}
onActivate={handleActivate}
onSelect={handleSelect}
onToggle={handleToggle}
onRename={handleRename}
dndManager={dragDropManager}
>
{FileTreeNode}
</Tree>
{isSearching ? (
searchResults.length > 0 ? (
<Tree<FileTreeNodeType>
ref={treeRef}
data={searchResults}
width="100%"
height={treeHeight}
rowHeight={SEARCH_RESULT_ROW_HEIGHT}
indent={0}
overscanCount={OVERSCAN_COUNT}
idAccessor="id"
childrenAccessor="children"
openByDefault={false}
disableMultiSelection={false}
onActivate={handleActivate}
onSelect={handleSelect}
onRename={handleRename}
dndManager={dragDropManager}
>
{FileSearchResultNode}
</Tree>
) : (
<div className="flex-1 flex items-center justify-center text-muted-foreground text-sm p-4">
{isSearchFetching ? "Searching files..." : "No matching files"}
</div>
)
) : (
<Tree<FileTreeNodeType>
ref={treeRef}
data={treeData}
width="100%"
height={treeHeight}
rowHeight={ROW_HEIGHT}
indent={TREE_INDENT}
overscanCount={OVERSCAN_COUNT}
idAccessor="id"
childrenAccessor="children"
openByDefault={false}
disableMultiSelection={false}
onActivate={handleActivate}
onSelect={handleSelect}
onToggle={handleToggle}
onRename={handleRename}
dndManager={dragDropManager}
>
{FileTreeNode}
</Tree>
)}
</div>
</FileTreeContextMenu>

Expand Down
Loading
Loading