Open i124q2n8 opened 10 months ago
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.
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
ts-patch-ts-node-workaround.js:
const originalExtensions = Object.fromEntries(
Object.entries(require.extensions).map(([k, v]) => {
return [
k,
(module, filename) => {
restore();
return v(module, filename);
},
];
}),
);
function restore() {
for (const [k, v] of Object.entries(originalExtensions)) {
if (require.extensions[k] !== v) {
require.extensions[k] = v;
}
}
}
restore();
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: https://github.com/nonara/ts-patch/blob/b4b50de8acdee25f69c3902fdd6eab22194ec891/projects/patch/src/plugin/register-plugin.ts#L111
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