Skip to content

fix(preview): stop flattenTree overflowing the stack on a huge directory - #6439

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/flatten-tree-stack-overflow-w4
Aug 24, 2026
Merged

fix(preview): stop flattenTree overflowing the stack on a huge directory#6439
pedrofrxncx merged 1 commit into
mainfrom
fix/flatten-tree-stack-overflow-w4

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

The bug

flattenTree (the file-explorer's tree→row flattener, apps/web/src/components/sandbox/preview/file-explorer/utils.ts) recursed into an expanded directory's children and merged the result back with rows.push(...flattenTree(...)). Spreading an array into a call's argument list is bounded by the JS engine's max-arguments limit, well below what a legitimate sandbox directory can hold (build output, generated assets, a large node_modules/.bin-style dump, etc.) — once a single expanded directory has enough direct children (~700k in this engine), the spread throws RangeError: Maximum call stack size exceeded and the whole file explorer crashes instead of rendering a (possibly slow but working) tree.

The fix

Replaced the spread-push with a plain for...of loop appending one row at a time — same output, no argument-list ceiling. Net change: +8/-1 in utils.ts.

Failure scenario + regression test

Expanding a directory with a very large number of direct children (build output, a huge asset dump, etc.) crashed the whole panel. Added a test in utils.test.ts building a 700,000-child directory and asserting flattenTree returns all rows instead of throwing — this reproducibly threw the RangeError before the fix (confirmed by hand against the old spread-based implementation) and passes after it.

How a reviewer confirms

bun test apps/web/src/components/sandbox/preview/file-explorer/utils.test.ts — 33 pass / 0 fail.

Checks run locally

  • bun run fmt — clean
  • cd apps/web && bunx tsc --noEmit — clean
  • bun test apps/web/src/components/sandbox/preview/file-explorer/utils.test.ts — 33 pass / 0 fail
  • bunx oxlint on both changed files — 0 warnings / 0 errors

Full CI validates the rest.


Summary by cubic

Prevents the file explorer from crashing when expanding a directory with a very large number of children. Previously flattenTree used rows.push(...flattenTree(...)), which hit the engine’s max-arguments limit and threw; now it appends rows in a loop and renders the full tree.

  • Change is limited to flattenTree: replace spread-push with a for...of loop. Output is identical; no API changes.
  • Adds a regression test that builds a directory with 700,000 children and asserts flattenTree returns all rows without throwing.

Written for commit c94cb0b. Summary will update on new commits.

Review in cubic

@pedrofrxncx
pedrofrxncx merged commit 8ec1feb into main Aug 24, 2026
34 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/flatten-tree-stack-overflow-w4 branch August 24, 2026 16:03
decocms Bot pushed a commit that referenced this pull request Aug 24, 2026
PR: #6439 fix(preview): stop flattenTree overflowing the stack on a huge directory
Bump type: patch

- decocms (apps/api/package.json): 4.260.7 -> 4.260.8
- @decocms/native (apps/native/package.json): 4.260.7 -> 4.260.8

Deploy-Scope: web
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant