Skip to content

Commit ed2b201

Browse files
author
Luca Forstner
authored
fix(nextjs): Download CLI binary if it can't be found (#9584)
1 parent 5698094 commit ed2b201

File tree

3 files changed

+100
-36
lines changed

3 files changed

+100
-36
lines changed

packages/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@sentry/types": "7.80.1",
3333
"@sentry/utils": "7.80.1",
3434
"@sentry/vercel-edge": "7.80.1",
35-
"@sentry/webpack-plugin": "1.20.0",
35+
"@sentry/webpack-plugin": "1.21.0",
3636
"chalk": "3.0.0",
3737
"resolve": "1.22.8",
3838
"rollup": "2.78.0",

packages/nextjs/src/config/webpack.ts

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as chalk from 'chalk';
77
import * as fs from 'fs';
88
import * as path from 'path';
99
import { sync as resolveSync } from 'resolve';
10+
import type { Compiler } from 'webpack';
1011

1112
import type { VercelCronsConfig } from '../common/types';
1213
// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our
@@ -22,6 +23,7 @@ import type {
2223
WebpackConfigObjectWithModuleRules,
2324
WebpackEntryProperty,
2425
WebpackModuleRule,
26+
WebpackPluginInstance,
2527
} from './types';
2628

2729
const RUNTIME_TO_SDK_ENTRYPOINT_MAP = {
@@ -35,8 +37,8 @@ const RUNTIME_TO_SDK_ENTRYPOINT_MAP = {
3537
let showedMissingAuthTokenErrorMsg = false;
3638
let showedMissingOrgSlugErrorMsg = false;
3739
let showedMissingProjectSlugErrorMsg = false;
38-
let showedMissingCLiBinaryErrorMsg = false;
3940
let showedHiddenSourceMapsWarningMsg = false;
41+
let showedMissingCliBinaryWarningMsg = false;
4042

4143
// TODO: merge default SentryWebpackPlugin ignore with their SentryWebpackPlugin ignore or ignoreFile
4244
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
@@ -376,6 +378,7 @@ export function constructWebpackConfigFunction(
376378
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
377379
if (SentryWebpackPlugin) {
378380
newConfig.plugins = newConfig.plugins || [];
381+
newConfig.plugins.push(new SentryCliDownloadPlugin());
379382
newConfig.plugins.push(
380383
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
381384
new SentryWebpackPlugin(
@@ -739,6 +742,19 @@ export function getWebpackPluginOptions(
739742
if (err) {
740743
const errorMessagePrefix = `${chalk.red('error')} -`;
741744

745+
if (err.message.includes('ENOENT')) {
746+
if (!showedMissingCliBinaryWarningMsg) {
747+
// eslint-disable-next-line no-console
748+
console.error(
749+
`\n${errorMessagePrefix} ${chalk.bold(
750+
'The Sentry binary to upload sourcemaps could not be found.',
751+
)} Source maps will not be uploaded. Please check that post-install scripts are enabled in your package manager when installing your dependencies and please run your build once without any caching to avoid caching issues of dependencies.\n`,
752+
);
753+
showedMissingCliBinaryWarningMsg = true;
754+
}
755+
return;
756+
}
757+
742758
// Hardcoded way to check for missing auth token until we have a better way of doing this.
743759
if (err.message.includes('Authentication credentials were not provided.')) {
744760
let msg;
@@ -835,30 +851,6 @@ function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions
835851
const { isServer } = buildContext;
836852
const { disableServerWebpackPlugin, disableClientWebpackPlugin } = userSentryOptions;
837853

838-
/** Non-negotiable */
839-
840-
// This check is necessary because currently, `@sentry/cli` uses a post-install script to download an
841-
// architecture-specific version of the `sentry-cli` binary. If `yarn install`, `npm install`, or `npm ci` are run
842-
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
843-
// try to build their apps.
844-
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
845-
846-
// @ts-expect-error - this exists, the dynamic import just doesn't know it
847-
if (!SentryWebpackPlugin || !SentryWebpackPlugin.cliBinaryExists()) {
848-
if (!showedMissingCLiBinaryErrorMsg) {
849-
// eslint-disable-next-line no-console
850-
console.error(
851-
`${chalk.red('error')} - ${chalk.bold(
852-
'Sentry CLI binary not found.',
853-
)} Source maps will not be uploaded. Please check that postinstall scripts are enabled in your package manager when installing your dependencies and please run your build once without any caching to avoid caching issues of dependencies.\n`,
854-
);
855-
showedMissingCLiBinaryErrorMsg = true;
856-
}
857-
return false;
858-
}
859-
860-
/** User override */
861-
862854
if (isServer && disableServerWebpackPlugin !== undefined) {
863855
return !disableServerWebpackPlugin;
864856
} else if (!isServer && disableClientWebpackPlugin !== undefined) {
@@ -1047,3 +1039,54 @@ function getRequestAsyncStorageModuleLocation(
10471039

10481040
return undefined;
10491041
}
1042+
1043+
let downloadingCliAttempted = false;
1044+
1045+
class SentryCliDownloadPlugin implements WebpackPluginInstance {
1046+
public apply(compiler: Compiler): void {
1047+
compiler.hooks.beforeRun.tapAsync('SentryCliDownloadPlugin', (compiler, callback) => {
1048+
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
1049+
if (!SentryWebpackPlugin) {
1050+
// Pretty much an invariant.
1051+
return callback();
1052+
}
1053+
1054+
// @ts-expect-error - this exists, the dynamic import just doesn't know it
1055+
if (SentryWebpackPlugin.cliBinaryExists()) {
1056+
return callback();
1057+
}
1058+
1059+
if (!downloadingCliAttempted) {
1060+
downloadingCliAttempted = true;
1061+
// eslint-disable-next-line no-console
1062+
console.log(
1063+
`\n${chalk.cyan('info')} - ${chalk.bold(
1064+
'Sentry binary to upload source maps not found.',
1065+
)} Package manager post-install scripts are likely disabled or there is a caching issue. Manually downloading instead...`,
1066+
);
1067+
1068+
// @ts-expect-error - this exists, the dynamic import just doesn't know it
1069+
const cliDownloadPromise: Promise<void> = SentryWebpackPlugin.downloadCliBinary({
1070+
log: () => {
1071+
// No logs from directly from CLI
1072+
},
1073+
});
1074+
1075+
cliDownloadPromise.then(
1076+
() => {
1077+
// eslint-disable-next-line no-console
1078+
console.log(`${chalk.cyan('info')} - Sentry binary was successfully downloaded.\n`);
1079+
return callback();
1080+
},
1081+
e => {
1082+
// eslint-disable-next-line no-console
1083+
console.error(`${chalk.red('error')} - Sentry binary download failed:`, e);
1084+
return callback();
1085+
},
1086+
);
1087+
} else {
1088+
return callback();
1089+
}
1090+
});
1091+
}
1092+
}

yarn.lock

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5067,7 +5067,7 @@
50675067
magic-string "0.27.0"
50685068
unplugin "1.0.1"
50695069

5070-
"@sentry/cli@^1.74.4", "@sentry/cli@^1.74.6":
5070+
"@sentry/cli@^1.74.4":
50715071
version "1.74.6"
50725072
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.74.6.tgz#c4f276e52c6f5e8c8d692845a965988068ebc6f5"
50735073
integrity sha512-pJ7JJgozyjKZSTjOGi86chIngZMLUlYt2HOog+OJn+WGvqEkVymu8m462j1DiXAnex9NspB4zLLNuZ/R6rTQHg==
@@ -5080,6 +5080,18 @@
50805080
proxy-from-env "^1.1.0"
50815081
which "^2.0.2"
50825082

5083+
"@sentry/cli@^1.77.1":
5084+
version "1.77.1"
5085+
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.77.1.tgz#ebcf884712ef6c3c75443f491ec16f6a22148aec"
5086+
integrity sha512-OtJ7U9LeuPUAY/xow9wwcjM9w42IJIpDtClTKI/RliE685vd/OJUIpiAvebHNthDYpQynvwb/0iuF4fonh+CKw==
5087+
dependencies:
5088+
https-proxy-agent "^5.0.0"
5089+
mkdirp "^0.5.5"
5090+
node-fetch "^2.6.7"
5091+
progress "^2.0.3"
5092+
proxy-from-env "^1.1.0"
5093+
which "^2.0.2"
5094+
50835095
"@sentry/cli@^2.17.0":
50845096
version "2.17.0"
50855097
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-2.17.0.tgz#fc809ecd721eb5323502625fa904b786af28ad89"
@@ -5124,12 +5136,12 @@
51245136
dependencies:
51255137
"@sentry/cli" "^1.74.4"
51265138

5127-
"@sentry/webpack-plugin@1.20.0":
5128-
version "1.20.0"
5129-
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.20.0.tgz#e7add76122708fb6b4ee7951294b521019720e58"
5130-
integrity sha512-Ssj1mJVFsfU6vMCOM2d+h+KQR7QHSfeIP16t4l20Uq/neqWXZUQ2yvQfe4S3BjdbJXz/X4Rw8Hfy1Sd0ocunYw==
5139+
"@sentry/webpack-plugin@1.21.0":
5140+
version "1.21.0"
5141+
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.21.0.tgz#bbe7cb293751f80246a4a56f9a7dd6de00f14b58"
5142+
integrity sha512-x0PYIMWcsTauqxgl7vWUY6sANl+XGKtx7DCVnnY7aOIIlIna0jChTAPANTfA2QrK+VK+4I/4JxatCEZBnXh3Og==
51315143
dependencies:
5132-
"@sentry/cli" "^1.74.6"
5144+
"@sentry/cli" "^1.77.1"
51335145
webpack-sources "^2.0.0 || ^3.0.0"
51345146

51355147
"@sideway/address@^4.1.3":
@@ -8290,10 +8302,10 @@ aws4@^1.8.0:
82908302
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
82918303
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==
82928304

8293-
axios@1.3.4, axios@^1.2.2:
8294-
version "1.3.4"
8295-
resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.4.tgz#f5760cefd9cfb51fd2481acf88c05f67c4523024"
8296-
integrity sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==
8305+
axios@1.6.0:
8306+
version "1.6.0"
8307+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.0.tgz#f1e5292f26b2fd5c2e66876adc5b06cdbd7d2102"
8308+
integrity sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==
82978309
dependencies:
82988310
follow-redirects "^1.15.0"
82998311
form-data "^4.0.0"
@@ -8316,6 +8328,15 @@ axios@^1.0.0:
83168328
form-data "^4.0.0"
83178329
proxy-from-env "^1.1.0"
83188330

8331+
axios@^1.2.2:
8332+
version "1.3.4"
8333+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.4.tgz#f5760cefd9cfb51fd2481acf88c05f67c4523024"
8334+
integrity sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==
8335+
dependencies:
8336+
follow-redirects "^1.15.0"
8337+
form-data "^4.0.0"
8338+
proxy-from-env "^1.1.0"
8339+
83198340
b4a@^1.6.4:
83208341
version "1.6.4"
83218342
resolved "https://registry.yarnpkg.com/b4a/-/b4a-1.6.4.tgz#ef1c1422cae5ce6535ec191baeed7567443f36c9"

0 commit comments

Comments
 (0)