Skip to content

Commit

Permalink
fix: Hide require.resolve from bundlers
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Apr 25, 2024
1 parent 3ab4f76 commit 8453c61
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ import type { OnRequireFn } from 'require-in-the-middle';
import { Hook } from 'require-in-the-middle';
import { readFileSync } from 'fs';

/**
* Bundlers try and resolve require.resolve calls and complain when they can't
* reason about a dynamic module name.
* @param mod The module global
*/
function hiddenFromBundlersRequireResolve(
mod: { require: { resolve: (name: string) => string } },
name: string
): string {
return mod.require.resolve(name);
}

/**
* Base abstract class for instrumenting node plugins
*/
Expand Down Expand Up @@ -146,7 +158,7 @@ export abstract class InstrumentationBase
this._modules.forEach((module: InstrumentationModuleDefinition) => {
const { name } = module;
try {
const resolvedModule = require.resolve(name);
const resolvedModule = hiddenFromBundlersRequireResolve(module, name);

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / browser-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (14)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (14)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (14)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-windows-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (16)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (16)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (16)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (18)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / node-tests (20)

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.

Check failure on line 161 in experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

View workflow job for this annotation

GitHub Actions / webworker-tests

Argument of type 'InstrumentationModuleDefinition' is not assignable to parameter of type '{ require: { resolve: (name: string) => string; }; }'.
if (require.cache[resolvedModule]) {
// Module is already cached, which means the instrumentation hook might not work
this._diag.warn(
Expand Down

0 comments on commit 8453c61

Please sign in to comment.