Overly broad patterns during build exceed actual file count
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.4k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
Link to the code that reproduces this issue
https://github.com/remcohaszing/next-issue-broad-patterns
To Reproduce
Setup:
git clone https://github.com/remcohaszing/next-issue-broad-patterns.git
cd next-issue-broad-patterns
npm install
Generate 1000 files to import dynamically:
node script.ts
Build the Next.js project:
$ npx next build
▲ Next.js 16.2.2 (Turbopack)
Creating an optimized production build ...
Turbopack build encountered 1 warnings:
./app/page.tsx:14:39
The file pattern ('posts/' <dynamic> '/' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts/' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts/' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> | 'posts/' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> '/' <dynamic> | 'posts/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> | 'posts/' <dynamic> '/' <dynamic> | 'posts' <dynamic> '/' <dynamic> | 'posts/' <dynamic> | 'posts' <dynamic>) matches 16000 files in [project]/
12 | for(const d of await readdir(cDir)) {
13 | // results.push(await readFile(`posts/${a}/${b}/${c}/${d}`, 'utf8'))
> 14 | results.push(await readFile(join('posts', a, b, c, d), 'utf8'))
| ^^^^^^^^^^^^^^^^^^^^^^^^^
15 | }
16 | }
17 | }
Overly broad patterns can lead to build performance issues and over bundling.
✓ Compiled successfully in 3.2s
✓ Finished TypeScript in 1576ms
✓ Collecting page data using 4 workers in 335ms
✓ Generating static pages using 4 workers (3/3) in 471ms
✓ Finalizing page optimization in 9ms
Route (app)
┌ ○ /
└ ○ /_not-found
○ (Static) prerendered as static content
Current vs. Expected behavior
This logs that 16000 files are matched from page.tsx. In reality there are only 1000 that match the pattern.
Commenting line 14 and uncommenting line 13, works around the issue.
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #202601231045~1769703228~24.04~cb87b5b SMP PREEMPT_DYNAMIC Thu J
Available memory (MB): 31804
Available CPU cores: 12
Binaries:
Node: 24.14.1
npm: 11.11.0
Yarn: 1.22.22
pnpm: 9.14.4
Relevant Packages:
next: 16.2.2 // Latest available version is detected (16.2.2).
eslint-config-next: N/A
react: 19.2.4
react-dom: 19.2.4
typescript: 6.0.2
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next build (local), Vercel (Deployed)
Additional context
This looks like a duplicate of #85366, which was marked as solved.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the warning from the linked project using app/page.tsx line 14, script.ts, and npx next build; compare it with the readFile workaround on line 13. Trace how the dynamic path is analyzed during the build and verify that the reported match count reflects the 1000 generated files rather than 16000. Compare the behavior with issue #85366 before deciding whether this is a distinct fix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nextjs, typescript
- Domain
- build-system, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100