diff --git a/.changeset/next16-standalone-instrumentation.md b/.changeset/next16-standalone-instrumentation.md new file mode 100644 index 000000000..63643a5fd --- /dev/null +++ b/.changeset/next16-standalone-instrumentation.md @@ -0,0 +1,13 @@ +--- +"@opennextjs/aws": patch +--- + +Fix `server/instrumentation.js does not exist` build failure on Next.js 16 + +On Next.js 16 the standalone output no longer copies `server/instrumentation.js` +into the standalone directory, but `copyTracedFiles` copies the instrumentation +`.nft.json` trace and then asserts the `.js` file exists in the standalone dir, +throwing `File server/instrumentation.js does not exist` during the server +bundle. The instrumentation file is now copied from the build dir into the +standalone dir (mirroring the existing `.nft.json` copy) so the assertion passes +and the file ships in the bundle. diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 9f6689c0f..317006d04 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -243,6 +243,20 @@ File ${serverPath} does not exist path.join(dotNextDir, INSTRUMENTATION_TRACE_FILE), path.join(standaloneNextDir, INSTRUMENTATION_TRACE_FILE), ); + // Next 16's standalone output no longer includes `server/instrumentation.js` + // in the standalone dir, but `computeCopyFilesForPage` asserts it exists + // there. Copy it from the build dir (mirroring the .nft.json copy above) so + // the assertion passes and the instrumentation file ships in the bundle. + const instrumentationServerFile = path.join("server", "instrumentation.js"); + if ( + existsSync(path.join(dotNextDir, instrumentationServerFile)) && + !existsSync(path.join(standaloneNextDir, instrumentationServerFile)) + ) { + copyFileAndMakeOwnerWritable( + path.join(dotNextDir, instrumentationServerFile), + path.join(standaloneNextDir, instrumentationServerFile), + ); + } computeCopyFilesForPage("instrumentation"); logger.debug("Adding instrumentation trace files"); }