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

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

Closed
yehonatanz opened this issue Apr 14, 2024 · 2 comments · Fixed by #8875
Closed

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

yehonatanz opened this issue Apr 14, 2024 · 2 comments · Fixed by #8875
Assignees
Labels
Milestone

Comments

@yehonatanz
Copy link
Contributor

yehonatanz commented Apr 14, 2024

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

@yehonatanz
Copy link
Contributor Author

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

kdy1 pushed a commit that referenced this issue Apr 19, 2024
)

**Description:**

See #8858 

**BREAKING CHANGE:**
`jsc.paths` are now prioritized differently to align with `tsc`. The order is:
1. Exact matches
2. Wildcard pattern matches, sorted by length of prefix before wildcard


**Related issue:**

 - Closes #8858
@kdy1 kdy1 modified the milestones: Planned, v1.4.17 Apr 23, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented May 23, 2024

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 May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants