From 283509420e9fe8e71aa349bca1451f451d2942a3 Mon Sep 17 00:00:00 2001 From: Randolf J <34705014+jrandolf@users.noreply.github.com> Date: Mon, 27 Jan 2025 01:00:30 -0800 Subject: [PATCH] fix(v8/core): Pass `module` into `loadModule` (#15139) The `loadModule` function currently utilizes `require` to load modules starting from the `@sentry/core` directory. This approach is incompatible with package managers like pnpm, which do not hoist dependencies. Consequently, when another module, such as @sentry/nextjs, invokes `loadModule`, it fails to locate its own dependencies because the search initiates within the @sentry/core tree. --- packages/core/src/utils-hoist/node.ts | 10 ++++++---- packages/nextjs/src/config/webpack.ts | 2 +- packages/remix/src/utils/instrumentServer.ts | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/core/src/utils-hoist/node.ts b/packages/core/src/utils-hoist/node.ts index 3805248bdedd..489a5c2cb57d 100644 --- a/packages/core/src/utils-hoist/node.ts +++ b/packages/core/src/utils-hoist/node.ts @@ -42,14 +42,16 @@ export function dynamicRequire(mod: any, request: string): any { * That is to mimic the behavior of `require.resolve` exactly. * * @param moduleName module name to require + * @param existingModule module to use for requiring * @returns possibly required module */ -export function loadModule(moduleName: string): T | undefined { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function loadModule(moduleName: string, existingModule: any = module): T | undefined { let mod: T | undefined; try { // eslint-disable-next-line deprecation/deprecation - mod = dynamicRequire(module, moduleName); + mod = dynamicRequire(existingModule, moduleName); } catch (e) { // no-empty } @@ -57,9 +59,9 @@ export function loadModule(moduleName: string): T | undefined { if (!mod) { try { // eslint-disable-next-line deprecation/deprecation - const { cwd } = dynamicRequire(module, 'process'); + const { cwd } = dynamicRequire(existingModule, 'process'); // eslint-disable-next-line deprecation/deprecation - mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T; + mod = dynamicRequire(existingModule, `${cwd()}/node_modules/${moduleName}`) as T; } catch (e) { // no-empty } diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index d8a29d7d025c..616e5fa08351 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -332,7 +332,7 @@ export function constructWebpackConfigFunction( // Symbolication for dev-mode errors is done elsewhere. if (!isDev) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin') ?? {}; + const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin', module) ?? {}; if (sentryWebpackPlugin) { if (!userSentryOptions.sourcemaps?.disable) { diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 1d67403209bc..8a51582eda21 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -448,7 +448,7 @@ const makeWrappedCreateRequestHandler = (options: RemixOptions) => export function instrumentServer(options: RemixOptions): void { const pkg = loadModule<{ createRequestHandler: CreateRequestHandlerFunction; - }>('@remix-run/server-runtime'); + }>('@remix-run/server-runtime', module); if (!pkg) { DEBUG_BUILD && logger.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');