Skip to content

fix(nextjs): Use correct abs path for server init #3649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions packages/nextjs/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +21,7 @@ type WebpackConfig = {
devtool: string;
plugins: PlainObject[];
entry: EntryProperty;
output: { path: string };
output: { filename: string; path: string };
target: string;
context: string;
};
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions packages/nextjs/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down