Skip to content

fix: prioritize src/ directory for instrumentation.ts in development #82513

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import getGzipSize from 'next/dist/compiled/gzip-size'
import textTable from 'next/dist/compiled/text-table'
import path from 'path'
import { promises as fs } from 'fs'
import { promises as fs, existsSync } from 'fs'
import { isValidElementType } from 'next/dist/compiled/react-is'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import browserslist from 'next/dist/compiled/browserslist'
Expand Down Expand Up @@ -1783,11 +1783,20 @@ export function getPossibleInstrumentationHookFilenames(
extensions: string[]
) {
const files = []
const srcDir = path.join(folder, 'src')

const hasSrcDir = existsSync(srcDir)

for (const extension of extensions) {
files.push(
path.join(folder, `${INSTRUMENTATION_HOOK_FILENAME}.${extension}`),
path.join(folder, `src`, `${INSTRUMENTATION_HOOK_FILENAME}.${extension}`)
)
if (hasSrcDir) {
files.push(
path.join(folder, `src`, `${INSTRUMENTATION_HOOK_FILENAME}.${extension}`)
)
} else {
files.push(
path.join(folder, `${INSTRUMENTATION_HOOK_FILENAME}.${extension}`)
)
}
Comment on lines +1791 to +1799
Copy link
Contributor

@vercel vercel bot Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function now only returns instrumentation hook files from either the root OR src directory, but Next.js is designed to support both locations simultaneously.

View Details

Analysis

The getPossibleInstrumentationHookFilenames function has been modified to only return file paths from one location (either root or src directory) based on whether the src directory exists. This breaks the intended functionality because:

  1. Multiple functions confirm both locations should be valid: Other functions in the same file like isInstrumentationHookFile (lines 1776-1777) and isInstrumentationHookFilename (lines 309-310) clearly show that instrumentation hooks are valid in both /instrumentation and /src/instrumentation locations simultaneously.

  2. Function name implies all possibilities: The function is named getPossibleInstrumentationHookFilenames (emphasis on "Possible"), suggesting it should return all potential locations where the files could exist.

  3. Original behavior was correct: The original implementation returned both paths for each extension, allowing the consumer to check all possible locations.

  4. Potential missed files: If a user has instrumentation files in both the root and src directories, the new logic will only detect files in one location, potentially missing important instrumentation hooks.

The function is used in file watching scenarios where all possible paths should be monitored, not just paths from one directory.


Recommendation

Revert the logic to return both possible paths for each extension, similar to the original implementation:

export function getPossibleInstrumentationHookFilenames(
  folder: string,
  extensions: string[]
) {
  const files = []
  for (const extension of extensions) {
    files.push(
      path.join(folder, `${INSTRUMENTATION_HOOK_FILENAME}.${extension}`),
      path.join(folder, `src`, `${INSTRUMENTATION_HOOK_FILENAME}.${extension}`)
    )
  }
  return files
}

If optimization is desired, the file existence checking should be done by the consumer of this function, not within the function that's supposed to return all "possible" locations.

}

return files
Expand Down
Loading