Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 12 additions & 36 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,43 +254,19 @@ function getFinalConfigObject(

let nextMajor: number | undefined;
const isTurbopack = process.env.TURBOPACK;
let isTurbopackSupported = false;
if (nextJsVersion) {
const { major, minor, patch, prerelease } = parseSemver(nextJsVersion);
nextMajor = major;
const isSupportedVersion =
major !== undefined &&
minor !== undefined &&
patch !== undefined &&
(major > 15 ||
(major === 15 && minor > 3) ||
(major === 15 && minor === 3 && patch === 0 && prerelease === undefined) ||
(major === 15 && minor === 3 && patch > 0));
isTurbopackSupported = isSupportedVersion;
const isSupportedCanary =
major !== undefined &&
minor !== undefined &&
patch !== undefined &&
prerelease !== undefined &&
major === 15 &&
minor === 3 &&
patch === 0 &&
prerelease.startsWith('canary.') &&
parseInt(prerelease.split('.')[1] || '', 10) >= 28;
const supportsClientInstrumentation = isSupportedCanary || isSupportedVersion;
const isTurbopackSupported = supportsProductionCompileHook();

if (!supportsClientInstrumentation && isTurbopack) {
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn(
`[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (\`next dev --turbo\`). The Sentry SDK is compatible with Turbopack on Next.js version 15.3.0 or later. You are currently on ${nextJsVersion}. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack. Note that the SDK will continue to work for non-Turbopack production builds. This warning is only about dev-mode.`,
);
} else if (process.env.NODE_ENV === 'production') {
// eslint-disable-next-line no-console
console.warn(
`[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (\`next build --turbo\`). The Sentry SDK is compatible with Turbopack on Next.js version 15.3.0 or later. You are currently on ${nextJsVersion}. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack. Note that as Turbopack is still experimental for production builds, some of the Sentry SDK features like source maps will not work. Follow this issue for progress on Sentry + Turbopack: https://github.com/getsentry/sentry-javascript/issues/8105.`,
);
}
if (!isTurbopackSupported && isTurbopack) {
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn(
`[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (\`next dev --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.`,
);
} else if (process.env.NODE_ENV === 'production') {
// eslint-disable-next-line no-console
console.warn(
`[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (\`next build --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.`,
);
}
}

Expand Down
202 changes: 202 additions & 0 deletions packages/nextjs/test/config/withSentryConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,206 @@ describe('withSentryConfig', () => {
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
});
});

describe('turbopack version compatibility warnings', () => {
const originalTurbopack = process.env.TURBOPACK;
const originalNodeEnv = process.env.NODE_ENV;

afterEach(() => {
vi.restoreAllMocks();
process.env.TURBOPACK = originalTurbopack;
// @ts-expect-error - NODE_ENV is read-only in types but we need to restore it in tests
process.env.NODE_ENV = originalNodeEnv;
});

it('warns in development mode when Turbopack is enabled with unsupported 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
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).toHaveBeenCalledWith(
'[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (`next dev --turbopack`). The Sentry SDK is compatible with Turbopack on Next.js version 15.4.1 or later. You are currently on 15.4.0. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack.',
);

consoleWarnSpy.mockRestore();
});

it('warns in production mode when Turbopack is enabled with unsupported 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
process.env.NODE_ENV = 'production';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.9');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).toHaveBeenCalledWith(
'[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (`next build --turbopack`). The Sentry SDK is compatible with Turbopack on Next.js version 15.4.1 or later. You are currently on 15.3.9. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack.',
);

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
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});

it('does not warn when Turbopack is enabled with higher 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
process.env.NODE_ENV = 'production';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.5.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});

it('does not warn when Turbopack is enabled with Next.js 16+', () => {
process.env.TURBOPACK = '1';
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});

it('does not warn when Turbopack is not enabled', () => {
delete process.env.TURBOPACK;
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});

it('warns even when Next.js version cannot be determined if Turbopack is unsupported', () => {
process.env.TURBOPACK = '1';
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue(undefined);
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

// Warning will still show because supportsProductionCompileHook returns false
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('You are currently on undefined'));

consoleWarnSpy.mockRestore();
});

it('warns with correct version in message for edge case versions', () => {
process.env.TURBOPACK = '1';
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0-canary.15');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).toHaveBeenCalledWith(
'[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack (`next dev --turbopack`). The Sentry SDK is compatible with Turbopack on Next.js version 15.4.1 or later. You are currently on 15.4.0-canary.15. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack.',
);

consoleWarnSpy.mockRestore();
});

it('does not warn in other environments besides development and production', () => {
process.env.TURBOPACK = '1';
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'test';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});

it('handles falsy TURBOPACK environment variable', () => {
process.env.TURBOPACK = '';
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});

it('warns when TURBOPACK=0 (truthy string) with unsupported version', () => {
process.env.TURBOPACK = '0';
// @ts-expect-error - NODE_ENV is read-only in types but we need to set it for testing
process.env.NODE_ENV = 'development';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

materializeFinalNextConfig(exportedNextConfig);

// Note: '0' is truthy in JavaScript, so this will trigger the warning
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'),
);

consoleWarnSpy.mockRestore();
});
});
});
Loading