timocov / dts-bundle-generator

A tool to generate a single bundle of dts with types tree-shaking
MIT License
762 stars 40 forks source link

Maximum call stack size exceeded (Infinite Loop) at `computeUsagesRecursively` #314

Closed KieranTH closed 6 months ago

KieranTH commented 6 months ago

Bug report Computing usages recursively causes infinite loop when type declarations are cyclic.

After spending a few hours attempting to debug this myself in the types-usage-evaluator file, I've had no luck.

I personally have valid cyclic types in a GQL backend I'm running, but I've also been able to replicate the issue with another more popular package: React FontAwesome.

I'm not entirely familiar with how other tools such as TSC deal with this issue, my first instinct is to run a check on whether a Type/Interface contains itself at any other point in the tree and exit the loop, but unsure if that's the correct approach.

Here's a repo that displays this issue in a very simple setup:

https://github.com/KieranTH/DTS-Bundle-Circular-Issue

I'm using Bun in this example but any runtime/package manager should have this issue.

To reproduce the issue just clone that repo and run bun i && bun run build.mjs

Thanks!

Actual output

RangeError: Maximum call stack size exceeded.
      at computeUsagesRecursively (/home/project/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:155:13)
      at computeUsageForNode (/home/project/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:55:21)
      at visitNodes (/home/project/node_modules/typescript/lib/typescript.js:27934:24)
      at forEachChildInSourceFile (/home/project/node_modules/typescript/lib/typescript.js:28556:18)
      at forEachChild (/home/project/node_modules/typescript/lib/typescript.js:28019:37)
      at computeUsages (/home/project/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:40:13)
      at new TypesUsageEvaluator (/home/project/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:10:9)
      at generateDtsBundle (/home/project/node_modules/dts-bundle-generator/dist/bundle-generator.js:21:33)
timocov commented 6 months ago

but any runtime/package manager should have this issue.

Apparently, if you just run dts-bundle-generator tool it works just fine:

$ ./node_modules/.bin/dts-bundle-generator index.ts
Compiling input files...
Processing index.ts
Writing index.ts -> index.d.ts
Checking generated files...
Done in 1.31s

$ cat index.d.ts
// Generated by dts-bundle-generator v9.3.1

import { faCat, faSquareMinus } from '@fortawesome/free-solid-svg-icons';

export {
        faCat,
        faSquareMinus,
};

export {};

I'm curious what bun-plugin-dts adds to the config so it makes it to fail...

timocov commented 6 months ago

It seems it might be the issue with Bun itself. Well, technically it is just the difference between runtimes. Apparently nodejs has higher limits for the callstack size than bun, thus you see this error. I just cut a ticket to bun https://github.com/oven-sh/bun/issues/10270 just in case if they want to sync/increase this value, but regardless I'll try to fix it from my size because even if they will do that, there is a cache to hit this issue even with a higher limit with more complex types.

timocov commented 6 months ago

Oh btw this has been fixed in #303 already (but it is not published yet). I just validated with local build it works just fine.

KieranTH commented 6 months ago

Appreciate the quick response!

Thank you for looking into it and creating the issue on Bun's side!

I'll keep a look out for the performance improvement release :) Thanks

timocov commented 6 months ago

The fix has been published in 9.4.0 version.