Skip to content

fix(next): resolve src/ entry patterns like Next.js does#1859

Merged
webpro merged 1 commit into
webpro-nl:mainfrom
dayongkr:feat/next-conditional-src-patterns
Jul 5, 2026
Merged

fix(next): resolve src/ entry patterns like Next.js does#1859
webpro merged 1 commit into
webpro-nl:mainfrom
dayongkr:feat/next-conditional-src-patterns

Conversation

@dayongkr

@dayongkr dayongkr commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Problem

The plugin marks files as production entries that Next.js never even picks up, so dead code sits there unreported.

Every entry pattern is added twice, for the root and for src/, but Next.js only ever uses one of the two locations per router directory — from the src folder docs:

src/app or src/pages will be ignored if app or pages are present in the root directory.

Example — a project routes from a root pages/ directory and keeps the page implementations under src/pages/:

pages/home.tsx           route (entry)
src/pages/HomePage.tsx   implementation, imported from pages/home.tsx
src/pages/OldPage.tsx    imported by nothing: dead code

Next.js ignores src/pages here, but the plugin's src/pages/** entry pattern marks every file in it as a production entry, so OldPage.tsx is never reported as an unused file.

Fix

When next.config.* is present, locate each router directory the way Next.js does (find-pages-dir.ts, unchanged since v12) and scope the app/ and pages/ entry patterns to the resolved location: root when the root directory exists, src/ when only the src one does, both when neither is found.

pages and app are located independently, since mixing a root pages/ with src/app is valid up to Next.js 15 (the same-parent restriction was added in 16.0).

Root-level file conventions (middleware, proxy, instrumentation, instrumentation-client) are out of scope and stay matched in both locations, as before.

Tests

  • next-root-and-src (new): root app/ + pages/ next to a src/ folder — src/app/page.tsx and src/pages/legacy.ts are reported unused; root entries and src/middleware.ts are not.
  • next-mixed-routers (new): root pages/ combined with src/app — all router files stay entries.

@dayongkr
dayongkr force-pushed the feat/next-conditional-src-patterns branch 4 times, most recently from 5b2d17a to 292bfe8 Compare July 5, 2026 08:50
@dayongkr
dayongkr marked this pull request as ready for review July 5, 2026 08:59
@dayongkr
dayongkr marked this pull request as draft July 5, 2026 09:07
@dayongkr
dayongkr force-pushed the feat/next-conditional-src-patterns branch from 292bfe8 to 20c2319 Compare July 5, 2026 09:30
@dayongkr
dayongkr force-pushed the feat/next-conditional-src-patterns branch from 20c2319 to 1cc0062 Compare July 5, 2026 09:34
@dayongkr
dayongkr marked this pull request as ready for review July 5, 2026 09:35
@pkg-pr-new

pkg-pr-new Bot commented Jul 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@1859
npm i https://pkg.pr.new/@knip/language-server@1859
npm i https://pkg.pr.new/@knip/mcp@1859

commit: 1cc0062

@webpro

webpro commented Jul 5, 2026

Copy link
Copy Markdown
Member

Thank you @dayongkr! ✨

@webpro
webpro merged commit 6e6a509 into webpro-nl:main Jul 5, 2026
30 checks passed
@webpro

webpro commented Jul 7, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v6.25.0. See Release 6.25.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

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.

2 participants