Skip to content

Commit c084bd6

Browse files
authored
ref(nextjs): Display build compatibility warning for webpack (#17746)
1 parent 16ee36a commit c084bd6

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ function getFinalConfigObject(
275275
}
276276
}
277277

278+
// webpack case
279+
if (
280+
userSentryOptions.useRunAfterProductionCompileHook &&
281+
!supportsProductionCompileHook(nextJsVersion ?? '') &&
282+
!isTurbopack
283+
) {
284+
// eslint-disable-next-line no-console
285+
console.warn(
286+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
287+
);
288+
}
289+
278290
// If not explicitly set, turbopack uses the runAfterProductionCompile hook (as there are no alternatives), webpack does not.
279291
const shouldUseRunAfterProductionCompileHook =
280292
userSentryOptions?.useRunAfterProductionCompileHook ?? (isTurbopack ? true : false);

packages/nextjs/test/config/withSentryConfig.test.ts

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,4 +1173,277 @@ describe('withSentryConfig', () => {
11731173
consoleWarnSpy.mockRestore();
11741174
});
11751175
});
1176+
1177+
describe('useRunAfterProductionCompileHook warning logic', () => {
1178+
const originalTurbopack = process.env.TURBOPACK;
1179+
1180+
afterEach(() => {
1181+
vi.restoreAllMocks();
1182+
process.env.TURBOPACK = originalTurbopack;
1183+
});
1184+
1185+
it('warns when useRunAfterProductionCompileHook is enabled with unsupported Next.js version in webpack mode', () => {
1186+
delete process.env.TURBOPACK; // Ensure webpack mode
1187+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0'); // Unsupported version
1188+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1189+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1190+
1191+
const sentryOptions = {
1192+
useRunAfterProductionCompileHook: true,
1193+
};
1194+
1195+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1196+
1197+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1198+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1199+
);
1200+
1201+
consoleWarnSpy.mockRestore();
1202+
});
1203+
1204+
it('does not warn when useRunAfterProductionCompileHook is enabled with supported Next.js version in webpack mode', () => {
1205+
delete process.env.TURBOPACK; // Ensure webpack mode
1206+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1'); // Supported version
1207+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
1208+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1209+
1210+
const sentryOptions = {
1211+
useRunAfterProductionCompileHook: true,
1212+
};
1213+
1214+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1215+
1216+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1217+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1218+
);
1219+
1220+
consoleWarnSpy.mockRestore();
1221+
});
1222+
1223+
it('does not warn when useRunAfterProductionCompileHook is disabled with unsupported Next.js version in webpack mode', () => {
1224+
delete process.env.TURBOPACK; // Ensure webpack mode
1225+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0'); // Unsupported version
1226+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1227+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1228+
1229+
const sentryOptions = {
1230+
useRunAfterProductionCompileHook: false,
1231+
};
1232+
1233+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1234+
1235+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1236+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1237+
);
1238+
1239+
consoleWarnSpy.mockRestore();
1240+
});
1241+
1242+
it('does not warn when useRunAfterProductionCompileHook is undefined with unsupported Next.js version in webpack mode', () => {
1243+
delete process.env.TURBOPACK; // Ensure webpack mode
1244+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0'); // Unsupported version
1245+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1246+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1247+
1248+
const sentryOptions = {}; // useRunAfterProductionCompileHook is undefined
1249+
1250+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1251+
1252+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1253+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1254+
);
1255+
1256+
consoleWarnSpy.mockRestore();
1257+
});
1258+
1259+
it('does not warn when useRunAfterProductionCompileHook is enabled with unsupported Next.js version in turbopack mode', () => {
1260+
process.env.TURBOPACK = '1'; // Ensure turbopack mode
1261+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0'); // Unsupported version
1262+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1263+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1264+
1265+
const sentryOptions = {
1266+
useRunAfterProductionCompileHook: true,
1267+
};
1268+
1269+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1270+
1271+
// Should not warn about useRunAfterProductionCompileHook incompatibility in turbopack mode
1272+
// (though it may warn about turbopack version compatibility)
1273+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1274+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1275+
);
1276+
1277+
consoleWarnSpy.mockRestore();
1278+
});
1279+
1280+
it('warns with different unsupported Next.js versions', () => {
1281+
delete process.env.TURBOPACK; // Ensure webpack mode
1282+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1283+
1284+
const sentryOptions = {
1285+
useRunAfterProductionCompileHook: true,
1286+
};
1287+
1288+
// Test with 15.3.9
1289+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.9');
1290+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1291+
1292+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1293+
1294+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1295+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1296+
);
1297+
1298+
consoleWarnSpy.mockClear();
1299+
1300+
// Test with 14.2.0
1301+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('14.2.0');
1302+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1303+
1304+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1305+
1306+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1307+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1308+
);
1309+
1310+
consoleWarnSpy.mockClear();
1311+
1312+
// Test with canary version that's unsupported
1313+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0-canary.42');
1314+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1315+
1316+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1317+
1318+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1319+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1320+
);
1321+
1322+
consoleWarnSpy.mockRestore();
1323+
});
1324+
1325+
it('does not warn with supported Next.js versions', () => {
1326+
delete process.env.TURBOPACK; // Ensure webpack mode
1327+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1328+
1329+
const sentryOptions = {
1330+
useRunAfterProductionCompileHook: true,
1331+
};
1332+
1333+
// Test with 15.4.1
1334+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
1335+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
1336+
1337+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1338+
1339+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1340+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1341+
);
1342+
1343+
consoleWarnSpy.mockClear();
1344+
1345+
// Test with 15.5.0
1346+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.5.0');
1347+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
1348+
1349+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1350+
1351+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1352+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1353+
);
1354+
1355+
consoleWarnSpy.mockClear();
1356+
1357+
// Test with 16.0.0
1358+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0');
1359+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
1360+
1361+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1362+
1363+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1364+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1365+
);
1366+
1367+
consoleWarnSpy.mockClear();
1368+
1369+
// Test with supported canary version
1370+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1-canary.1');
1371+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
1372+
1373+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1374+
1375+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(
1376+
expect.stringContaining('The configured `useRunAfterProductionCompileHook` option is not compatible'),
1377+
);
1378+
1379+
consoleWarnSpy.mockRestore();
1380+
});
1381+
1382+
it('handles edge case when Next.js version is undefined', () => {
1383+
delete process.env.TURBOPACK; // Ensure webpack mode
1384+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue(undefined);
1385+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1386+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1387+
1388+
const sentryOptions = {
1389+
useRunAfterProductionCompileHook: true,
1390+
};
1391+
1392+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1393+
1394+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1395+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1396+
);
1397+
1398+
consoleWarnSpy.mockRestore();
1399+
});
1400+
1401+
it('handles edge case when Next.js version is empty string', () => {
1402+
delete process.env.TURBOPACK; // Ensure webpack mode
1403+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('');
1404+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1405+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1406+
1407+
const sentryOptions = {
1408+
useRunAfterProductionCompileHook: true,
1409+
};
1410+
1411+
materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1412+
1413+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1414+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1415+
);
1416+
1417+
consoleWarnSpy.mockRestore();
1418+
});
1419+
1420+
it('works correctly with other sentry options present', () => {
1421+
delete process.env.TURBOPACK; // Ensure webpack mode
1422+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.0'); // Unsupported version
1423+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);
1424+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1425+
1426+
const sentryOptions = {
1427+
useRunAfterProductionCompileHook: true,
1428+
debug: true,
1429+
sourcemaps: {
1430+
disable: false,
1431+
},
1432+
tunnelRoute: '/tunnel',
1433+
};
1434+
1435+
const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
1436+
1437+
expect(consoleWarnSpy).toHaveBeenCalledWith(
1438+
'[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic.',
1439+
);
1440+
1441+
// Ensure other functionality still works (tunnel route creates rewrites function)
1442+
expect(finalConfig.rewrites).toBeInstanceOf(Function);
1443+
// Release name should be set (from git or environment)
1444+
expect(finalConfig.env).toHaveProperty('_sentryRelease');
1445+
1446+
consoleWarnSpy.mockRestore();
1447+
});
1448+
});
11761449
});

0 commit comments

Comments
 (0)