Skip to content

fix: recover routes after watchpack eperm - #100359

Open
asxxb wants to merge 5 commits into
vercel:canaryfrom
asxxb:fix/watchpack-eperm-initial-scan
Open

asxxb wants to merge 5 commits into
vercel:canaryfrom
asxxb:fix/watchpack-eperm-initial-scan

Conversation

@asxxb

@asxxb asxxb commented Oct 17, 2026 •

Copy link
Copy Markdown

What?

Fixes Next.js development route discovery when Watchpack encounters EPERM during its initial directory scan.

When the initial scan fails for a route directory, Watchpack can treat the directory as removed, omit valid page files from its known files, and fail to emit a subsequent aggregation. This can leave the development server waiting for the initial scan deadline and affected routes unavailable.

Fixes #100268

Why?

The existing initial-scan protection prevents Next.js from publishing an incomplete route graph while Watchpack is performing its initial scan.

However, when Watchpack encounters EPERM, it may not emit another aggregation to reevaluate the initial-scan guard. This can delay route discovery until the fallback deadline.

The fix preserves the existing startup-race protection while allowing valid page files from the initial filesystem snapshot to be recovered when Watchpack fails to report them.

How?

  • Preserve the initial-scan guard and its 30-second deadline.
  • Include the initial page-file snapshot in Watchpack's watched files.
  • Recover existing snapshot page files that are missing from Watchpack's known files.
  • Serialize aggregation processing to prevent concurrent passes from modifying shared route state.
  • Coalesce aggregation events received while a processing pass is running.
  • Handle fallback timer cleanup and aggregation errors consistently.
  • Add an end-to-end regression test that injects an initial EPERM during a nested Pages Router directory scan and verifies route availability across subsequent aggregations.

The recovery is limited to valid page files captured in the initial filesystem snapshot. It does not attempt to recreate Watchpack's failed directory watcher.

Testing

Build and static checks

  • pnpm --filter=next build
  • pnpm --filter=next typescript
  • pnpm types:test-lib
  • pnpm --filter=next types
  • pnpm lint-ast-grep
  • ESLint on modified files
  • Prettier check
  • git diff --check

End-to-end tests

  • Targeted Watchpack EPERM and route-discovery tests: Webpack 2/2 passed; Turbopack 2/2 passed.
  • Full next/head development route-discovery test file: Webpack 7/7 passed; Turbopack 7/7 passed.
  • next/head production/start tests: Turbopack 5/5 passed.

A repeated test run encountered an intermittent Invalid hook call / useContext failure involving styled-jsx and react-dom-server.edge in an unrelated test. The Watchpack EPERM regression test passed where reached.

git diff --check completed successfully.

Scope and limitation

This change restores routes from the pre-watch filesystem snapshot and ensures they remain available across subsequent aggregations. It does not guarantee that Watchpack resumes detecting future file changes inside a directory whose initial scan failed with EPERM.

@asxxb
asxxb force-pushed the fix/watchpack-eperm-initial-scan branch from 56540e3 to 56b79ae Compare October 17, 2026 19:21
@github-actions

Copy link
Copy Markdown
Contributor

Failing CI jobs

Commit: 56b79ae | About building and testing Next.js

@farisbasha

Copy link
Copy Markdown

@asxxb LGTM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next dev: EPERM during Watchpack's initial scan silently drops a route directory (hangs, then 404 until restart)

2 participants