Skip to content

fix(nextjs): Skip re instrumentating on generate phase of experimental build mode #16410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like us to expand this comment to be more specific to what we don't support, or link to a docs page that explains this in more detail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can write something up in the docs and link it here when it's ready; yet i'm not sure how useful it'll be for users. Source maps, tracing, and error handling work as expected for now, but since we inline things differently at compile time than what Next.js does, some issues might still come up and it's just good to flag -if something breaks- that this isn't a fully robust solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having something there is useful because we can add items to it as it comes up, even if nothing is too important atm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, will add it to the docs! Is this a blocker for this PR though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a PR blocker, we can do it as a follow up

);
}
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 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();
Expand Down
25 changes: 25 additions & 0 deletions packages/nextjs/test/config/withSentryConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
});
Loading