You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We ran into an issue with ts-node + ts-patch where large projects or projects that use hmr fail to compile.
RangeError: Maximum call stack size exceeded
at console.log (node:internal/console/constructor:378:6)
at /ts-node-ts-patch-bug/transformer.ts:30:21
at tspWrappedFactory (evalmachine.<anonymous>:223:31)
at transformSourceFileOrBundle (evalmachine.<anonymous>:89683:51)
at transformation (evalmachine.<anonymous>:112809:16)
at transformRoot (evalmachine.<anonymous>:112832:73)
at transformNodes (evalmachine.<anonymous>:112817:72)
at emitJsFileOrBundle (evalmachine.<anonymous>:113404:26)
at emitSourceFileOrBundle (evalmachine.<anonymous>:113339:7)
at forEachEmittedFile (evalmachine.<anonymous>:113093:26)
I am not sure if the issue is caused by ts-patch or ts-node, but the following line in ts-patch calls into ts-node, which in turn re-registers the extensions:
If this is an issue in ts-node I am happy to report it there.
Minimal reproducible example:
src/main.ts
import{resolve}from"path";asyncfunctionmain(){for(leti=0;i<1_000_000;i++){const{ test }=awaitimport("./test");test();// simulate: hot module replacementdeleterequire.cache[resolve("./src/test.ts")];}}main();
(Note: Another main.ts without delete require.cache[...] can be found below)
The same error occurs when importing ~2500 distinct files. Simulated by copying ./src/test.ts to ./src/o/{i}.ts and importing it.
src/main.ts
import{readFile,writeFile}from"fs/promises";asyncfunctionmain(){constcontent=awaitreadFile("./src/test.ts","utf-8");for(leti=0;i<1_000_000;i++){console.log(i);awaitwriteFile(`./src/o/${i}.ts`,content);const{ test }=awaitimport(`./o/${i}`);test();}}main();
The text was updated successfully, but these errors were encountered:
In case someone else got this issue. Here is a hacky workaround: node --stack-size=100000 -r ts-node/register src/main.ts.
Note that the stack will continue to grow and importing gets slower for each import.
Update 2024-04-23
Increasing the stack-size is only a temporary fix as this will crash node after a (long) while.
We now register a helper AFTER ts-node: node -r ts-node/register -r ./ts-patch-ts-node-workaround.js src/main.ts
This hook restores the original ts-node handlers after each import and thus remove the unnecessary recursive calls. (Note that you are no longer able to register new extensions afterwards)
We ran into an issue with ts-node + ts-patch where large projects or projects that use hmr fail to compile.
After some digging it seems that ts-patch causes ts-node to call
registerExtension
every time a transformer is used.registerExtensions
calls all old handlers which leads to a unnecessary long chain of handlers. See https://github.com/TypeStrong/ts-node/blob/ddb05ef23be92a90c3ecac5a0220435c65ebbd2a/src/index.ts#L1341I am not sure if the issue is caused by ts-patch or ts-node, but the following line in ts-patch calls into ts-node, which in turn re-registers the extensions:
ts-patch/projects/patch/src/plugin/register-plugin.ts
Line 111 in b4b50de
If this is an issue in ts-node I am happy to report it there.
Minimal reproducible example:
src/main.ts
(Note: Another main.ts without
delete require.cache[...]
can be found below)src/test.ts
tsconfig.json
transformer.ts
package.json
Another example
The same error occurs when importing ~2500 distinct files. Simulated by copying
./src/test.ts
to./src/o/{i}.ts
and importing it.src/main.ts
The text was updated successfully, but these errors were encountered: