Skip to content
TypeScript

fix(web): expanding all folders in Files no longer walks node_modules (#21258)

tiliakoos64a76c84ef79
diff --git a/apps/web/src/components/files/FileBrowserPanel.browser.test.tsx b/apps/web/src/components/files/FileBrowserPanel.browser.test.tsx
index a09803e9f2f..118d1cf0321 100644
--- a/apps/web/src/components/files/FileBrowserPanel.browser.test.tsx
+++ b/apps/web/src/components/files/FileBrowserPanel.browser.test.tsx
@@ -15,7 +15,7 @@ const files = vi.hoisted(() => ({
searchPending: false,
searchError: null as string | null,
loadingDirectories: new Set<string>() as ReadonlySet<string>,
- load: vi.fn(async () => {}),
+ load: vi.fn(async (_directoryPath: string) => {}),
refresh: vi.fn(),
}));
@@ -52,7 +52,7 @@ vi.mock("~/components/ui/tooltip", () => ({
TooltipPopup: () => null,
}));
-describe("Files panel search", () => {
+describe("Files panel", () => {
let root: Root | undefined;
let container: HTMLDivElement | undefined;
const props = {
@@ -243,4 +243,89 @@ describe("Files panel search", () => {
expect(hasMessage("No matching files.")).toBe(false);
expect(container!.textContent).toContain("Workspace search failed.");
});
+
+ describe("expand all", () => {
+ const workspace: Record<string, ProjectEntry[]> = {
+ src: [
+ { path: "src/lib", kind: "directory" },
+ { path: "src/index.ts", kind: "file" },
+ ],
+ "src/lib": [{ path: "src/lib/util.ts", kind: "file" }],
+ node_modules: [
+ { path: "node_modules/react", kind: "directory", ignored: true },
+ { path: "node_modules/.modules.yaml", kind: "file", ignored: true },
+ ],
+ "node_modules/react": [{ path: "node_modules/react/index.js", kind: "file", ignored: true }],
+ };
+ const listed = () => files.load.mock.calls.map(([path]) => path);
+
+ // Answer each listing the panel asks for, as the server would, until it asks for no more.
+ async function answerListings() {
+ const answered = new Set<string>();
+ for (;;) {
+ const path = listed().find((requested) => !answered.has(requested));
+ if (path === undefined) return;
+ answered.add(path);
+ files.directoryEntries = [...files.directoryEntries, ...(workspace[path] ?? [])];
+ await render();
+ }
+ }
+
+ function button(label: "Expand all folders" | "Collapse all folders") {
+ const match = container!.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`);
+ expect(match, label).not.toBeNull();
+ return match!;
+ }
+
+ async function clickRow(path: string) {
+ const row = container!
+ .querySelector("file-tree-container")!
+ .shadowRoot!.querySelector<HTMLElement>(`[data-item-path="${path}"]`);
+ expect(row, path).not.toBeNull();
+ await act(async () => row!.click());
+ await answerListings();
+ }
+
+ beforeEach(() => {
+ files.load.mockClear();
+ files.directoryEntries = [
+ { path: "src", kind: "directory" },
+ { path: "node_modules", kind: "directory", ignored: true },
+ { path: "README.md", kind: "file" },
+ ];
+ });
+
+ it("opens every folder except ignored ones, which it never lists", async () => {
+ await render();
+ await act(async () => button("Expand all folders").click());
+ await answerListings();
+
+ expect(listed().toSorted()).toEqual(["src", "src/lib"]);
+ expect(visiblePaths()).toContain("src/lib/util.ts");
+ expect(visiblePaths()).not.toContain("node_modules/.modules.yaml");
+ button("Collapse all folders");
+
+ // Collapsing a folder by hand ends expand all; reopening it counts as all expanded again,
+ // with the ignored folder still closed.
+ await clickRow("src/lib/");
+ button("Expand all folders");
+ await clickRow("src/lib/");
+ button("Collapse all folders");
+ });
+
+ it("opens an ignored folder by hand without walking into it, and collapses it with the rest", async () => {
+ await render();
+ await act(async () => button("Expand all folders").click());
+ await answerListings();
+
+ await clickRow("node_modules/");
+ expect(listed()).toContain("node_modules");
+ expect(listed()).not.toContain("node_modules/react");
+ expect(visiblePaths()).toContain("node_modules/react/");
+ expect(visiblePaths()).not.toContain("node_modules/react/index.js");
+
+ await act(async () => button("Collapse all folders").click());
+ expect(visiblePaths()).toEqual(["node_modules/", "src/", "README.md"]);
+ });
+ });
});
diff --git a/apps/web/src/components/files/FileBrowserPanel.tsx b/apps/web/src/components/files/FileBrowserPanel.tsx
index 7b8825d8e08..94b36844614 100644
--- a/apps/web/src/components/files/FileBrowserPanel.tsx
+++ b/apps/web/src/components/files/FileBrowserPanel.tsx
@@ -130,8 +130,12 @@ function buildTreeEntries(
entries.map((entry) => [entry.path, entry.kind] as const),
);
const treePaths = entries.map(treePath);
- const directoryPaths = entries.filter((entry) => entry.kind === "directory").map(treePath);
- return { entries, entryKinds, treePaths, directoryPaths };
+ const directories = entries.filter((entry) => entry.kind === "directory");
+ const directoryPaths = directories.map(treePath);
+ // Expanding a folder lists it, and an ignored one such as `node_modules` can hold tens of
+ // thousands, so expand all skips ignored folders. They still open by hand.
+ const expandableDirectoryPaths = directories.filter((entry) => !entry.ignored).map(treePath);
+ return { entries, entryKinds, treePaths, directoryPaths, expandableDirectoryPaths };
}
export default function FileBrowserPanel({
@@ -165,12 +169,8 @@ export default function FileBrowserPanel({
{ environmentId, cwd: hostFolder ? null : cwd, query: query.slice(0, 256) },
200,
);
- const { entries, entryKinds, treePaths, directoryPaths } = buildTreeEntries(
- directoryEntries,
- pathSearch.entries,
- pathSearch.isPending,
- query,
- );
+ const { entries, entryKinds, treePaths, directoryPaths, expandableDirectoryPaths } =
+ buildTreeEntries(directoryEntries, pathSearch.entries, pathSearch.isPending, query);
const entryKindsRef = useRef(entryKinds);
const previousTreePathsRef = useRef<readonly string[] | null>(null);
const syncingSelectionRef = useRef(false);
@@ -321,12 +321,16 @@ export default function FileBrowserPanel({
// Pierre restores the unfiltered tree when a search has no matches.
const hasNoSearchMatches = search.value.trim().length > 0 && search.matchingPaths.length === 0;
const allDirectoriesExpanded = useFileTreeSelector(model, (currentModel) =>
- areAllDirectoriesExpanded(currentModel, directoryPaths),
+ areAllDirectoriesExpanded(currentModel, expandableDirectoryPaths),
);
const toggleAllDirectories = () => {
const expanded = !(expandAll || allDirectoriesExpanded);
setExpandAll(expanded);
- setAllDirectoriesExpanded(model, directoryPaths, expanded);
+ setAllDirectoriesExpanded(
+ model,
+ expanded ? expandableDirectoryPaths : directoryPaths,
+ expanded,
+ );
};
const closeSearch = () => {
setQuery("");
@@ -411,8 +415,9 @@ export default function FileBrowserPanel({
}, [ready, entryKinds, model, treePaths]);
useEffect(() => {
- if (expandAll && !query.trim()) setAllDirectoriesExpanded(model, directoryPaths, true);
- }, [directoryPaths, expandAll, model, query]);
+ if (expandAll && !query.trim())
+ setAllDirectoriesExpanded(model, expandableDirectoryPaths, true);
+ }, [expandableDirectoryPaths, expandAll, model, query]);
useEffect(() => {
if (!selectedPath) {