-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS 5.1.3 Regression: TSC Compiler throws FATAL ERROR: Reached heap limit Allocation failed #54491
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
Comments
Can confirm. Not only does it crash, but it also cause the transpile time to increase by ~x6 before the crash happens. |
bisected to #53672 , cc @ahejlsberg |
The problem originates in type UnshiftTuple<T extends [...any[]]> = T extends [T[0], ...infer Tail] ? Tail : never;
type ExpandSmallerTuples<T extends [...any[]]> = T extends [T[0], ...infer Tail] ? T | ExpandSmallerTuples<Tail> : [];
type Shift<A extends Array<any>> = ((...args: A) => void) extends (...args: [A[0], ...infer R]) => void ? R : never;
type GrowExpRev<A extends Array<any>, N extends number, P extends Array<Array<any>>> = A['length'] extends N ? A : GrowExpRev<[...A, ...P[0]][N] extends undefined ? [...A, ...P[0]] : A, N, Shift<P>>;
type GrowExp<A extends Array<any>, N extends number, P extends Array<Array<any>>> = [...A, ...A][N] extends undefined ? GrowExp<[...A, ...A], N, [A, ...P]> : GrowExpRev<A, N, P>;
type Tuple<T, N extends number> = number extends N ? Array<T> : N extends 0 ? [] : N extends 1 ? [T] : GrowExp<[T], N, [[]]>;
declare class ArrayValidator<T extends unknown[], I = T[number]> {
lengthRange<S extends number, E extends number>(start: S, endBefore: E): ArrayValidator<Exclude<ExpandSmallerTuples<UnshiftTuple<[...Tuple<I, E>]>>, ExpandSmallerTuples<UnshiftTuple<[...Tuple<I, S>]>>>>;
} You can use this TS playground to verify this. Just wait for a moment - you will see that the browser tab crashes. I reduced this to the |
|
The issue appears to be runaway type instantiation as the compiler is attempting to compute variance information. That logic relies on our various depth limiters to catch infinite recursion, but apparently that doesn't happen fast enough here. A quick workaround for now is to change the declaration of the type Tuple<T, N extends number> = N extends number ? number extends N ? Array<T> : N extends 0 ? [] : N extends 1 ? [T] : GrowExp<[T], N, [[]]> : never; That is, introduce a sanity check that |
Isolated tabs are a wonderful thing. I still remember the days when this kind of thing would have taken out the entire browser. Still a bit alarming that any JS code is able to outright crash a process (and thereby cause a DoS), though. |
Many thanks for the fix that we can apply @ahejlsberg! We'll release an update for @sapphire/shapeshift ASAP so I think this issue can be closed. Edit: I have released @sapphire/shapeshift v3.9.2 that resolves this issue |
You guys are legends for fixing this so quickly, thanks to everyone involved! If my understanding is correct, we now wait for the next TS release where this is backported, or just use the nightly version? |
I also encountered this issue in my node discord.js project after upgrading from TypeScript 5.0.4 to 5.1.3. Enabling the |
As an npm {
"overrides": {
"@sapphire/shapeshift": "3.9.2"
}
} Then run Yarn v1 {
"resolutions": {
"@sapphire/shapeshift": "3.9.2"
}
} Then run Yarn v3 yarn up -R @sapphire/shapeshift pnpm pnpm --recursive update @sapphire/[email protected] |
Bug Report
π Search Terms
tsc, compiler, heap limit, crash, memory
π Version & Regression Information
β― Repository Link
https://github.com/virtuallyunknown/tsc-error
π» Code
To reproduce the error, clone the repository and run the build script or just
npx tsc
.π Actual behavior
Running the typescript compiler (
tsc
) from project root causes the build to fail and produce this error:I also tried
typescript@next
, but that doesn't fix it. I also tried this command:NODE_OPTIONS=--max-old-space-size=28000 npx tsc --noEmit --diagnostics --extendedDiagnostics --incremental false
but it crashes with a different error after a while.
π Expected behavior
Running the typescript compiler (
tsc
) command compiles the build successfully after 1-2 seconds, just like it did in5.0.4
.The text was updated successfully, but these errors were encountered: