Skip to content

Conversation

@isaacs
Copy link

@isaacs isaacs commented Dec 21, 2025

Add support for patching an ESM internal module that is exported with a submodule path name in the package.json exports object.

Which problem is this PR solving?

It is not currently possible to patch an ESM submodule export.

Fixes # (issue)

Short description of the changes

When determining whether to apply a patch, the file.name is compared against the normalizedPath. However, this makes it impossible to get import-in-the-middle to consider the module a valid hook target.

Allow passing in the submodule specifier as the file name, treating it as the effective "path" to be hooked.

This is the continuation (one level higher in the stack) of nodejs/import-in-the-middle#215

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update (maybe?)

I believe it could be argued that this is a new feature, and perhaps even not the most elegant approach. It does feel a bit clunky, tbh, since the "filename" being passed in is actually a virtual path defined in package.json, not an actual path on disk, since those are fully abstracted away by the ESM module loading semantics. But, I can't see how it would break any existing use cases.

How Has This Been Tested?

Test included in the patch, at experimental/packages/opentelemetry-instrumentation/test/node/EsmSubmoduleInstrumentation.test.mjs

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

Add support for patching an ESM internal module that is exported with a
submodule path name in the `package.json` exports object.
@isaacs isaacs requested a review from a team as a code owner December 21, 2025 17:14
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 21, 2025

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: isaacs / name: isaacs (3f351ea)

isaacs added a commit to getsentry/sentry-javascript that referenced this pull request Dec 22, 2025
This patches the `@langchain/langgraph/prebuilt` submodule export, when
loading in ESM mode.

Requires an update to `@opentelemetry/instrumentation`, in order to not
block the patching of ESM submodule exports.

depends-on: open-telemetry/opentelemetry-js#6246
@isaacs
Copy link
Author

isaacs commented Dec 22, 2025

ping @open-telemetry/javascript-approvers

Any guidance would be much appreciated, if this isn't the best way to get this done, but it's blocking the instrumenting of certain package submodule exports in ESM mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant