From f37baee6929a12c1e4a4e41d046496cad5a0670c Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 4 Jun 2021 11:22:32 -0400 Subject: [PATCH 1/3] fix(nextjs): Use correct abs path for server init closes #3648 --- packages/nextjs/package.json | 6 +- packages/nextjs/src/utils/config.ts | 5 +- packages/nextjs/src/utils/instrumentServer.ts | 10 +- packages/nextjs/test/integration/yarn.lock | 152 +++++++++--------- 4 files changed, 90 insertions(+), 83 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index ca8f31877def..7486bb82b665 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -56,9 +56,9 @@ "test:watch": "jest --watch", "test:unit": "jest", "test:integration": "run-s test:integration:build test:integration:server test:integration:client", - "test:integration:build": "cd test/integration && yarn && yarn build && cd ../..", - "test:integration:server": "node test/integration/test/server.js --silent", - "test:integration:client": "node test/integration/test/client.js --silent", + "test:integration:build": "cd test/integration && rm -rf node_modules .next .env.local && yarn && yarn build", + "test:integration:server": "cd test/integration && node test/server.js --silent", + "test:integration:client": "cd test/integration && node test/client.js --silent", "pack": "npm pack", "vercel:branch": "source vercel/set-up-branch-for-test-app-use.sh", "vercel:project": "source vercel/make-project-use-current-branch.sh" diff --git a/packages/nextjs/src/utils/config.ts b/packages/nextjs/src/utils/config.ts index 0cc240facd34..7aae7e79043e 100644 --- a/packages/nextjs/src/utils/config.ts +++ b/packages/nextjs/src/utils/config.ts @@ -158,7 +158,10 @@ export function withSentryConfig( if (config.target === 'node') { const serverSDKInitOutputPath = path.join(config.output.path, SERVER_SDK_INIT_PATH); const projectDir = config.context; - setRuntimeEnvVars(projectDir, { SENTRY_SERVER_INIT_PATH: serverSDKInitOutputPath }); + setRuntimeEnvVars(projectDir, { + // ex: .next/server/sentry/initServerSdk.js + SENTRY_SERVER_INIT_PATH: path.relative(projectDir, serverSDKInitOutputPath), + }); } let newConfig = config; diff --git a/packages/nextjs/src/utils/instrumentServer.ts b/packages/nextjs/src/utils/instrumentServer.ts index a591147b3b82..a4ae67386170 100644 --- a/packages/nextjs/src/utils/instrumentServer.ts +++ b/packages/nextjs/src/utils/instrumentServer.ts @@ -2,9 +2,11 @@ import { deepReadDirSync } from '@sentry/node'; import { extractTraceparentData, getActiveTransaction, hasTracingEnabled } from '@sentry/tracing'; import { Event as SentryEvent } from '@sentry/types'; import { fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils'; +// import * as assert from 'assert'; import * as domain from 'domain'; import * as http from 'http'; import { default as createNextServer } from 'next'; +import * as path from 'path'; import * as querystring from 'querystring'; import * as url from 'url'; @@ -114,11 +116,13 @@ function makeWrappedHandlerGetter(origHandlerGetter: HandlerGetter): WrappedHand try { // `SENTRY_SERVER_INIT_PATH` is set at build time, and points to a webpack-processed version of the user's // `sentry.server.config.js`. Requiring it starts the SDK. - require(process.env.SENTRY_SERVER_INIT_PATH as string); + require(path.resolve(process.env.SENTRY_SERVER_INIT_PATH as string)); } catch (err) { // Log the error but don't bail - we still want the wrapping to happen, in case the user is doing something weird - // and manually calling `Sentry.init()` somewhere else. - logger.error(`[Sentry] Could not initialize SDK. Received error:\n${err}`); + // and manually calling `Sentry.init()` somewhere else. We log to console instead of using logger from utils + // because Sentry is not initialized. + // eslint-disable-next-line no-console + console.error(`[Sentry] Could not initialize SDK. Received error:\n${err}`); } // stash this in the closure so that `makeWrappedReqHandler` can use it diff --git a/packages/nextjs/test/integration/yarn.lock b/packages/nextjs/test/integration/yarn.lock index 8cad483824df..f6024b8ec1ab 100644 --- a/packages/nextjs/test/integration/yarn.lock +++ b/packages/nextjs/test/integration/yarn.lock @@ -103,14 +103,14 @@ resolved "https://registry.yarnpkg.com/@opentelemetry/context-base/-/context-base-0.14.0.tgz#c67fc20a4d891447ca1a855d7d70fa79a3533001" integrity sha512-sDOAZcYwynHFTbLo6n8kIbLiVF3a3BLkrmehJUyEbT9F+Smbi47kLGS2gG2g0fjBLR/Lr1InPD7kXL7FaTqEkw== -"@sentry/browser@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.5.0.tgz#2382493691c3fac5d8b652ae46f09f1b29d288ef" - integrity sha512-n1e8hNKwuVP4bLqRK5J0DHFqnnnrbv6h6+Bc1eNRbf32/e6eZ3Cb36PTplqDCxwnMnnIEEowd5F4ZWeTLPPY3A== - dependencies: - "@sentry/core" "6.5.0" - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" +"@sentry/browser@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.5.1.tgz#9a6ed5607b3b0f4e83f38720e3e202906f8c5bdb" + integrity sha512-iVLCdEFwsoWAzE/hNknexPQjjDpMQV7mmaq9Z1P63bD6MfhwVTx4hG4pHn8HEvC38VvCVf1wv0v/LxtoODAYXg== + dependencies: + "@sentry/core" "6.5.1" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" tslib "^1.9.3" "@sentry/cli@^1.63.1": @@ -125,106 +125,106 @@ progress "^2.0.3" proxy-from-env "^1.1.0" -"@sentry/core@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.5.0.tgz#03ecbad7845b31f03a84eddf4884877c999bb6be" - integrity sha512-Hx/WvhM5bXcXqfIiz+505TjYYfPjQ8mrxby/EWl+L7dYUCyI/W6IZKTc/MoHlLuM+JPUW9c1bw/97TzbgTzaAA== +"@sentry/core@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.5.1.tgz#c8b6c3ed86ed07b193c95d599c1b9a4a161e500e" + integrity sha512-Mh3sl/iUOT1myHmM6RlDy2ARzkUClx/g4DAt1rJ/IpQBOlDYQraplXSIW80i/hzRgQDfwhwgf4wUa5DicKBjKw== dependencies: - "@sentry/hub" "6.5.0" - "@sentry/minimal" "6.5.0" - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" + "@sentry/hub" "6.5.1" + "@sentry/minimal" "6.5.1" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" tslib "^1.9.3" -"@sentry/hub@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-6.5.0.tgz#ad3c9bcf83050ea217f3c25cc625e6b447f1d9d7" - integrity sha512-vEChnLoozOJzEJoTUvaAsK/n7IHoQFx8P1TzQmnR+8XGZJZmGHG6bBXUH0iS2a9hhR1WkoEBeiL+t96R9uyf0A== +"@sentry/hub@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-6.5.1.tgz#135ef09d07d32e87a53f664c0ae8fcc4f5963519" + integrity sha512-lBRMBVMYP8B4PfRiM70murbtJAXiIAao/asDEMIRNGMP6pI2ArqXfJCBYDkStukhikYD0Kqb4trXq+JYF07Hbg== dependencies: - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" tslib "^1.9.3" -"@sentry/integrations@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-6.5.0.tgz#b97702f5d5e5456b9fae225b60141bd014671662" - integrity sha512-pI8DESNTbsj60CDtLzIdLHex59NGzjTBR9Wpt7SG8NPhgEAuS3tUU4Thjyib7sGb7mxRw1sSQt/FsjDd7vMjLg== +"@sentry/integrations@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-6.5.1.tgz#e7016f3023a98e2ef893daba18ba74bd8d62654b" + integrity sha512-NYiW0rH7fwv7aRtrRnfCSIiwulfV2NoLjhmghCONsyo10DNtYmOpogLotCytZFWLDnTJW1+pmTomq8UW/OSTcQ== dependencies: - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" localforage "^1.8.1" tslib "^1.9.3" -"@sentry/minimal@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.5.0.tgz#aa89b8e24c88aa85c99ef64e0b460497c90133f9" - integrity sha512-MT83ONaBhTCFUlDIQFpsG/lq3ZjGK7jwQ10qxGadSg1KW6EvtQRg+OBwULeQ7C+nNEAhseNrC/qomZMT8brncg== +"@sentry/minimal@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.5.1.tgz#b8c1b382c2ea788eec3d32d203e5081b00eb6838" + integrity sha512-q9Do/oreu1RP695CXCLowVDuQyk7ilE6FGdz2QLpTXAfx8247qOwk6+zy9Kea/Djk93+BoSDVQUSneNiVwl0nQ== dependencies: - "@sentry/hub" "6.5.0" - "@sentry/types" "6.5.0" + "@sentry/hub" "6.5.1" + "@sentry/types" "6.5.1" tslib "^1.9.3" "@sentry/nextjs@file:../..": - version "6.5.0" - dependencies: - "@sentry/core" "6.5.0" - "@sentry/integrations" "6.5.0" - "@sentry/node" "6.5.0" - "@sentry/react" "6.5.0" - "@sentry/tracing" "6.5.0" - "@sentry/utils" "6.5.0" + version "6.5.1" + dependencies: + "@sentry/core" "6.5.1" + "@sentry/integrations" "6.5.1" + "@sentry/node" "6.5.1" + "@sentry/react" "6.5.1" + "@sentry/tracing" "6.5.1" + "@sentry/utils" "6.5.1" "@sentry/webpack-plugin" "1.15.0" tslib "^1.9.3" -"@sentry/node@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/node/-/node-6.5.0.tgz#4c043792bae0b84de7c485fad605fd871be050a5" - integrity sha512-KSypDtc8XPoyMdL1BCdkNokNmEaf+AZhD4HTElmIKHpIyiYvM1bSqiOpVohnwR3E+5qNeVVBPoVjDpHWRaNzQg== +"@sentry/node@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/node/-/node-6.5.1.tgz#a572b380858de5aeaf98eade6d8d3afcba13d364" + integrity sha512-Yh8J/QJ5e8gRBVL9VLCDpUvmiaxsxVZm0CInPHw3V/smgMkrzSKEiqxSeMq8ImPlaJrCFECqdpv4gnvYKI+mQQ== dependencies: - "@sentry/core" "6.5.0" - "@sentry/hub" "6.5.0" - "@sentry/tracing" "6.5.0" - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" + "@sentry/core" "6.5.1" + "@sentry/hub" "6.5.1" + "@sentry/tracing" "6.5.1" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" cookie "^0.4.1" https-proxy-agent "^5.0.0" lru_map "^0.3.3" tslib "^1.9.3" -"@sentry/react@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/react/-/react-6.5.0.tgz#db2067e080cac24da046af24865b82d3567adbf4" - integrity sha512-NyH+v8MwX+nzuhPRGy3+DHSB0es5yaCUNrtAdCtbe8EhERSoYvqAyWIQ+Fp5++PGjfAtYbz0W0IpsjguZbnT2Q== +"@sentry/react@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/react/-/react-6.5.1.tgz#1c7019fc3d8b1168c1ab8936cb65971b314d060d" + integrity sha512-YeGi7FzInhMZQxiy5fKqb7kS6W+u4NfsjzsVV3bLjJ1kiVtbpzZ2gs+ObHqW3zVE622V4nL7A4P8/CBHbcm5PA== dependencies: - "@sentry/browser" "6.5.0" - "@sentry/minimal" "6.5.0" - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" + "@sentry/browser" "6.5.1" + "@sentry/minimal" "6.5.1" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" hoist-non-react-statics "^3.3.2" tslib "^1.9.3" -"@sentry/tracing@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/tracing/-/tracing-6.5.0.tgz#4d8efce53362a820d002838495f0ef446150aefc" - integrity sha512-6jpmYM3Lt4w6dOeK8keGAis722ooLtX5UcPbekkTufXiqKRR5VWg8DLUp7z7oD6h4GLrLbeNtCiH6h20ZW2ggw== +"@sentry/tracing@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/tracing/-/tracing-6.5.1.tgz#a5f3e497d4f1f319f36475df050e135cf65af750" + integrity sha512-y1W/xFC2hAuKqSuuaovkElHY4pbli3XoXrreesg8PtO7ilX6ZbatOQbHsEsHQyoUv0F6aVA+MABOxWH2jt7tfw== dependencies: - "@sentry/hub" "6.5.0" - "@sentry/minimal" "6.5.0" - "@sentry/types" "6.5.0" - "@sentry/utils" "6.5.0" + "@sentry/hub" "6.5.1" + "@sentry/minimal" "6.5.1" + "@sentry/types" "6.5.1" + "@sentry/utils" "6.5.1" tslib "^1.9.3" -"@sentry/types@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.5.0.tgz#2cdb50875bb73d87708b9c0a80d4ca057b3596b5" - integrity sha512-yQpTCIYxBsYT0GenqHNNKeXV8CSkkYlAxB1IGV2eac4IKC5ph5GW6TfDGwvlzQSQ297RsRmOSA8o3I5gGPd2yA== +"@sentry/types@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.5.1.tgz#0a34ecfd1ae9275a416a105640eb4bed45a46a1d" + integrity sha512-b/7a6CMoytaeFPx4IBjfxPw3nPvsQh7ui1C8Vw0LxNNDgBwVhPLzUOWeLWbo5YZCVbGEMIWwtCUQYWxneceZSA== -"@sentry/utils@6.5.0": - version "6.5.0" - resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.5.0.tgz#8722542b9a901623195cffaab5d18ce176c1e459" - integrity sha512-CcHuaQN6vRuAsIC+3sA23NmWLRmUN0x/HNQxk0DHJylvYQdEA0AUNoLXogykaXh6NrCx4DNq9yCQTNTSC3mFxg== +"@sentry/utils@6.5.1": + version "6.5.1" + resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.5.1.tgz#046baf7d1a6564d6d555437ad3674dba9bc0806a" + integrity sha512-Wv86JYGQH+ZJ5XGFQX7h6ijl32667ikenoL9EyXMn8UoOYX/MLwZoQZin1P60wmKkYR9ifTNVmpaI9OoTaH+UQ== dependencies: - "@sentry/types" "6.5.0" + "@sentry/types" "6.5.1" tslib "^1.9.3" "@sentry/webpack-plugin@1.15.0": From c93bab7eabe4ba4e84b56b22367b532d8071da4b Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 4 Jun 2021 11:46:27 -0400 Subject: [PATCH 2/3] ref: Use rimraf --- packages/nextjs/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 7486bb82b665..22f903048d47 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -55,8 +55,9 @@ "test": "run-s test:unit test:integration", "test:watch": "jest --watch", "test:unit": "jest", - "test:integration": "run-s test:integration:build test:integration:server test:integration:client", - "test:integration:build": "cd test/integration && rm -rf node_modules .next .env.local && yarn && yarn build", + "test:integration": "run-s test:integration:clean test:integration:build test:integration:server test:integration:client", + "test:integration:clean": "cd test/integration && rimraf node_modules .next .env.local", + "test:integration:build": "cd test/integration && yarn && yarn build", "test:integration:server": "cd test/integration && node test/server.js --silent", "test:integration:client": "cd test/integration && node test/client.js --silent", "pack": "npm pack", From 0d1c36f41be6c57f4ab3c769128b471fe8f7b793 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 1 Jun 2021 13:59:05 -0700 Subject: [PATCH 3/3] ref: account for output path diff between webpack versions Co-authored-by: Abhijeet Prasad --- packages/nextjs/src/utils/config.ts | 7 ++++--- packages/nextjs/src/utils/instrumentServer.ts | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nextjs/src/utils/config.ts b/packages/nextjs/src/utils/config.ts index 7aae7e79043e..5c5234b92ff6 100644 --- a/packages/nextjs/src/utils/config.ts +++ b/packages/nextjs/src/utils/config.ts @@ -7,7 +7,7 @@ import * as path from 'path'; const SENTRY_CLIENT_CONFIG_FILE = './sentry.client.config.js'; const SENTRY_SERVER_CONFIG_FILE = './sentry.server.config.js'; -// this is where the transpiled/bundled version of `USER_SERVER_CONFIG_FILE` will end up +// this is where the transpiled/bundled version of `SENTRY_SERVER_CONFIG_FILE` will end up export const SERVER_SDK_INIT_PATH = 'sentry/initServerSDK.js'; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -21,7 +21,7 @@ type WebpackConfig = { devtool: string; plugins: PlainObject[]; entry: EntryProperty; - output: { path: string }; + output: { filename: string; path: string }; target: string; context: string; }; @@ -156,7 +156,8 @@ export function withSentryConfig( // if we're building server code, store the webpack output path as an env variable, so we know where to look for the // webpack-processed version of `sentry.server.config.js` when we need it if (config.target === 'node') { - const serverSDKInitOutputPath = path.join(config.output.path, SERVER_SDK_INIT_PATH); + const outputLocation = path.dirname(path.join(config.output.path, config.output.filename)); + const serverSDKInitOutputPath = path.join(outputLocation, SERVER_SDK_INIT_PATH); const projectDir = config.context; setRuntimeEnvVars(projectDir, { // ex: .next/server/sentry/initServerSdk.js diff --git a/packages/nextjs/src/utils/instrumentServer.ts b/packages/nextjs/src/utils/instrumentServer.ts index a4ae67386170..0125b3a49240 100644 --- a/packages/nextjs/src/utils/instrumentServer.ts +++ b/packages/nextjs/src/utils/instrumentServer.ts @@ -2,7 +2,6 @@ import { deepReadDirSync } from '@sentry/node'; import { extractTraceparentData, getActiveTransaction, hasTracingEnabled } from '@sentry/tracing'; import { Event as SentryEvent } from '@sentry/types'; import { fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils'; -// import * as assert from 'assert'; import * as domain from 'domain'; import * as http from 'http'; import { default as createNextServer } from 'next';