Skip to content

Custom path options for SENTRY_SERVER_INIT_PATH to use in monorepo environments. #3723

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

Closed
4 of 5 tasks
Saegusae opened this issue Jun 21, 2021 · 6 comments · Fixed by #3786
Closed
4 of 5 tasks

Custom path options for SENTRY_SERVER_INIT_PATH to use in monorepo environments. #3723

Saegusae opened this issue Jun 21, 2021 · 6 comments · Fixed by #3786
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Type: Improvement

Comments

@Saegusae
Copy link

Saegusae commented Jun 21, 2021

Package + Version

  • @sentry/nextjs

Version:

6.7.1

Description

When using @sentry/nextjs in a @nrwl/nx monorepo workspace environment, the output path is applied to .env.local file relative to the "application" path, which resolves to this in development environment (as dist folder is in workspace root but app source is in apps/<app-name> directory):

SENTRY_SERVER_INIT_PATH=..\..\dist\apps\<app-name>\.next\server\sentry\initServerSDK.js

Which is correct as it gets the output from the webpack config. But in the nx workspace environment, the serve and build commands must be executed from the workspace root and not the next app root, so this relative path becomes wrong for both the development server and the production build pipeline (as in most cases only the built artifacts are cloned over).

So this error is thrown:

[Sentry] Could not initialize SDK. Received error:
Error: Cannot find module 'C:\Users\<username>\Desktop\dist\apps\<app-name>\.next\server\sentry\initServerSDK.js'
Require stack:
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@sentry\nextjs\dist\utils\instrumentServer.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@sentry\nextjs\dist\index.server.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\apps\promocodes.com\next.config.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\next\dist\next-server\server\config.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@nrwl\next\src\utils\config.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@nrwl\next\src\executors\server\server.impl.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@nrwl\tao\src\shared\workspace.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@nrwl\tao\src\commands\run.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@nrwl\tao\index.js
- C:\Users\<username>\Desktop\Dev\<workspace-name>\node_modules\@nrwl\cli\lib\run-cli.js

Note: I have also tried providing this environment variable in the next.config.js, it still defaults to loading the .env.local file, which gets overwritten on every serve or build command execution.

Proposed Solution

I confirmed if I run a production server after removing the double back directories from .env.local file, the SDK boots up correctly and is initialized. So I could probably get around this in the CI pipeline by monkey patching the file, or just adding a DefinePlugin for that env variable to the nextjs webpack config, but that would not be a very good solution. As for my proposed solution, there should be an option to define SENTRY_SERVER_INIT_PATH with it defaulting to self-overwriting .env.local as the @sentry/nextjs config does at the moment.

@shertu
Copy link

shertu commented Jun 23, 2021

Is this issue related to this error message I am getting?

image

@Saegusae
Copy link
Author

Saegusae commented Jun 23, 2021

I suspect this error gets thrown when Sentry's self executing function (that runs when withSentryConfig is imported in the next.config.js file) can not read the environment variable SENTRY_SERVER_INIT_PATH. So it's most likely related to the issue.

@tmilewski
Copy link

I'm running into the same issue with Nx. If I set the ENV var, it works... but it's overwritten and fails the second time around.

@tmilewski
Copy link

@Saegusae Not ideal, but I did this at the top of my next.config.js file:

process.env['SENTRY_SERVER_INIT_PATH'] = `./dist/apps/${process.env.PROJECT_NAME}/.next/server/sentry/initServerSDK.js`;`

@Saegusae
Copy link
Author

@Saegusae Not ideal, but I did this at the top of my next.config.js file:

process.env['SENTRY_SERVER_INIT_PATH'] = `./dist/apps/${process.env.PROJECT_NAME}/.next/server/sentry/initServerSDK.js`;`

@tmilewski yeah, i did something similar by injecting the env var before the run script. We should at least be able to stop sentry for injecting code and throwing errors on development environment (since it's not required on the local machine dev server).

@lobsterkatie
Copy link
Member

@tmilewski @Saegusae @shertu This should be fixed in 6.9.0, as we switched our approach and no longer are using the env file or env variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Type: Improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants