Repository: deno
Issue: LSP hangs and reaches heap limit
Labels: lsp

Version: Deno 1.46.3, 1.46.2, ...?

I've encountered this bug in large repo durign different code-editing scenarios, but it was hard to determine where the issue origins from. I've managed to create a [repro repo](https://github.com/albnnc/deno-repro-20240913), but LSP hangs only after a particular code edit. Note that

- the file didn't get saved, LSP has got stuck only after typing what I've typed;
- the issue reproduces (almost?) every time you type shown changes.

https://github.com/user-attachments/assets/81353b43-4cf1-4ebc-9404-d4ca73458709

<details>
<summary>Complete LS log after heap limit has been reached</summary>

```
Starting Deno language server...
  version: 1.46.3 (release, aarch64-apple-darwin)
  executable: /Users/albnnc/.deno/bin/deno
Connected to "Visual Studio Code" 1.93.1
Enabling import suggestions for: https://deno.land
Refreshing configuration tree...
  Resolved Deno configuration file: "file:///Users/albnnc/Code/deno-repro-130924/deno.json"
  Resolved .npmrc: "/Users/albnnc/.npmrc"
  Resolved lockfile: "file:///Users/albnnc/Code/deno-repro-130924/deno.lock"
Server ready.
[Info  - 14:13:38] Connection to server got closed. Server will restart.
Starting Deno language server...
  version: 1.46.3 (release, aarch64-apple-darwin)
  executable: /Users/albnnc/.deno/bin/deno
Connected to "Visual Studio Code" 1.93.1
Enabling import suggestions for: https://deno.land
Refreshing configuration tree...
  Resolved Deno configuration file: "file:///Users/albnnc/Code/deno-repro-130924/deno.json"
  Resolved .npmrc: "/Users/albnnc/.npmrc"
  Resolved lockfile: "file:///Users/albnnc/Code/deno-repro-130924/deno.lock"
[Error - 14:13:38] Request textDocument/codeAction failed.
  Message: The position is out of range.
  Code: -32602 
Server ready.

<--- Last few GCs --->

[46676:0x128008000]   154815 ms: Scavenge (interleaved) 9994.6 (10003.9) -> 9994.5 (10004.9) MB, pooled: 0 MB, 26.92 / 0.00 ms  (average mu = 0.390, current mu = 0.239) allocation failure; 
[46676:0x128008000]   156188 ms: Mark-Compact 9995.5 (10004.9) -> 9995.3 (10010.9) MB, pooled: 0 MB, 1371.21 / 0.00 ms  (average mu = 0.234, current mu = 0.033) allocation failure; scavenge might not succeed


<--- JS stacktrace --->



#
# Fatal JavaScript out of memory: Reached heap limit
#
==== C stack trace ===============================

    0   deno                                0x00000001018d686c v8::base::debug::StackTrace::StackTrace() + 24
    1   deno                                0x00000001018dc214 v8::platform::(anonymous namespace)::PrintStackTrace() + 24
    2   deno                                0x00000001018d36b4 v8::base::FatalOOM(v8::base::OOMType, char const*) + 68
    3   deno                                0x0000000101925e34 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) + 616
    4   deno                                0x0000000101aeea18 v8::internal::Heap::stack() + 0
    5   deno                                0x0000000101aecd8c v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) + 916
    6   deno                                0x0000000101ab7540 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) + 2292
    7   deno                                0x0000000101ab7f2c v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) + 44
    8   deno                                0x0000000101a9f144 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) + 624
    9   deno                                0x0000000101ebf1e0 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) + 224
    10  deno                                0x0000000102c4f9f4 Builtins_CEntry_Return1_ArgvOnStack_NoBuiltinExit + 84
    11  deno                                0x0000000102bbb62c Builtins_GrowFastSmiOrObjectElements + 332
    12  ???                                 0x0000000106d1525c 0x0 + 4409348700
    13  ???                                 0x00000001072f0840 0x0 + 4415490112
    14  ???                                 0x000000010736ddfc 0x0 + 4416003580
    15  ???                                 0x0000000106d14d48 0x0 + 4409347400
    16  ???                                 0x00000001072f5f80 0x0 + 4415512448
    17  ???                                 0x000000010736ddfc 0x0 + 4416003580
    18  ???                                 0x0000000107331bc8 0x0 + 4415757256
    19  ???                                 0x0000000106d98814 0x0 + 4409886740
    20  ???                                 0x000000010736ddfc 0x0 + 4416003580
    21  ???                                 0x0000000106d98a14 0x0 + 4409887252
    22  ???                                 0x000000010736ddfc 0x0 + 4416003580
    23  ???                                 0x0000000106d98b4c 0x0 + 4409887564
    24  ???                                 0x000000010736ddfc 0x0 + 4416003580
    25  ???                                 0x0000000106d8cbe4 0x0 + 4409838564
    26  ???                                 0x000000010736ddfc 0x0 + 4416003580
    27  ???                                 0x0000000106d14d48 0x0 + 4409347400
    28  ???                                 0x00000001072f3160 0x0 + 4415500640
    29  ???                                 0x000000010736ddfc 0x0 + 4416003580
    30  ???                                 0x0000000106d8cbe4 0x0 + 4409838564
    31  ???                                 0x000000010736ddfc 0x0 + 4416003580
    32  ???                                 0x000000010728cc04 0x0 + 4415081476
    33  ???                                 0x000000010736ddfc 0x0 + 4416003580
    34  ???                                 0x0000000106d14d48 0x0 + 4409347400
    35  ???                                 0x0000000106d00340 0x0 + 4409262912
    36  ???                                 0x000000010736ddfc 0x0 + 4416003580
    37  ???                                 0x0000000106d98a14 0x0 + 4409887252
    38  ???                                 0x000000010736ddfc 0x0 + 4416003580
    39  ???                                 0x0000000106d98b4c 0x0 + 4409887564
    40  ???                                 0x000000010736ddfc 0x0 + 4416003580
    41  ???                                 0x0000000106d98b4c 0x0 + 4409887564
    42  ???                                 0x000000010736ddfc 0x0 + 4416003580
    43  ???                                 0x0000000107355ce4 0x0 + 4415904996
    44  ???                                 0x000000010736ddfc 0x0 + 4416003580
    45  ???                                 0x0000000106d14d48 0x0 + 4409347400
    46  ???                                 0x0000000106d00340 0x0 + 4409262912
    47  ???                                 0x000000010736ddfc 0x0 + 4416003580
    48  ???                                 0x0000000106d98b4c 0x0 + 4409887564
    49  ???                                 0x000000010736ddfc 0x0 + 4416003580
    50  ???                                 0x0000000106d98a14 0x0 + 4409887252
    51  ???                                 0x000000010736ddfc 0x0 + 4416003580
    52  ???                                 0x0000000107355ce4 0x0 + 4415904996
    53  ???                                 0x000000010736ddfc 0x0 + 4416003580
    54  ???                                 0x0000000106d14d48 0x0 + 4409347400
    55  ???                                 0x0000000106d00340 0x0 + 4409262912
    56  ???                                 0x000000010736ddfc 0x0 + 4416003580
    57  ???                                 0x0000000106d98a14 0x0 + 4409887252
    58  ???                                 0x000000010736ddfc 0x0 + 4416003580
    59  ???                                 0x0000000106d98a14 0x0 + 4409887252
    60  ???                                 0x000000010736ddfc 0x0 + 4416003580
    61  ???                                 0x0000000107355ce4 0x0 + 4415904996
[Info  - 14:16:14] Connection to server got closed. Server will restart.
Starting Deno language server...
  version: 1.46.3 (release, aarch64-apple-darwin)
  executable: /Users/albnnc/.deno/bin/deno
Connected to "Visual Studio Code" 1.93.1
Enabling import suggestions for: https://deno.land
Refreshing configuration tree...
  Resolved Deno configuration file: "file:///Users/albnnc/Code/deno-repro-130924/deno.json"
  Resolved .npmrc: "/Users/albnnc/.npmrc"
  Resolved lockfile: "file:///Users/albnnc/Code/deno-repro-130924/deno.lock"
Server ready.
```
</details>