Skip to content

Commit 2835094

Browse files
jrandolfLms24
authored andcommitted
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.
1 parent a3ddff6 commit 2835094

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

packages/core/src/utils-hoist/node.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,26 @@ export function dynamicRequire(mod: any, request: string): any {
4242
* That is to mimic the behavior of `require.resolve` exactly.
4343
*
4444
* @param moduleName module name to require
45+
* @param existingModule module to use for requiring
4546
* @returns possibly required module
4647
*/
47-
export function loadModule<T>(moduleName: string): T | undefined {
48+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49+
export function loadModule<T>(moduleName: string, existingModule: any = module): T | undefined {
4850
let mod: T | undefined;
4951

5052
try {
5153
// eslint-disable-next-line deprecation/deprecation
52-
mod = dynamicRequire(module, moduleName);
54+
mod = dynamicRequire(existingModule, moduleName);
5355
} catch (e) {
5456
// no-empty
5557
}
5658

5759
if (!mod) {
5860
try {
5961
// eslint-disable-next-line deprecation/deprecation
60-
const { cwd } = dynamicRequire(module, 'process');
62+
const { cwd } = dynamicRequire(existingModule, 'process');
6163
// eslint-disable-next-line deprecation/deprecation
62-
mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T;
64+
mod = dynamicRequire(existingModule, `${cwd()}/node_modules/${moduleName}`) as T;
6365
} catch (e) {
6466
// no-empty
6567
}

packages/nextjs/src/config/webpack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export function constructWebpackConfigFunction(
332332
// Symbolication for dev-mode errors is done elsewhere.
333333
if (!isDev) {
334334
// eslint-disable-next-line @typescript-eslint/no-explicit-any
335-
const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin') ?? {};
335+
const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin', module) ?? {};
336336

337337
if (sentryWebpackPlugin) {
338338
if (!userSentryOptions.sourcemaps?.disable) {

packages/remix/src/utils/instrumentServer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ const makeWrappedCreateRequestHandler = (options: RemixOptions) =>
448448
export function instrumentServer(options: RemixOptions): void {
449449
const pkg = loadModule<{
450450
createRequestHandler: CreateRequestHandlerFunction;
451-
}>('@remix-run/server-runtime');
451+
}>('@remix-run/server-runtime', module);
452452

453453
if (!pkg) {
454454
DEBUG_BUILD && logger.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');

0 commit comments

Comments
 (0)