privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
9.74k stars 152 forks source link

Incompatible with `import-in-the-middle` loader #554

Closed timfish closed 6 months ago

timfish commented 6 months ago

Acknowledgements

Problem & expected behavior (under 200 words)

With import-in-the-middle@1.7.1 and the following code

test.ts

import { register } from "node:module";
register("import-in-the-middle/hook.mjs", import.meta.url);
await import("node:util");

Running:

tsx test.ts

I get the following stack trace:

TypeError: The URL must be of scheme file
    at new NodeError (node:internal/errors:406:5)
    at fileURLToPath (node:internal/url:1393:11)
    at finalizeResolution (node:internal/modules/esm/resolve:234:42)
    at moduleResolve (node:internal/modules/esm/resolve:845:10)
    at defaultResolve (node:internal/modules/esm/resolve:1043:11)
    at nextResolve (node:internal/modules/esm/hooks:833:28)
    at y (file:///Users/tim/Documents/Repositories/sentry-v8-tsx-error-repro/node_modules/tsx/dist/esm/index.mjs?1715697897382:128:19)
    at j (file:///Users/tim/Documents/Repositories/sentry-v8-tsx-error-repro/node_modules/tsx/dist/esm/index.mjs?1715697897382:191:20)
    at nextResolve (node:internal/modules/esm/hooks:833:28)
    at resolve (/Users/tim/Documents/Repositories/sentry-v8-tsx-error-repro/node_modules/import-in-the-middle/hook.js:94:23) 

If you disable allowJs this no longer errors.

Bugs are expected to be fixed by those affected by it

Supporting engineering work financially will speed up resolution

privatenumber commented 6 months ago

I'm going to close this temporarily for bug tracking purposes since the minimal reproduction should prove that the bug is in tsx rather than import-in-the-middle.

But don't let that deter you and feel free to investigate further. If you determine it's actually a bug in tsx, I'd be happy to accept a PR.

The published tsx code is minified, but the contribution guide has development tips on how you can turn it off and use the dev version: https://github.com/privatenumber/tsx/blob/master/CONTRIBUTING.md#development

timfish commented 6 months ago

Do you have any tips for debugging loader hooks? I'm console.loging in multiple modules but it's super painful to not be able to add breakpoints in the node built in code!

nwalters512 commented 6 months ago

I'm highly confident that this is an issue with import-in-the-middle and not tsx: https://github.com/getsentry/sentry-javascript/issues/12011#issuecomment-2110923550

timfish commented 6 months ago

Turns out it's a Node bug triggered by a combination of import-in-the-middle setting parentURL to node:* and tsx attempting to load missing files while searching through various extensions.

https://github.com/nodejs/node/issues/52987