OptionalChainNotNullableCheck causes heap out of memory error

This issue has been tracked since 2022-09-18.

Which @angular/* package(s) are the source of the bug?

compiler-cli

Is this a regression?

Yes

Description

When I run ng serve on the reproduction repo, I get a "JavaScript heap out of memory error". I think this is due to this change: #46686.

Please provide a link to a minimal reproduction of the bug

https://github.com/sightsoundtheatres/heap-limit-repro

Please provide the exception or error you saw

⠸ Generating browser application bundles (phase: building)...
<--- Last few GCs --->

[31320:0x150078000]    22441 ms: Mark-sweep 4041.8 (4134.5) -> 4031.8 (4139.2) MB, 943.8 / 0.0 ms  (average mu = 0.218, current mu = 0.022) allocation failure; scavenge might not succeed
[31320:0x150078000]    23917 ms: Mark-sweep 4049.9 (4141.5) -> 4040.1 (4147.7) MB, 1451.6 / 0.0 ms  (average mu = 0.105, current mu = 0.017) allocation failure; scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0x100bdddb8 node::Abort() [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 2: 0x100bddfa8 node::ModifyCodeGenerationFromStrings(v8::Local<v8::Context>, v8::Local<v8::Value>, bool) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 3: 0x100d23cb0 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 4: 0x100ecea18 v8::internal::EmbedderStackStateScope::EmbedderStackStateScope(v8::internal::Heap*, v8::internal::EmbedderStackStateScope::Origin, cppgc::EmbedderStackState) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 5: 0x100ecd4fc v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 6: 0x100ec17a4 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 7: 0x100ec1fd4 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 8: 0x100ea7e08 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
 9: 0x101237e14 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
10: 0x10158504c Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
11: 0x1015df1e8 Builtins_RegExpReplace [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
12: 0x101576ccc Builtins_StringPrototypeReplace [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
13: 0x106aaf0bc 
14: 0x106a9ff28 
15: 0x106aaf8d8 
16: 0x106ab3660 
17: 0x106aaf9a0 
18: 0x106ab3660 
19: 0x1061facbc 
20: 0x106213ee0 
21: 0x1068f7610 
22: 0x1062130d8 
23: 0x10694a3e4 
24: 0x106ab1c28 
25: 0x106a38094 
26: 0x1066ba8d0 
27: 0x106a4ca90 
28: 0x106a4cc30 
29: 0x106a4cd70 
30: 0x1015a3eec Builtins_ArrayMap [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
31: 0x101510198 Builtins_InterpreterEntryTrampoline [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
32: 0x101510198 Builtins_InterpreterEntryTrampoline [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
33: 0x101510198 Builtins_InterpreterEntryTrampoline [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
34: 0x10695d224 
35: 0x10695cc6c 
36: 0x106710774 
37: 0x1015d06f8 Builtins_PromiseFulfillReactionJob [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
38: 0x101533c4c Builtins_RunMicrotasks [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
39: 0x10150e3a4 Builtins_JSRunMicrotasksEntry [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
40: 0x100e50240 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
41: 0x100e50730 v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
42: 0x100e5090c v8::internal::Execution::TryRunMicrotasks(v8::internal::Isolate*, v8::internal::MicrotaskQueue*, v8::internal::MaybeHandle<v8::internal::Object>*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
43: 0x100e76fdc v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
44: 0x100e77874 v8::internal::MicrotaskQueue::PerformCheckpoint(v8::Isolate*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
45: 0x100d945d0 v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
46: 0x100d940cc v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
47: 0x100d938f8 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
48: 0x10158518c Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
49: 0x106561408 
50: 0x10150e4d0 Builtins_JSEntryTrampoline [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
51: 0x10150e164 Builtins_JSEntry [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
52: 0x100e50270 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
53: 0x100e4f7a4 v8::internal::Execution::Call(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, int, v8::internal::Handle<v8::internal::Object>*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
54: 0x100d3fcd8 v8::Function::Call(v8::Local<v8::Context>, v8::Local<v8::Value>, int, v8::Local<v8::Value>*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
55: 0x100b28d00 node::InternalCallbackScope::Close() [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
56: 0x100b28fc8 node::InternalMakeCallback(node::Environment*, v8::Local<v8::Object>, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
57: 0x100b292b0 node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
58: 0x100b84384 node::Environment::CheckImmediate(uv_check_s*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
59: 0x1014f5fcc uv__run_check [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
60: 0x1014efd28 uv_run [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
61: 0x100b296d4 node::SpinEventLoop(node::Environment*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
62: 0x100c18dcc node::NodeMainInstance::Run(int*, node::Environment*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
63: 0x100c18aa4 node::NodeMainInstance::Run() [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
64: 0x100bac57c node::LoadSnapshotDataAndRun(node::SnapshotData const**, node::InitializationResult*) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
65: 0x100bac754 node::Start(int, char**) [/Users/jared.bates/.nvm/versions/node/v18.9.0/bin/node]
66: 0x18e3c3e50 start [/usr/lib/dyld]
[1]    31320 abort      ng serve

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.1.3
Node: 16.16.0
Package Manager: npm 8.11.0 
OS: darwin arm64

Angular: 14.1.3
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1401.3
@angular-devkit/build-angular   14.1.3
@angular-devkit/core            14.1.3
@angular-devkit/schematics      14.1.3
@schematics/angular             14.1.3
rxjs                            7.5.5
typescript                      4.6.3

Anything else?

This repro is an example of how we use svg's in one of our internal applications. We need the svg's to be able to bind to properties on the component. We were just trying to upgrade to Angular 14 from 13 and saw this issue pop up.

JoostK wrote this answer on 2022-09-18

The new check triggers 2026 new diagnostics and we're not currently reusing a single TypeScript source file to report the errors on, which eats a lot of RAM because that template is huge. The root cause is not the optionalChainNotNullable check by itself; disabling that rule in the repro still OOMs as there are plenty other errors in that template (likely because the necessary backing component code was omitted from the repro).

I'd suggest you disable the check using the following in tsconfig.json to get the error count down, therefore avoiding the OOM:

{
  // ...
  "angularCompilerOptions": {
    // ...
    "extendedDiagnostics": {
      "checks": {
        "optionalChainNotNullable": "suppress"
      }
    }
  }
JoostK wrote this answer on 2022-09-21

FYI, this is fixed in 15.0.0-next.3 but just missed the 14.2.3 release as it needed to be backported by hand. It will be in the next release, likely around the same time next week.

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-10-05
Star Count 84152
Watcher Count 3063
Fork Count 22247
Issue Count 1203

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date