nestjs / nest-cli

CLI tool for Nest applications 🍹
https://nestjs.com
Other
1.97k stars 394 forks source link

Using swc with paths creates incorrect imports on Windows #2211

Closed MarkNjunge closed 1 year ago

MarkNjunge commented 1 year ago

Is there an existing issue for this?

Current behavior

Building using swc leads to imports that use paths to be absolute paths instead of relative ones.

nest build -b swc

const _appservice = require("C:/path/to/nest-swc-paths-issue/src/app.service");

Building using swc directly does not have the same issue

npx swc ./src -d dist

const _appservice = require("./app.service");

Since using swc directly does not have this issue, it is unlikely that the problem lies there.

The issue likely only affects windows.

Minimum reproduction code

https://github.com/MarkNjunge/nest-swc-paths-issue

Steps to reproduce

No response

Expected behavior

The import paths should be relative

Package version

10.0.0

NestJS version

10.0.0

Node.js version

16.15.1, 18.17.0

In which operating systems have you tested?

Other

No response

jmcdo29 commented 1 year ago

https://github.com/nestjs/nest/issues/11891

MarkNjunge commented 1 year ago

It's not a problem with swc. Building via swc directly works. The issue arises when using nestjs.

micalevisk commented 1 year ago

@MarkNjunge did you tested with @nestjs/cli@10.0.0 or just with the latest version?

MarkNjunge commented 1 year ago

@micalevisk I've tested again after ensuring @nestjs/cli@10.0.0 and I'm still experience the same issue.

kamilmysliwiec commented 1 year ago

What if you remove paths from your tsconfig.json file https://github.com/MarkNjunge/nest-swc-paths-issue/blob/master/tsconfig.json?

MarkNjunge commented 1 year ago

I've tried that it's still the same.

powerfulyang commented 1 year ago

Temporarily fix by setting swcOptions to undefine and set paths and baseUrl in .swcrc. Here is a demo.

kamilmysliwiec commented 1 year ago

For Windows users, path mapping (auto-inheriting baseUrl and paths from tsconfig by swc) was turned off in 10.1.16.

MarkNjunge commented 1 year ago

What's the correct config to get it to work? I've updated the dependencies and the issue persists.

I've updated the repo to reflect the changes. https://github.com/MarkNjunge/nest-swc-paths-issue

powerfulyang commented 1 year ago

Temporarily fix by setting swcOptions to undefine and set paths and baseUrl in .swcrc. Here is a demo.

swcOptions shouldn't be an object when use node to run @swc/cli on Windows.

powerfulyang commented 1 year ago

https://github.com/powerfulyang/api.powerfulyang.com/actions/runs/5947215073/job/16128968492#step:9:35

MarkNjunge commented 1 year ago

As @powerfulyang suggested, I was able to get it to work by changing swcOptions to undefined.

https://github.com/nestjs/nest-cli/blob/6c085971b23909f66e309d4d1b94380709429eed/lib/compiler/swc/swc-compiler.ts#L175

smiley-uriux commented 1 year ago

FWIW I'm seeing absolute paths in *nix builds too after upgrading to the latest @nestjs/cli and @swc/core packages without having an .swrc file in my project. Everything was working great until all the recent movement.

kamilmysliwiec commented 1 year ago

https://github.com/nestjs/nest-cli/pull/2271