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

Imports use absolute paths on windows instead of relative #7806

Closed
martijnimhoff opened this issue Aug 14, 2023 · 31 comments · Fixed by #7892
Closed

Imports use absolute paths on windows instead of relative #7806

martijnimhoff opened this issue Aug 14, 2023 · 31 comments · Fixed by #7892
Assignees
Labels
Milestone

Comments

@martijnimhoff
Copy link

martijnimhoff commented Aug 14, 2023

Describe the bug

I'm using @swc/core on version 1.3.76. On version 1.3.74, i do not have this problem. I'm also on windows.

When I compile my TS into JS, my imports break because in the output an absolute path is used. This path leads to the uncompiled TS files. Please have a look at the expected vs actual behavior.

Input code

No response

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  "module": {
    "type": "commonjs"
  },
  "jsc": {
    "target": "es2022",
    "parser": {
      "syntax": "typescript"
    },
    "keepClassNames": true,
    "baseUrl": "./"
  },
  "minify": false
}

Playground link

No response

Expected behavior

I'm expecting this output:

const _admin = /*#__PURE__*/ _interop_require_default(require("./routes/admin"));

Actual behavior

I'm getting this output:

const _admin = /*#__PURE__*/ _interop_require_default(require("C:/Projects/example-project/src/routes/admin"));

Version

1.3.76

Additional context

On 1.3.74 I'm getting the expected behavior.

@kdy1 kdy1 self-assigned this Aug 14, 2023
@kdy1 kdy1 added this to the Planned milestone Aug 14, 2023
@kdy1
Copy link
Member

kdy1 commented Aug 14, 2023

Please provide a reproduction

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 14, 2023
@wp-harm
Copy link

wp-harm commented Aug 17, 2023

This is a continuation from this issue: #7603 (comment)

A reproduction was asked for. Using this method the issue can be reproduced:

  1. Clone: https://github.com/kenso312/nestjs-v10-webpack-boilerplate (it's not mine, but it has SWC + path alias enabled)
  2. Install deps by running pnpm install (install pnpm using this command if you don't have it: npm install -g pnpm)
  3. Copy env file: cp .env.example .env
  4. Run pnpm dev
  5. On Windows this will fail and on MacOS it works (I don't know about Linux but I'm assuming it will work)

I think the issue should be re-opened as it seems the issue has become much more clear 🙂

@kdy1
Copy link
Member

kdy1 commented Aug 17, 2023

Hmm.. It seems like a too large app to debug. https://antfu.me/posts/why-reproductions-are-required

@kdy1
Copy link
Member

kdy1 commented Aug 17, 2023

I accept next.js app as repro because I'm familiar with it, but digging into a framework is extremely time consuming

@wp-harm
Copy link

wp-harm commented Aug 17, 2023

I see, I understand your time is valuable in that sense. I'll see if I can make simpler repo myself 👍

@kdy1
Copy link
Member

kdy1 commented Aug 17, 2023

@wp-harm Can you create a smaller repro using nest.js starter kit? I found that https://github.com/KirianCaumes/nestjs-bug-swc-import is small enough

@kdy1 kdy1 reopened this Aug 17, 2023
@kdy1

This comment was marked as outdated.

@kdy1 kdy1 modified the milestones: v1.3.77, Planned Aug 17, 2023
@wp-harm
Copy link

wp-harm commented Aug 17, 2023

I think I did you one better. This repo is the root of the issue for NestJS. Essentially I did a deep dive into the NestJS CLI repo and and discovered the line calling the SWC compiler: https://github.com/nestjs/nest-cli/blob/master/lib/compiler/swc/swc-compiler.ts#L165

The method and input to that method I distilled into this repo: https://github.com/harm-less/swc-path-bug.
This input can be seen in script/compile.js. The only thing I changed is the process.cwd() to make sure the input would work on your PC as well.

I tested this repo on both my Windows and MacOS system and only the latter worked. The resulting compiled JS is exactly as @martijnimhoff mentioned in his initial message on both operating systems.

I did discover a few workarounds during my research, perhaps you can make more sense of them than I can:

  1. If you disable/remove swcOptions.jsc.baseUrl from the compile script, on Windows it suddenly starts working.
  2. What also seems to work is changing swcOptions.jsc.paths to ['@utils/*': [ 'dist/utils/*' ]] because then the absolute path is pointing to the correct dir (for NestJS or any project using SWC using the CLI this is actually a workaround for the time being. So to anyone encountering this problem, manually add the same paths to your .swcrc and point to your dist folder for each of the paths)

I do believe those workaround are not the way to go, so hopefully using are there findings you can more easily discover the root problem @kdy1 💪

@kdy1
Copy link
Member

kdy1 commented Aug 18, 2023

5c4bfa6 looks like the cause

@kdy1
Copy link
Member

kdy1 commented Aug 18, 2023

So my guess is a difference between UNC and normal path.

@greut

This comment was marked as outdated.

@kdy1

This comment was marked as off-topic.

@greut

This comment was marked as off-topic.

@greut

This comment was marked as off-topic.

@kdy1
Copy link
Member

kdy1 commented Aug 25, 2023

Can you try v1.3.79 once https://github.com/swc-project/swc/actions/runs/5972622833 is finished?

@kdy1
Copy link
Member

kdy1 commented Aug 25, 2023

@wp-harm Can you try v1.3.80?

@kdy1
Copy link
Member

kdy1 commented Aug 25, 2023

I'm pretty sure this is fixed

@kdy1 kdy1 closed this as completed Aug 25, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.80 Aug 25, 2023
@wp-harm
Copy link

wp-harm commented Aug 28, 2023

@kdy1 Will do! Tomorrow I'll be back at my Windows system and try it out 👍

@powerfulyang
Copy link

@wp-harm
Copy link

wp-harm commented Aug 29, 2023

I updated to v1.3.80 in my minimal reproduction repo, this was the result:

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
const _helloworldutils = require("C:/projects/github/typescript-swc-starter/swc-path-bug/src/utils/hello-world.utils.js");
console.log((0, _helloworldutils.helloWorld)());

//# sourceMappingURL=index.js.map

The output is still absolute and still references the /src/ directory so I can confirm the result from @powerfulyang.

The same happened to my NestJS app too.

@viniciussantos45
Copy link

I updated to v1.3.80 in my minimal reproduction repo, this was the result:

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
const _helloworldutils = require("C:/projects/github/typescript-swc-starter/swc-path-bug/src/utils/hello-world.utils.js");
console.log((0, _helloworldutils.helloWorld)());

//# sourceMappingURL=index.js.map

The output is still absolute and still references the /src/ directory so I can confirm the result from @powerfulyang.

The same happened to my NestJS app too.

Same problem here too, I'm using @nestjs/cli in version 10.1.16

@kdy1
Copy link
Member

kdy1 commented Aug 30, 2023

Okay, it's not reproducible usnig the Rust testing system of SWC. So my guess is related to jsc.baseUrl: process.cwd().
I'll test it using JS testing system

@kdy1
Copy link
Member

kdy1 commented Aug 30, 2023

The repro works if I give \\?\C:\Users\MSI\projects\swc-win\swc-path-bug as jsc.baseUrl.

@powerfulyang
Copy link

The repro works if I give \\?\C:\Users\MSI\projects\swc-win\swc-path-bug as jsc.baseUrl.

YES, \\?\C:\Users\MSI\projects\swc-win\swc-path-bug works.

@powerfulyang
Copy link

https://github.com/powerfulyang/swc-path-bug/actions/runs/6021221240/job/16333735691#step:7:11

@powerfulyang
Copy link

powerfulyang commented Aug 30, 2023

So, should the user manually set the prefix? 😗

@kdy1
Copy link
Member

kdy1 commented Aug 30, 2023

No, I'll normalize it from the Rust code. It's not matter of prefix, but it's UNC path of Windows

kdy1 added a commit that referenced this issue Aug 30, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.81 Aug 30, 2023
@viniciussantos45
Copy link

Using NestJS

After updating @swc/core to version 1.3.81 it started working for @nestjs/cli version 10.1.16

My current setup:

tsconfig.json

{
  "compilerOptions": {
   ...
    "outDir": "./dist",
    "baseUrl": ".",
    "paths": {
      "@common/*": ["src/common/*"],
      "@config/*": ["src/config/*"],
      "@modules/*": ["src/modules/*"]
    }
   ...
  }
}

.swcrc

{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  
  "minify": false,
  "jsc": {
    ...
    "baseUrl": "./",
    "paths": {
      "@common/*": ["src/common/*"],
      "@config/*": ["src/config/*"],
      "@modules/*": ["src/modules/*"]
    },
    ...
  }
}

nest-cli-.json

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "builder": "swc",
    "typeCheck": true
  }
}

@BlindKai
Copy link

BlindKai commented Sep 9, 2023

That is actually pretty weird.
I've just tried to create a plain project using the following configs (from the #7806 (comment)):

    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
  ...
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.83",

And the only time when the project starts is when the paths are specified in both .swcrc and tsconfig.json

    "paths": {
      "@common/*": ["src/common/*"],
      "@config/*": ["src/config/*"],
      "@modules/*": ["src/modules/*"]
    },

image

Without specifying these, I'm getting errors:

image

@Makoehle
Copy link

In my case VS-Code was automatically importing paths like so src/moduleOne/moduleOne.service.ts which lead to this error. I was able to fix the error my adapting one of your comments and add the following to my tsconfig.json.

    "paths": {
      "src/*": ["src/*"]
    }

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 28, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.