Skip to content

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

Closed
@Saegusae

Description

@Saegusae

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions