swc-project / swc

Rust-based platform for the Web

Home Page:https://swc.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

swc does not prioritize jsc.paths by matching prefix length like tsc specifies

yehonatanz opened this issue · comments

Describe the bug

Seems like swc does not prioritize jsc.paths matches by length but instead chooses the first (re: arbitrary, since JSON objects are unsorted) that matches.
This is opposed to the behavior specified by tsc here:

When multiple patterns match a module specifier, the pattern with the longest matching prefix before any * token is used

As far as I understand (zero rust experience, so please be forgiving), this can be rather easily fixed by sorting paths by index of * in tsc.rs.

I'm open to implementing it myself if that is indeed classified as bug that needs fixing.

Input code

import { hello } from '@app/helpers/hello';

hello();

Config

{
  "module": {
    "type": "commonjs",
    "strictMode": true,
    "noInterop": false
  },
  "jsc": {
    "paths": {
      "@app/*": [
        "packages/*/src"
      ],
      "@app/helpers/*": [
        "packages/helpers/src/*"
      ]
    },
    "baseUrl": ".",
    "parser": {
      "syntax": "typescript"
    }
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.13&code=H4sIAAAAAAAAA8vMLcgvKlGoVshIzcnJV6hVSCvKz1VQd0gsKNAHChWkFhXrg6XUrbm4wAwNTWsAh2tUWDUAAAA%3D&config=H4sIAAAAAAAAA3WQvQ7CMAyE9z5FlbGq6M7EysDIhBhCamghbSzblaiqvDtJm%2FIzMEX3nX3yZcryXHWuHiyobT4FFbSMGJUyrutcf2dVLpyFWiMHV0dXaIDEe7fvBchhwFdtGQL20VN3Np9Y1NLwWwaw04hVEcgpkXnIPPQNuCoqJqOScS5%2FdhqwCMR%2Fd1c%2FJISZNWN%2Bfbr5ohmOZGPNzdoPNTHQ94U89qKfcSj%2BCRtqUZY8HztmPnsBwh9FpEEBAAA%3D

SWC Info output

No response

Expected behavior

Expected behavior is to match the longer @app/helpers/* pattern and emit:

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
var _hello = require("./packages/helpers/src/hello");
(0, _hello.hello)();

Actual behavior

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
var _hello = require("./packages/helpers/hello/src");
(0, _hello.hello)();

Version

1.4.13

Additional context

No response

@kdy1 I took the liberty of submitting PR to address this: #8875

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.