This repository was archived by the owner on Nov 21, 2025. It is now read-only.
Commit f3a1c76
fix: prevent project from closing when only a template file is open
After opening a file, TypeScript does a cleanup step to remove/close
"orphaned" projects: https://github.com/microsoft/TypeScript/blob/a894f8ad2b0ff35b1bcfd91defba994136c68b18/src/server/editorServices.ts#L3337
As part of this, TS checks to make sure there are no open files for the
configured project: https://github.com/microsoft/TypeScript/blob/a894f8ad2b0ff35b1bcfd91defba994136c68b18/src/server/project.ts#L2333
If the only open file for the project is an HTML file, TS will not know
about it because we do not register those with the project (instead, we
associate the template file's `ScriptInfo` with the project, but do not
make the association in the other direction).
Because we do not have this two way association between the project and
the HTML `ScriptInfo`s, TypeScript closes the configured project on
actions like "go to definition" that open a `d.ts` file outside the
project. This is problematic for two particular reasons:
1. The project should not be closed in the first place. We will take a
performance hit when the user inevitably goes back to the template file
because now the project has to be re-opened.
2. The template file's `ScriptInfo` still tracks the closed project in
its `containingProjects` array. As part of the cleanup for closing a
project, TS would detach the `ScriptInfo`s from the project
(https://github.com/microsoft/TypeScript/blob/a894f8ad2b0ff35b1bcfd91defba994136c68b18/src/server/project.ts#L752)
but again, because we don't have the two-way association as mentioned
above, this does not happen. This results in a crash the next time a
request is made from the template file.
This commit addresses both of the issues above.
First, we prevent the project from being closed by creating an external project associated
with the configured project for the template file. When
TypeScript checks if a project has open refs/files, it returns `true` if
there are external project references
(https://github.com/microsoft/TypeScript/blob/a894f8ad2b0ff35b1bcfd91defba994136c68b18/src/server/project.ts#L2334).
This means that the clean up after opening a new file process as
mentioned above will not result in our project for the template being
closed.
Second, when retrieving a script info for a file we check if the project
as been closed. If it has, we detach the script info from the project,
log an error, and simply return `undefined`. To our knowledge, this
should not happen because of the first solution we implemented, but it
would prevent future crashes if there are other scenarios we missed.1 parent 65be7da commit f3a1c76
2 files changed
+103
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
84 | 120 | | |
85 | 121 | | |
86 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
307 | 308 | | |
308 | 309 | | |
309 | 310 | | |
| 311 | + | |
310 | 312 | | |
311 | 313 | | |
312 | 314 | | |
| |||
377 | 379 | | |
378 | 380 | | |
379 | 381 | | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
380 | 399 | | |
381 | 400 | | |
382 | 401 | | |
383 | 402 | | |
384 | 403 | | |
385 | 404 | | |
386 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
387 | 438 | | |
388 | 439 | | |
389 | 440 | | |
| |||
519 | 570 | | |
520 | 571 | | |
521 | 572 | | |
522 | | - | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
531 | 577 | | |
532 | 578 | | |
533 | 579 | | |
| |||
592 | 638 | | |
593 | 639 | | |
594 | 640 | | |
595 | | - | |
596 | | - | |
| 641 | + | |
| 642 | + | |
597 | 643 | | |
598 | 644 | | |
599 | 645 | | |
| |||
725 | 771 | | |
726 | 772 | | |
727 | 773 | | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
0 commit comments