Skip to content

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

Open
@FredericEspiau

Description

@FredericEspiau

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions