nestjs / nest-cli

CLI tool for Nest applications 🍹

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using swc with paths creates incorrect imports on Windows

MarkNjunge opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

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?

  • macOS
  • Windows
  • Linux

Other

No response

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

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

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

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

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

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

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

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.

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

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.