Skip to content

feat: add validation for root-level files when src directory exists #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from

Conversation

austinderek
Copy link
Owner

What?

This PR adds validation to throw an error when root-level files (middleware and instrumentation) are found outside the src/ directory when a src/ directory structure is being used.

Why?

Currently, Next.js allows middleware and instrumentation files to exist at the project root even when using a src/ directory structure. This can lead to confusion and inconsistent file organization. The Next.js documentation states that when using the src directory, middleware should be placed inside the src folder, but this wasn't enforced.

How?

  1. Added new error class: SrcDirectoryError in packages/next/src/build/utils.ts that provides clear error messaging and documentation links

  2. Added validation functions:

    • validateMiddlewareInSrcDir(fileName: string, isSrcDir: boolean)
    • validateInstrumentationInSrcDir(fileName: string, isSrcDir: boolean)
  3. Integrated validation in two key locations:

    • Development: packages/next/src/server/lib/router-utils/setup-dev-bundler.ts - validates during dev server startup
    • Build time: packages/next/src/build/index.ts - validates during production build
  4. Added test cases: Created test/integration/src-dir-validation/ to verify the validation behavior

The validation logic checks:

  • If src/ directory structure is being used (isSrcDir)
  • If the file is a middleware or instrumentation file
  • If the file is located outside the src/ directory
  • If all conditions are true, throws SrcDirectoryError with helpful guidance

⚠️ Human Review Checklist

Critical items to verify:

  1. File path logic: Verify that fileName.startsWith('/src/') correctly identifies files inside src directory for Next.js internal file paths
  2. Test execution: Run the new tests in test/integration/src-dir-validation/ to ensure they work correctly
  3. Existing tests: Confirm existing middleware-related tests still pass (especially test/integration/middleware-src)
  4. Dev vs Build consistency: Test that validation works identically in both next dev and next build scenarios
  5. Error message accuracy: Verify the documentation link in the error message is correct and helpful
  6. Edge cases: Test scenarios like:
    • Projects without src directory (should not validate)
    • Projects with src directory but no middleware/instrumentation (should not error)
    • Mixed scenarios with some files in src and some outside

⚠️ Testing Gap: This implementation could not be tested locally due to missing pnpm dependency. The validation logic and integration points need thorough testing to ensure correctness.

Breaking Change Consideration: This introduces new fatal errors for previously "working" (but incorrectly organized) projects. Consider if this should be a warning first before becoming an error in a future version.


Link to Devin run: https://app.devin.ai/sessions/94e9f081963a4ec3bc8bc9c8c1ca8975
Requested by: @devjiwonchoi


🔄 This is a mirror of upstream PR #82514

devin-ai-integration bot and others added 3 commits August 10, 2025 11:14
- Add SrcDirectoryError class for consistent error messaging
- Add validateMiddlewareInSrcDir and validateInstrumentationInSrcDir functions
- Integrate validation in both dev bundler and build processes
- Throw error when middleware/instrumentation files are outside src/ when src/ directory exists
- Add test cases to verify validation behavior

Co-Authored-By: Jiwon Choi <devjiwonchoi@gmail.com>
Co-Authored-By: Jiwon Choi <devjiwonchoi@gmail.com>
…ion issues

- Use normalizePathSep for cross-platform path handling
- Strip file extensions before validation in build context
- Ensure validation works consistently in dev and build modes

Co-Authored-By: Jiwon Choi <devjiwonchoi@gmail.com>
Copy link

staging bot commented Aug 11, 2025

✅ This pull request was succesfully scanned and it was determined that it does not contain any changes that present immediate security concerns. If you would still like for it to be reviewed by an expert from our reviewer community, you can submit it manually via the HackerOne PullRequest dashboard.

⏱️ Latest scan covered changes up to commit 8bafaef (latest)

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