getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.76k stars 1.52k forks source link

`.` in `exports` property of CJS file breaks dynamic imports #12622

Open MattIPv4 opened 1 week ago

MattIPv4 commented 1 week ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

8.11.0

Framework Version

No response

Link to Sentry event

No response

SDK Setup

Sentry.init();

Steps to Reproduce

package.json:

{
  "type": "module",
  "dependencies": {
    "@sentry/node": "^8.11.0"
  }
}

index.js:

import * as Sentry from '@sentry/node';

const main = async () => {
  Sentry.init();

  const other = await import(new URL('./other.cjs', import.meta.url));
  console.log(other);
};

main();

other.cjs:

exports['one.two'] = () => console.log('b');

Expected Result

It runs, outputting:

[Module: null prototype] {
  default: { 'one.two': [Function (anonymous)] },
  'one.two': [Function (anonymous)]
}

(this can be reproduced by commenting out the Sentry.init(); line)

Actual Result

It fails on a sytax error:

file://[snip]/other.cjs?iitm=true:21
      let $one.two = _.one.two
              ^

SyntaxError: Unexpected token '.'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:168:18)
    at callTranslator (node:internal/modules/esm/loader:279:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:285:30)
    at async link (node:internal/modules/esm/module_job:78:21)

Node.js v20.12.2
MattIPv4 commented 1 week ago

For some additional context, this works fine in @sentry/node@7. This is blocking our upgrade to @sentry/node@8. In our actual code, the usage of . in exports comes from files generated by grpc-tools, which are then run through @grpc/grpc-js.

AbhiPrasad commented 1 week ago

Sorry for the trouble, this is def some issues with the new automatic instrumentation in 8.x.

Could you apply patch-package with the linked patch here https://github.com/getsentry/sentry-javascript/issues/12485#issuecomment-2169398097 and see if the error goes away?

MattIPv4 commented 1 week ago

I had to install import-in-the-middle@1.8.0 as the patch failed on 1.8.1, so my package.json now looks like this (from the repro above):

{
  "type": "module",
  "scripts": {
    "postinstall": "patch-package"
  },
  "dependencies": {
    "@sentry/node": "^8.11.0",
    "import-in-the-middle": "^1.8.0",
    "patch-package": "^8.0.0"
  }
}

Running npm i shows that patch-package is applying the patch, however node index.js still results in the same syntax error as above.

timfish commented 1 week ago

This is currently tracked upstream by https://github.com/DataDog/import-in-the-middle/issues/94

I've been thinking of some ways to work around this and should get a PR submitted this week!

timfish commented 1 week ago

https://github.com/DataDog/import-in-the-middle/pull/115