Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fargito opened this issue Jul 6, 2024 · 4 comments · Fixed by #806
Closed

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

fargito opened this issue Jul 6, 2024 · 4 comments · Fixed by #806

Comments

@fargito
Copy link
Contributor

fargito commented Jul 6, 2024

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:

  • directory must be an absolute path to a directory where the specifier is resolved against. For CommonJS modules, it is the __dirname variable that contains the absolute path to the folder containing current module. For ECMAScript modules, it is the value of import.meta.url. In my case, it gives the expected value, i.e. /home/runner/work/rust-cdk-serverless/rust-cdk-serverless/iac
  • identifier: in my case it is ./shared

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.

@fargito fargito changed the title Unable to resolve relative import Unable to resolve relative import with oxc-resolver Jul 7, 2024
@fargito fargito changed the title Unable to resolve relative import with oxc-resolver Register: unable to resolve relative import with oxc-resolver Jul 7, 2024
@psychobolt
Copy link

psychobolt commented Jul 7, 2024

Unfortunately, I can't use the latest version of register because oxc-resolver doesn't support Yarn PnP: oxc-project/oxc-resolver#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 swc-project/swc#3247 (comment) , could there be plans to use another resolver?

@fargito
Copy link
Contributor Author

fargito commented Jul 8, 2024

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

@fargito
Copy link
Contributor Author

fargito commented Jul 8, 2024

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

@fargito
Copy link
Contributor Author

fargito commented Jul 8, 2024

Thanks @Brooooooklyn! This fixed it for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants