Skip to content

Commit dcac79e

Browse files
committed
fix next version in dev symbolication
1 parent f307a22 commit dcac79e

File tree

6 files changed

+562
-15
lines changed

6 files changed

+562
-15
lines changed

packages/nextjs/src/common/devErrorSymbolicationEventProcessor.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ type OriginalStackFrameResponse = {
1212

1313
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
1414
_sentryBasePath?: string;
15-
next?: {
16-
version?: string;
17-
};
15+
_sentryNextJsVersion: string | undefined;
1816
};
1917

2018
/**
@@ -39,9 +37,15 @@ export async function devErrorSymbolicationEventProcessor(event: Event, hint: Ev
3937
try {
4038
if (hint.originalException && hint.originalException instanceof Error && hint.originalException.stack) {
4139
const frames = stackTraceParser.parse(hint.originalException.stack);
40+
const nextJsVersion = globalWithInjectedValues._sentryNextJsVersion;
41+
42+
// If we for whatever reason don't have a Next.js version,
43+
// we don't want to symbolicate as this previously lead to infinite loops
44+
if (!nextJsVersion) {
45+
return event;
46+
}
4247

43-
const nextjsVersion = globalWithInjectedValues.next?.version || '0.0.0';
44-
const parsedNextjsVersion = nextjsVersion ? parseSemver(nextjsVersion) : {};
48+
const parsedNextjsVersion = parseSemver(nextJsVersion);
4549

4650
let resolvedFrames: ({
4751
originalCodeFrame: string | null;

packages/nextjs/src/config/turbopack/constructTurbopackConfig.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import type { NextConfigObject, TurbopackOptions, TurbopackRuleConfigItemOrShort
1414
export function constructTurbopackConfig({
1515
userNextConfig,
1616
routeManifest,
17+
nextJsVersion,
1718
}: {
1819
userNextConfig: NextConfigObject;
1920
routeManifest?: RouteManifest;
21+
nextJsVersion?: string;
2022
}): TurbopackOptions {
2123
const newConfig: TurbopackOptions = {
2224
...userNextConfig.turbopack,
@@ -40,6 +42,24 @@ export function constructTurbopackConfig({
4042
});
4143
}
4244

45+
if (nextJsVersion) {
46+
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
47+
matcher: '**/instrumentation.*',
48+
rule: {
49+
loaders: [
50+
{
51+
loader: path.resolve(__dirname, '..', 'loaders', 'valueInjectionLoader.js'),
52+
options: {
53+
values: {
54+
_sentryNextJsVersion: nextJsVersion,
55+
},
56+
},
57+
},
58+
],
59+
},
60+
});
61+
}
62+
4363
return newConfig;
4464
}
4565

packages/nextjs/src/config/webpack.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function constructWebpackConfigFunction(
4545
userSentryOptions: SentryBuildOptions = {},
4646
releaseName: string | undefined,
4747
routeManifest: RouteManifest | undefined,
48+
nextJsVersion: string | undefined,
4849
): WebpackConfigFunction {
4950
// Will be called by nextjs and passed its default webpack configuration and context data about the build (whether
5051
// we're building server or client, whether we're in dev, what version of webpack we're using, etc). Note that
@@ -90,7 +91,15 @@ export function constructWebpackConfigFunction(
9091
const newConfig = setUpModuleRules(rawNewConfig);
9192

9293
// Add a loader which will inject code that sets global values
93-
addValueInjectionLoader(newConfig, userNextConfig, userSentryOptions, buildContext, releaseName, routeManifest);
94+
addValueInjectionLoader({
95+
newConfig,
96+
userNextConfig,
97+
userSentryOptions,
98+
buildContext,
99+
releaseName,
100+
routeManifest,
101+
nextJsVersion,
102+
});
94103

95104
addOtelWarningIgnoreRule(newConfig);
96105

@@ -682,14 +691,23 @@ function setUpModuleRules(newConfig: WebpackConfigObject): WebpackConfigObjectWi
682691
*/
683692
// TODO: Remove this loader and replace it with a nextConfig.env (https://web.archive.org/web/20240917153554/https://nextjs.org/docs/app/api-reference/next-config-js/env) or define based (https://github.com/vercel/next.js/discussions/71476) approach.
684693
// In order to remove this loader though we need to make sure the minimum supported Next.js version includes this PR (https://github.com/vercel/next.js/pull/61194), otherwise the nextConfig.env based approach will not work, as our SDK code is not processed by Next.js.
685-
function addValueInjectionLoader(
686-
newConfig: WebpackConfigObjectWithModuleRules,
687-
userNextConfig: NextConfigObject,
688-
userSentryOptions: SentryBuildOptions,
689-
buildContext: BuildContext,
690-
releaseName: string | undefined,
691-
routeManifest: RouteManifest | undefined,
692-
): void {
694+
function addValueInjectionLoader({
695+
newConfig,
696+
userNextConfig,
697+
userSentryOptions,
698+
buildContext,
699+
releaseName,
700+
routeManifest,
701+
nextJsVersion,
702+
}: {
703+
newConfig: WebpackConfigObjectWithModuleRules;
704+
userNextConfig: NextConfigObject;
705+
userSentryOptions: SentryBuildOptions;
706+
buildContext: BuildContext;
707+
releaseName: string | undefined;
708+
routeManifest: RouteManifest | undefined;
709+
nextJsVersion: string | undefined;
710+
}): void {
693711
const assetPrefix = userNextConfig.assetPrefix || userNextConfig.basePath || '';
694712

695713
// Check if release creation is disabled to prevent injection that breaks build determinism
@@ -717,6 +735,7 @@ function addValueInjectionLoader(
717735
// Make sure that if we have a windows path, the backslashes are interpreted as such (rather than as escape
718736
// characters)
719737
_sentryRewriteFramesDistDir: userNextConfig.distDir?.replace(/\\/g, '\\\\') || '.next',
738+
_sentryNextJsVersion: nextJsVersion || '0.0.0',
720739
};
721740

722741
const clientValues = {

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,19 @@ function getFinalConfigObject(
314314
webpack:
315315
isTurbopack || userSentryOptions.disableSentryWebpackConfig
316316
? incomingUserNextConfigObject.webpack // just return the original webpack config
317-
: constructWebpackConfigFunction(incomingUserNextConfigObject, userSentryOptions, releaseName, routeManifest),
317+
: constructWebpackConfigFunction(
318+
incomingUserNextConfigObject,
319+
userSentryOptions,
320+
releaseName,
321+
routeManifest,
322+
nextJsVersion,
323+
),
318324
...(isTurbopackSupported && isTurbopack
319325
? {
320326
turbopack: constructTurbopackConfig({
321327
userNextConfig: incomingUserNextConfigObject,
322328
routeManifest,
329+
nextJsVersion,
323330
}),
324331
}
325332
: {}),

0 commit comments

Comments
 (0)