From b96fd6fcfb6934a4cbdfe4375f24593d37907e73 Mon Sep 17 00:00:00 2001 From: RulaKhaled Date: Wed, 28 May 2025 12:48:46 +0200 Subject: [PATCH 1/2] fix(nextjs): Skip re instrumentating on generate phase of experimental build mode --- .../nextjs/src/config/withSentryConfig.ts | 22 ++++++++++++++++ .../test/config/withSentryConfig.test.ts | 25 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index 7eedcc763e9b..9489554f2ed1 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -15,6 +15,7 @@ import { getNextjsVersion } from './util'; import { constructWebpackConfigFunction } from './webpack'; let showedExportModeTunnelWarning = false; +let showedExperimentalBuildModeWarning = false; /** * Modifies the passed in Next.js configuration with automatic build-time instrumentation and source map upload. @@ -67,6 +68,27 @@ function getFinalConfigObject( } } + if (process.argv.includes('--experimental-build-mode')) { + if (!showedExperimentalBuildModeWarning) { + showedExperimentalBuildModeWarning = true; + // eslint-disable-next-line no-console + console.warn( + '[@sentry/nextjs] The Sentry Next.js SDK does not currently fully support next build --experimental-build-mode', + ); + } + if (process.argv.includes('generate')) { + // Next.js v15.3.0-canary.1 splits the experimental build into two phases: + // 1. compile: Code compilation + // 2. generate: Environment variable inlining and prerendering (We don't instrument this phase, we inline in the compile phase) + // + // We assume a single “full” build and reruns Webpack instrumentation in both phases. + // During the generate step it collides with Next.js’s new inliner (they do some bad replacements in the inliner) + // producing malformed JS and build failures. + // We skip Sentry processing during generate to avoid this issue. + return incomingUserNextConfigObject; + } + } + setUpBuildTimeVariables(incomingUserNextConfigObject, userSentryOptions, releaseName); const nextJsVersion = getNextjsVersion(); diff --git a/packages/nextjs/test/config/withSentryConfig.test.ts b/packages/nextjs/test/config/withSentryConfig.test.ts index 6ad9a83e6791..f9db1a68771e 100644 --- a/packages/nextjs/test/config/withSentryConfig.test.ts +++ b/packages/nextjs/test/config/withSentryConfig.test.ts @@ -50,4 +50,29 @@ describe('withSentryConfig', () => { expect(exportedNextConfigFunction).toHaveBeenCalledWith(defaultRuntimePhase, defaultsObject); }); + + it('handles experimental build mode correctly', () => { + const originalArgv = process.argv; + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + try { + process.argv = [...originalArgv, '--experimental-build-mode']; + materializeFinalNextConfig(exportedNextConfig); + + expect(consoleWarnSpy).toHaveBeenCalledWith( + '[@sentry/nextjs] The Sentry Next.js SDK does not currently fully support next build --experimental-build-mode', + ); + + // Generate phase + process.argv = [...process.argv, 'generate']; + const generateConfig = materializeFinalNextConfig(exportedNextConfig); + + expect(generateConfig).toEqual(exportedNextConfig); + + expect(consoleWarnSpy).toHaveBeenCalledTimes(1); + } finally { + process.argv = originalArgv; + consoleWarnSpy.mockRestore(); + } + }); }); From 681247c077a99812dc9e55213495cd4b6995705d Mon Sep 17 00:00:00 2001 From: Rola Abuhasna Date: Tue, 3 Jun 2025 15:24:02 +0200 Subject: [PATCH 2/2] Update packages/nextjs/src/config/withSentryConfig.ts Co-authored-by: Charly Gomez --- packages/nextjs/src/config/withSentryConfig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index 9489554f2ed1..543f271c1999 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -82,7 +82,7 @@ function getFinalConfigObject( // 2. generate: Environment variable inlining and prerendering (We don't instrument this phase, we inline in the compile phase) // // We assume a single “full” build and reruns Webpack instrumentation in both phases. - // During the generate step it collides with Next.js’s new inliner (they do some bad replacements in the inliner) + // During the generate step it collides with Next.js’s inliner // producing malformed JS and build failures. // We skip Sentry processing during generate to avoid this issue. return incomingUserNextConfigObject;