Repository navigation
feat(nextjs): Warn when the instrumentation file is in the wrong folder #25603
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ import { getBuildLogger } from '../buildLogger'; | |
| import { createRouteManifest } from '../manifest/createRouteManifest'; | ||
| import type { RouteManifest } from '../manifest/types'; | ||
| import type { NextConfigObject, SentryBuildOptions } from '../types'; | ||
| import { requiresInstrumentationHook } from '../util'; | ||
| import { DEFAULT_PAGE_EXTENSIONS, requiresInstrumentationHook } from '../util'; | ||
| import { getGitRevision, getInstrumentationClientFileContents } from './buildTime'; | ||
| import { | ||
| findTunnelRouteHandlerFile, | ||
|
|
@@ -21,6 +21,8 @@ let showedUnusedTunnelRouteHandlerWarning = false; | |
| let showedTunnelRouteHandlerModeLog = false; | ||
| let showedExperimentalBuildModeWarning = false; | ||
|
|
||
| const MISPLACED_INSTRUMENTATION_FILE_WARNING_SHOWN = '__SENTRY_MISPLACED_INSTRUMENTATION_FILE_WARNING_SHOWN__'; | ||
|
|
||
| /** | ||
| * Resolves the Sentry release name to use for build-time behavior. | ||
| * | ||
|
|
@@ -266,6 +268,48 @@ export function warnIfMissingOnRouterTransitionStartHook(userSentryOptions: Sent | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Warns if the `instrumentation` file is not in the folder that contains the `app` or `pages` directory. | ||
| * `next build` only looks there, even though dev and Turbopack also find the file in other places. | ||
| */ | ||
| export function warnIfMisplacedInstrumentationFile( | ||
| incomingUserNextConfigObject: NextConfigObject, | ||
| userSentryOptions: SentryBuildOptions, | ||
| ): void { | ||
| if ( | ||
| process.env[MISPLACED_INSTRUMENTATION_FILE_WARNING_SHOWN] || | ||
| process.env.SENTRY_SUPPRESS_INSTRUMENTATION_FILE_WARNING | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const projectDir = process.cwd(); | ||
| const exists = (relativePath: string): boolean => fs.existsSync(path.join(projectDir, relativePath)); | ||
|
|
||
| // Same lookup as Next.js' `findPagesDir`: `pages` before `app`, and `<name>` before `src/<name>`. | ||
| const routerDir = ['pages', 'app'].flatMap(name => [name, path.join('src', name)]).find(exists); | ||
| if (!routerDir) { | ||
| return; | ||
| } | ||
|
|
||
| const loadedDir = path.dirname(routerDir); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to show the user in what wrong location the instrumentation file is? as in could we not just simplify this logic to:
I'd assume a user will know where he placed this file or can easily find out and less logic that might be buggy |
||
| const ignoredDir = loadedDir === 'src' ? '.' : 'src'; | ||
| const pageExtensions = incomingUserNextConfigObject.pageExtensions ?? DEFAULT_PAGE_EXTENSIONS; | ||
| const findInstrumentationFile = (dir: string): string | undefined => | ||
| pageExtensions.map(extension => path.join(dir, `instrumentation.${extension}`)).find(exists); | ||
|
|
||
| const misplacedFile = findInstrumentationFile(ignoredDir); | ||
| if (!misplacedFile || findInstrumentationFile(loadedDir)) { | ||
| return; | ||
| } | ||
|
|
||
| // Next.js may evaluate its config in child processes, which inherit this state from their parent. | ||
| process.env[MISPLACED_INSTRUMENTATION_FILE_WARNING_SHOWN] = '1'; | ||
| getBuildLogger(userSentryOptions.silent).warn( | ||
| `[@sentry/nextjs] Found ${misplacedFile}, but Next.js production builds only pick up the instrumentation file next to your ${routerDir} directory. Move it to ${path.join(loadedDir, path.basename(misplacedFile))}, or the Sentry SDK may not be initialized on the server in production.`, | ||
| ); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feat lacks integration or E2E testLow Severity This feat is covered only by unit tests that call Additional Locations (1)Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit 26e519d. Configure here. |
||
|
|
||
| /** | ||
| * Parses the major Next.js version number from a semver string. | ||
| */ | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a suggestion for readability, imo this is a bit more explicit and easier to understand at first glance