Skip to content

Commit 42d05fd

Browse files
committed
Fix instrumentation.ts loading inconsistency between dev and production
- Add validation to detect conflicting instrumentation files (both root and src/) - Throw error when both root and src instrumentation files exist - Apply fix to both build process and dev server - Consistent with middleware/proxy conflict handling
1 parent 40b9243 commit 42d05fd

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

packages/next/errors.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,5 +1050,7 @@
10501050
"1049": "Turbopack is not supported on this platform (%s/%s) because native bindings are not available. Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings.\\n\\nTo use Next.js on this platform, use Webpack instead:\\n next dev --webpack\\n\\nFor more information, see: https://nextjs.org/docs/app/api-reference/turbopack#supported-platforms",
10511051
"1050": "Turbopack is not supported on this platform (%s/%s) because native bindings are not available. Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings.\\n\\nTo build on this platform, use Webpack instead:\\n next build --webpack\\n\\nFor more information, see: https://nextjs.org/docs/app/api-reference/turbopack#supported-platforms",
10521052
"1051": "Turbopack trace server is not supported on this platform (%s/%s) because native bindings are not available. Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings.",
1053-
"1052": "Turbopack is not supported on this platform (%s/%s) because native bindings are not available. Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings. Use the --webpack flag instead."
1053+
"1052": "Turbopack is not supported on this platform (%s/%s) because native bindings are not available. Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings. Use the --webpack flag instead.",
1054+
"1053": "Conflicting instrumentation files detected: \"./%s\" and \"./%s\". Please remove one of them.",
1055+
"1054": "Conflicting instrumentation files detected: \"%s\" and \"%s\". Please remove one of them."
10541056
}

packages/next/src/build/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,8 @@ export default async function build(
12151215
.map((file) => path.join(rootDir, file).replace(dir, ''))
12161216

12171217
let instrumentationHookFilePath: string | undefined
1218+
let rootInstrumentationHookFilePath: string | undefined
1219+
let srcInstrumentationHookFilePath: string | undefined
12181220
let proxyFilePath: string | undefined
12191221
let middlewareFilePath: string | undefined
12201222

@@ -1235,10 +1237,31 @@ export default async function build(
12351237
isAtConventionLevel &&
12361238
fileBaseName === INSTRUMENTATION_HOOK_FILENAME
12371239
) {
1240+
if (normalizedFileDir === '/') {
1241+
rootInstrumentationHookFilePath = rootPath
1242+
} else if (normalizedFileDir === '/src') {
1243+
srcInstrumentationHookFilePath = rootPath
1244+
}
12381245
instrumentationHookFilePath = rootPath
12391246
}
12401247
}
12411248

1249+
if (rootInstrumentationHookFilePath && srcInstrumentationHookFilePath) {
1250+
const cwd = process.cwd()
1251+
const absoluteRootPath = path.join(
1252+
rootDir,
1253+
rootInstrumentationHookFilePath
1254+
)
1255+
const absoluteSrcPath = path.join(
1256+
rootDir,
1257+
srcInstrumentationHookFilePath
1258+
)
1259+
1260+
throw new Error(
1261+
`Conflicting instrumentation files detected: "./${path.relative(cwd, absoluteRootPath)}" and "./${path.relative(cwd, absoluteSrcPath)}". Please remove one of them.`
1262+
)
1263+
}
1264+
12421265
if (middlewareFilePath) {
12431266
if (proxyFilePath) {
12441267
const cwd = process.cwd()

packages/next/src/server/lib/router-utils/setup-dev-bundler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,15 @@ async function startWatcher(
591591
continue
592592
}
593593
if (isInstrumentationHookFile(rootFile)) {
594+
if (
595+
serverFields.actualInstrumentationHookFile &&
596+
serverFields.actualInstrumentationHookFile !== rootFile
597+
) {
598+
const existingFile = serverFields.actualInstrumentationHookFile
599+
throw new Error(
600+
`Conflicting instrumentation files detected: "${existingFile}" and "${rootFile}". Please remove one of them.`
601+
)
602+
}
594603
serverFields.actualInstrumentationHookFile = rootFile
595604
await propagateServerField(
596605
opts,

0 commit comments

Comments
 (0)