fix: instrument ESM package export submodule #6246
Open
+137
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add support for patching an ESM internal module that is exported with a submodule path name in the
package.jsonexports 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.nameis compared against thenormalizedPath. 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.
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.mjsChecklist: