getsentry / sentry-javascript

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

using `--import ./instrument.mjs` and `tsx` fails #12357

Closed AbhiPrasad closed 1 week ago

AbhiPrasad commented 1 month 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.7.0

Framework Version

Node v20.14.0

Link to Sentry event

No response

SDK Setup

Sentry.init({ dsn, tracesSampleRate: 1.0 });

Steps to Reproduce

"NODE_OPTIONS='--import ./instrument.mjs' doppler run -- tsx src/index.ts"

Discord thread: https://discord.com/channels/621778831602221064/1247628587176034324/1247628587176034324

Expected Result

Does not cause an error

Actual Result

> NODE_OPTIONS='--import ./instrument.mjs' doppler run -- tsx watch src/index.ts

SyntaxError [Error]: Unexpected token (5:9)
    at pp$4.raise (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:3573:15)
    at pp$9.unexpected (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:772:10)
    at pp$9.expect (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:766:28)
    at pp$7.parseBindingList (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:2077:19)
    at pp$8.parseFunctionParams (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:1386:24)
    at pp$8.parseFunction (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:1375:10)
    at Parser.parseExport (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn-import-attributes@1.9.5_acorn@8.11.3/node_modules/acorn-import-attributes/lib/index.js:172:35)
    at pp$8.parseStatement (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:948:76)
    at pp$8.parseTopLevel (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:829:23)
    at Parser.parse (/Users/ollebergkvist/Documents/dev/projects/alpha-securities/api-crons/node_modules/.pnpm/acorn@8.11.3/node_modules/acorn/dist/acorn.js:601:17) {
  pos: 179,
  loc: { line: 5, column: 9 },
  raisedAt: 180
}

Happens even when we pin import-in-the-middle to 1.8.0

timfish commented 1 month ago

This appears to be because the tsx and import-in-the-middle loaders are registered in the wrong order. import-in-the-middle is being passed TypeScript which it was never designed to parse.

timfish commented 1 month ago

https://github.com/privatenumber/tsx/issues/571

timfish commented 1 month ago

This should hopefully have been fixed by the numerous PRs recently merged at import-in-the-middle.

Specifically for tsx, import-in-the-middle now gracefully handles source code it can't parse by falling back to the parent loader. This does mean that iitm won't be able to hook TypeScript files but that's likely not much of a concern for now.

While we wait for this to be released, there is a patch available that combines all the fixes. If anyone can confirm this patch fixes this issue that would be super helpful!

AbhiPrasad commented 1 week ago

With the newest release of import-in-the-middle v1.9.0 this should be fixed.

If you upgrade to a fresh install of the latest version of the Node SDK it should use import-in-the-middle@1.9.0 by default.