Nextjs "server Sentry.Init()" is never called if `npm run build` happened in different environment than hosting
See original GitHub issue- Review the documentation: https://docs.sentry.io/
- Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
- Use the latest release: https://github.com/getsentry/sentry-javascript/releases
- Provide a link to the affected event from your Sentry account
Package + Version
-
@sentry/browser
-
@sentry/node
-
raven-js
-
raven-node
(raven for node) - other:
@sentry/nextjs
Version:
6.5.1
Description
I use Github Actions to build my nextjs application, then deploy artifacts to a NodeJS Server hosted elsewhere. https://nextjs.org/docs/deployment#nodejs-server
During npm run build
@sentry/nextjs creates a file .env.local and sets an environment variable SENTRY_SERVER_INIT_PATH. The value of said variable is an absolute path i.e. C:\some_path_to_project\.next\server\sentry\initServerSDK.js
During npm run start
process.env.SENTRY_SERVER_INIT_PATH is used to require initServerSDK.js. However, npm run start
is called after artifacts have been uploaded to a different environment running a NodeJS server. Because of this, the absolute path of process.env.SENTRY_SERVER_INIT_PATH is no longer valid.
initServerSDK.js cannot be found and Sentry.Init() is never called - serverside logging does not happen.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Thank you for a great reply @AbhiPrasad - and for providing references.
However, from reading next-server.ts, my understanding is that next.js is already handling those scenarios (path locations, distDir, serverless) when they expose .serverBuildDir. If we use the property which next.js has already exposed, my feeling is that this would be the most safe option. This means that we have to make zero assumptions and give next.js 100% responsibility for telling us where the directory is.
If they add more options in the future, I would expect .serverBuildDir to reflect this.
You can see their current implementation here: https://github.com/vercel/next.js/blob/0402fc459eb12a775372172a351f3a3f4fee83e3/packages/next/next-server/server/next-server.ts#L243-L246
I case this makes sense, I would gladly dedicate some time to creating a PR 😃
@AbhiPrasad Looking through the code, I am a bit curious why you have chosen a 2 step model (create/consume) which creates a disk file dependency (.env.local).
Could instrumentServer.ts not simply do this:
With this solution you would not need to create .env.local during build and could remove some complexity 😃