Skip to content

[BUG]: when using ESM, integrations won't be registered if the hook uses a file #5479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FredericEspiau opened this issue Mar 25, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@FredericEspiau
Copy link
Contributor

FredericEspiau commented Mar 25, 2025

Tracer Version(s)

5.43.0

Node.js Version(s)

22.14.0

Bug Report

Hey and thanks for the hard work,

let's take the example of express:

file: 'lib/middleware/query.js'

addHook({
  name: 'express',
  versions: ['4'],
  file: 'lib/middleware/query.js'
}, query => {
  // redacted
})

Here, you add a hook for the express integration that should work for all version 4.*.*. It should look for the lib/middleware/query.js file.

This hook will work if both the version matches and the file is present.

Unfortunately, there is a difference between require-in-the-middle and import-in-the-middle that will prevent this from working.

The difference will happen around here:

matchesFile = moduleName === fullFilename

If you use cjs, fullFilename will be express/lib/middleware/query.js and moduleName will be express/lib/middleware/query.js

If you use esm, fullFilename will be express/lib/middleware/query.js and moduleName will be express

This doesn't seem to be an issue with dd-trace-js but rather with import-in-the-middle.

I think the issue is here: https://github.com/nodejs/import-in-the-middle/blob/53a33a9b07799bff815864089a0c072d223df47b/index.js#L147

This looks like an optimisation to prevent sending unrelated files to the callbacks but it prevents lots of files to be sent to the callback, including the ones we need.

If you log the files sent to the Hook, you will notice that require-in-the-middle sends much more files than import-in-the-middle.

If you remove this line if (!baseDir.endsWith(specifiers.get(filename))) continue in import-in-the-middle it works again.

As I'm really not sure if the issue is with dd-trace-js or import-in-the-middle, I'm creating an issue both here and there.

Note: this should fix this issue as well: #5092

Related issue in import-in-the-middle: nodejs/import-in-the-middle#185

Thanks in advance.

Reproduction Code

No response

Error Logs

No response

Tracer Config

No response

Operating System

No response

Bundling

No Bundling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant