Skip to content

Commit 2f2d099

Browse files
authored
fix(nextjs): Add sentry-cli existence check for enabling webpack plugin (#4311)
Our nextjs SDK uses `sentry-cli` (by way of `SentryWebpackPlugin` and `@sentry/cli`) to upload sourcemaps. Because binaries (like the `sentry-cli` executable) need to be compiled differently for different OSs and architectures, `@sentry/cli` uses a post-install script to download the correct one as part of its install process, rather than ship with all possible binaries at once. Of course, this goes awry if the post-install script isn't run, which is exactly what happens when `@sentry/cli` is installed with the `--ignore-scripts` option. The resulting missing binary then causes errors which bubble up to and through our SDK to the nextjs build process, which promptly crashes. This fixes that by checking to make sure they binary has been downloaded before enabling `SentryWebpackPlugin`. Fixes getsentry/sentry-cli#1085.
1 parent 539b026 commit 2f2d099

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@ export function constructWebpackConfigFunction(
6262
newConfig.entry = async () => addSentryToEntryProperty(origEntryProperty, buildContext);
6363

6464
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
65-
const enableWebpackPlugin = buildContext.isServer
66-
? !userNextConfig.sentry?.disableServerWebpackPlugin
67-
: !userNextConfig.sentry?.disableClientWebpackPlugin;
65+
const enableWebpackPlugin =
66+
// TODO: this is a hack to fix https://github.com/getsentry/sentry-cli/issues/1085, which is caused by
67+
// https://github.com/getsentry/sentry-cli/issues/915. Once the latter is addressed, this existence check can come
68+
// out. (The check is necessary because currently, `@sentry/cli` uses a post-install script to download an
69+
// architecture-specific version of the `sentry-cli` binary. If `yarn install`, `npm install`, or `npm ci` are run
70+
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
71+
// try to build their apps.)
72+
ensureCLIBinaryExists() &&
73+
(buildContext.isServer
74+
? !userNextConfig.sentry?.disableServerWebpackPlugin
75+
: !userNextConfig.sentry?.disableClientWebpackPlugin);
6876

6977
if (enableWebpackPlugin) {
7078
// TODO Handle possibility that user is using `SourceMapDevToolPlugin` (see
@@ -296,3 +304,7 @@ export function getWebpackPluginOptions(
296304

297305
return { ...defaultPluginOptions, ...userPluginOptions };
298306
}
307+
308+
function ensureCLIBinaryExists(): boolean {
309+
return fs.existsSync(path.join(require.resolve('@sentry/cli'), '../../sentry-cli'));
310+
}

0 commit comments

Comments
 (0)