Skip to content

Commit

Permalink
fix(nextjs): Add sentry-cli existence check for enabling webpack pl…
Browse files Browse the repository at this point in the history
…ugin (#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.
  • Loading branch information
lobsterkatie committed Dec 23, 2021
1 parent 539b026 commit 2f2d099
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,17 @@ export function constructWebpackConfigFunction(
newConfig.entry = async () => addSentryToEntryProperty(origEntryProperty, buildContext);

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

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

return { ...defaultPluginOptions, ...userPluginOptions };
}

function ensureCLIBinaryExists(): boolean {
return fs.existsSync(path.join(require.resolve('@sentry/cli'), '../../sentry-cli'));
}

0 comments on commit 2f2d099

Please sign in to comment.