diff --git a/packages/nextjs/src/config/withSentryConfig/getFinalConfigObjectBundlerUtils.ts b/packages/nextjs/src/config/withSentryConfig/getFinalConfigObjectBundlerUtils.ts index 60b2d3e96538..76432027ae56 100644 --- a/packages/nextjs/src/config/withSentryConfig/getFinalConfigObjectBundlerUtils.ts +++ b/packages/nextjs/src/config/withSentryConfig/getFinalConfigObjectBundlerUtils.ts @@ -13,6 +13,8 @@ import { constructWebpackConfigFunction } from '../webpack'; import { DEFAULT_SERVER_EXTERNAL_PACKAGES } from './constants'; import type { VercelCronsConfigResult } from './getFinalConfigObjectUtils'; +const UNSUPPORTED_TURBOPACK_WARNING_SHOWN = '__SENTRY_UNSUPPORTED_TURBOPACK_WARNING_SHOWN__'; + /** * Information about the active bundler and feature support based on Next.js version. */ @@ -43,7 +45,14 @@ export function maybeWarnAboutUnsupportedTurbopack( silent?: boolean, ): void { // Warn if using turbopack with an unsupported Next.js version - if (!bundlerInfo.isTurbopackSupported && bundlerInfo.isTurbopack) { + if ( + !bundlerInfo.isTurbopackSupported && + bundlerInfo.isTurbopack && + !silent && + process.env[UNSUPPORTED_TURBOPACK_WARNING_SHOWN] !== '1' + ) { + // Next.js may evaluate its config in child processes, which inherit this state from their parent. + process.env[UNSUPPORTED_TURBOPACK_WARNING_SHOWN] = '1'; getBuildLogger(silent).warn( `[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack. The Sentry SDK is compatible with Turbopack on Next.js version 15.4.1 or later. You are currently on ${nextJsVersion}. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack.`, ); diff --git a/packages/nextjs/test/config/silent.test.ts b/packages/nextjs/test/config/silent.test.ts index 0892fe80dfe1..3cd414fc2867 100644 --- a/packages/nextjs/test/config/silent.test.ts +++ b/packages/nextjs/test/config/silent.test.ts @@ -21,6 +21,14 @@ const WEBPACK: BundlerInfo = { isTurbopack: false, isWebpack: true, isTurbopackS // variants need distinct paths to both actually scan. Neither is a directory, which is what makes it log. const NOT_A_DIRECTORY = { silent: __filename, notSilent: path.join(__dirname, 'testUtils.ts') }; +beforeEach(() => { + delete process.env.__SENTRY_UNSUPPORTED_TURBOPACK_WARNING_SHOWN__; +}); + +afterEach(() => { + delete process.env.__SENTRY_UNSUPPORTED_TURBOPACK_WARNING_SHOWN__; +}); + describe('getBuildLogger', () => { it('forwards to the console when not silent', () => { const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); diff --git a/packages/nextjs/test/config/withSentryConfig.test.ts b/packages/nextjs/test/config/withSentryConfig.test.ts index 303b3413f7a8..977bd4ae72ed 100644 --- a/packages/nextjs/test/config/withSentryConfig.test.ts +++ b/packages/nextjs/test/config/withSentryConfig.test.ts @@ -19,6 +19,14 @@ const EXPECTED_DEFAULT_EXTERNALS = [ ]; describe('withSentryConfig', () => { + beforeEach(() => { + delete process.env.__SENTRY_UNSUPPORTED_TURBOPACK_WARNING_SHOWN__; + }); + + afterEach(() => { + delete process.env.__SENTRY_UNSUPPORTED_TURBOPACK_WARNING_SHOWN__; + }); + // `next.config.js` / `next.config.mjs` get no type checking, so this warning is the only signal // those users receive that the option is gone. describe('removed `unstable_sentryWebpackPluginOptions`', () => { @@ -1280,6 +1288,24 @@ describe('withSentryConfig', () => { consoleWarnSpy.mockRestore(); }); + it('warns only once when the config is materialized repeatedly', () => { + process.env.TURBOPACK = '1'; + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0'); + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false); + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + materializeFinalNextConfig(exportedNextConfig); + materializeFinalNextConfig(exportedNextConfig); + materializeFinalNextConfig(exportedNextConfig); + + const turbopackWarnings = consoleWarnSpy.mock.calls.filter(([message]) => + String(message).includes('WARNING: You are using the Sentry SDK with Turbopack'), + ); + expect(turbopackWarnings).toHaveLength(1); + + consoleWarnSpy.mockRestore(); + }); + it('does not warn when Turbopack is enabled with supported Next.js version', () => { process.env.TURBOPACK = '1'; // @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing