aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.23k stars 44 forks source link

Circular reference between tsconfig files leads to deadlock #132

Closed bitschubse closed 2 weeks ago

bitschubse commented 5 months ago

After updating to version 4.3.1 (same problem for 4.3.0), our build hangs directly after the build command call. There are no log messages or errors.

Using the DEBUG variable we get only the following output. Build hangs after that.

  vite-tsconfig-paths options.root   == /home/user/app +0ms
  vite-tsconfig-paths project root   == /home/user/app +1ms
  vite-tsconfig-paths workspace root == undefined +0ms
  vite-tsconfig-paths projects: [
  '/home/user/app/tsconfig.json',
  '/home/user/app/project1/tsconfig.json',
  '/home/user/app/project2/tsconfig.json',
  '/home/user/app/project3/tsconfig.json'
] +71ms

Used vite dependency versions:

"vite": "4.5.1",
"vite-plugin-dts": "3.7.0",
"vite-plugin-eslint": "1.8.1",
"vite-tsconfig-paths": "4.3.1",

Does anyone have any idea what the cause is or how we can get to the bottom of the problem?

aleclarson commented 5 months ago

You probably want to pin to v4.2.3 for now. If someone gets me a StackBlitz reproduction, I'll look into it.

bitschubse commented 5 months ago

Unfortunately, it is not so easy to reduce our project to a easy StackBlitz reproduction. Therefore I had hoped for a first idea which changes in the new versions could lead to such a problem. But I will try my best to create a StackBlitz reproduction.

aleclarson commented 5 months ago

The content of your tsconfig.json files might hint at the problem.

The only change in 4.3 is upgrading tsconfck (the package used for finding and parsing tsconfig files) by a major version.

navaru commented 5 months ago

Have the same issue, simple reproducible case with 2 tsconfigs, most likely tsconfck.parse goes into a loop, in my case.

tsconfig.json

{
    "references": [{ "path": "./tsconfig.test.json" }],
}

tsconfig.test.json

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "composite": true
    }
}
Yberion commented 5 months ago

Hello @aleclarson, I'm facing the same issue with Analogjs & Angular.

Here is a reproduction:

With 4.3.0 I'm facing this issue:

> nx run analog-nx:test

failed to load config from E:\Git\analog-nx\apps\analog-nx\vite.config.ts
 >  NX   No "exports" main defined in E:\Git\analog-nx\node_modules\tsconfck\package.json

With 4.3.1 nothing happen

image

Downgrading to 4.2.3 works for now.

navaru commented 5 months ago

Seems like you have a similar structure as in my case, where a tsconfig.base references another tsconfig that extends it. Most likely this is the issue.

dominikg commented 5 months ago

this seems to happen if you have an indirect circles. tsconfck detects circles in "extends", but a circle thats between references and extends can stay undetected and then it can lock up with 2 Promises waiting for each other if you try to parse a 2 of them at the same time.

This is bound to happen here https://github.com/aleclarson/vite-tsconfig-paths/blob/ca4e2aa7f3be36facbce0305cc145f9d5f9693d1/src/index.ts#L96 if all files in question are named tsconfig.json

but can also happen if many files are processed at the same time. I'll have to see how to guard against this in tsconfck.

A workaround in get-tsconfig-paths could be to serialize the parse of found configs for the time being, affected users could try to not have references point at files that they also extend.

circle:

/src/tsconfig.json # extends ../tsconfig.json
tsconfig.json  # references src/tsconfig.json

no circle:

/src/tsconfig.json # extends ../tsconfig.base.json
tsconfig.json  # references src/tsconfig.json , nothing but references in it
tsconfig.base.json  # all the base config values

the latter seems to be the pattern used by typescript itself: https://github.com/microsoft/TypeScript/blob/main/src/tsconfig.json

aleclarson commented 5 months ago

If anyone wants to implement a workaround in this plugin, PR welcome.

Otherwise, use the config solution recommended by Dominik above, and wait for a fix to be added to tsconfck (a package used by this plugin for tsconfig parsing).

bitschubse commented 2 months ago

@aleclarson: Problem seems to be fixed with 4.3.2 without any changes on our side. Should I close the issue?