swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.69k stars 69 forks source link

`@swc-node/register` broken imports #729

Open wszydlak opened 10 months ago

wszydlak commented 10 months ago

According to https://github.com/nrwl/nx/issues/18985#issuecomment-1704448088 - last change in @swc-node/register which added baseUrl to tsconfig broke nx generators, eg:

// /path/to/file.ts
import { something } from  './subdir';

// /path/to/subdir/index.ts
export const something = 1;

This throws an error: Cannot find module './subdir/index.js'

At least such a change should not be added as a bugfix, cause it broke bacward compatibility.

This worked great in @swc-node/register@1.6.6 and has been broken in 1.6.7

artamonovtech commented 10 months ago

I have the same problem with 1.6.7 version.

MrRefactoring commented 10 months ago

Me too

kurt-west commented 10 months ago

I did some debugging and found the new baseUrl variable that was added in 1.6.7 is to blame. If I comment out that line my project builds as normal; it would be nice if you (@wszydlak , @artamonovtech, @MrRefactoring) could try the same to confirm.

I manually tried everything relative and absolute path I could think of and nothing seemed to work, which leads me to believe this is an issue with this library vs. nx.

MrRefactoring commented 10 months ago

@kurt-west not helped for me :(

kurt-west commented 10 months ago

@MrRefactoring interesting... thanks for giving it a try.

wszydlak commented 10 months ago

I can confirm that removing line: https://github.com/swc-project/swc-node/blob/master/packages/register/read-default-tsconfig.ts#L123 helps.

Also confirm that changing this line also helps for nx case:

-baseUrl: resolve(options.baseUrl ?? './'),
+baseUrl: options.baseUrl != null ? resolve(options.baseUrl): undefined,

However i think that baseUrl should not be manipulated on swc side, if user needs to have absolute paths, then probably swc could give an option to register tsconfig transformer, where the user could modify config.

wszydlak commented 9 months ago

@kurt-west some status? Maybe some plans to restore previous behavior?

kurt-west commented 9 months ago

@wszydlak - sorry I'm not a maintainer or contributor to this project and know very little about swc in general. I just ran into the issue at it related to nx and did some investigating. My ping to the group was to confirm everyone was seeing similar behavior in the hopes it would be an easy fix for the swc team.

wszydlak commented 9 months ago

@Brooooooklyn any chances to get this work?

Tom910 commented 7 months ago

After upgrade version I found similar an issue too:

import fs from 'fs';
const {access} = fs.promises;

in this case fs has the undefined value and error TypeError: Cannot read properties of undefined. In my cases option "esModuleInterop": true missed after upgrade the version.