swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.71k stars 71 forks source link

Register: unable to resolve relative import with oxc-resolver #804

Closed fargito closed 1 week ago

fargito commented 1 week ago

Context

When updating @swc-node/register from version 1.10.0 to 1.10.2, relative typescript imports are no longer resolved.

When using a relative import, I get:

node:internal/modules/run_main:129
    triggerUncaughtException(
    ^
Error: Cannot find module './shared': ./shared cannot be resolved in file:///home/runner/work/rust-cdk-serverless/rust-cdk-serverless/iac/index.ts
    at resolve (file:///home/runner/work/rust-cdk-serverless/rust-cdk-serverless/node_modules/.pnpm/@swc-node+register@1.10.2_@swc+core@1.5.0_@swc+types@0.1.6_typescript@5.5.3/node_modules/@swc-node/register/esm/esm.mjs:208:15)
    at async nextResolve (node:internal/modules/esm/hooks:866:22)
    at async Hooks.resolve (node:internal/modules/esm/hooks:304:24)
    at async handleMessage (node:internal/modules/esm/worker:196:18)

Investigation

The issue has probably been introduced by #798, more precisely here.

I'm a bit puzzled, because the call to resolver.async is done with the correct arguments according to the oxc-resolver docs:

This looks correct according to the oxc-resolver docs, but oxc-resolver cannot find it. Maybe the ts config is incorrectly passed to the resolver?

I will try and set up a minimal reproduction in the coming days.

psychobolt commented 1 week ago

Unfortunately, I can't use the latest version of register because oxc-resolver doesn't support Yarn PnP: https://github.com/oxc-project/oxc-resolver/issues/53 . Even a simple import in my setup e.g. import { $ } from 'execa' will fail:

node:internal/modules/run_main:129
    triggerUncaughtException(
    ^
Error: Cannot find module 'execa': execa cannot be resolved in file:///Users/mitran/Projects/vite-storybook-boilerplate/bin/bootstrap.ts
    at resolve (file:///Users/mitran/Projects/vite-storybook-boilerplate/.yarn/unplugged/@swc-node-register-virtual-f17b414b8d/node_modules/@swc-node/register/esm/esm.mjs:208:15)
    at async nextResolve (node:internal/modules/esm/hooks:866:22)
    at async Hooks.resolve (node:internal/modules/esm/hooks:304:24)
    at async handleMessage (node:internal/modules/esm/worker:196:18)

Node.js v20.15.0

Seems like in https://github.com/swc-project/swc/issues/3247#issuecomment-2212345627 , could there be plans to use another resolver?

fargito commented 1 week ago

@psychobolt I'm using pnpm, so maybe it is a separate issue

fargito commented 1 week ago

I have set up a minimal reproduction: https://github.com/fargito/swc-node-relative-import-issue-repro

fargito commented 1 week ago

Thanks @Brooooooklyn! This fixed it for me.