diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index ca8f31877def..22f903048d47 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -55,10 +55,11 @@ "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 && 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": "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", "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..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,9 +156,13 @@ 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, { 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..0125b3a49240 100644 --- a/packages/nextjs/src/utils/instrumentServer.ts +++ b/packages/nextjs/src/utils/instrumentServer.ts @@ -5,6 +5,7 @@ import { fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils' 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 +115,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