Fix incorrect parser errors from unloaded assemblies#5450
Fix incorrect parser errors from unloaded assemblies#5450andyleejordan wants to merge 1 commit intomainfrom
Conversation
An attempt to fix #5381 where we believe the cause is that the didOpen()/didChange() notifications are being sent before PowerShell has totally finished loading. Since these run the PowerShell parser on the OmniSharp thread pool, they essentially race the analysis of the files by PSScriptAnalyzer on the PowerShell runspace pool. So when they beat it, the return errors. But when they're beaten by it, the PSSA analysis has caused the assemblies (and so custom attributes) to be loaded, no longer erroring. I posited we could gate the notifications instead of duplicating them like in #5402, and if the `Middleware` works as suspected by me (and Claude) this should fix it. Morever, we now also use a proper `Promise` instead of a while loop around a sleep to wait for the LSP server to be running. While all of this could just be an AI hallucination...it seems right.
0beff82 to
5d652ac
Compare
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent false parser diagnostics on initial document open (notably unresolved custom attribute types) by delaying textDocument/didOpen and textDocument/didChange until the PowerShell session is fully running, addressing the race described in #5381.
Changes:
- Introduces a promise-based “started” gate that resolves when the session reaches
Running. - Replaces the
waitUntilStarted()polling loop with awaiting the gate promise. - Adds LanguageClient middleware hooks to delay
didOpen/didChangenotifications until startup completes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -328,6 +334,8 @@ export class SessionManager implements Middleware { | |||
| } | |||
|
|
|||
| this.languageClient = undefined; | |||
There was a problem hiding this comment.
stop() replaces this.started with a new resolver without resolving/rejecting the previous promise. Any callers already awaiting the old this.started.promise (e.g., waitUntilStarted() or an in-flight middleware didOpen/didChange) can hang forever, even after a successful restart. Consider making the gate lifecycle explicit: create a new deferred at the beginning of each start() attempt, resolve it on Running, and reject/resolve it on stop/failure before replacing it so existing awaiters are unblocked.
| this.languageClient = undefined; | |
| this.languageClient = undefined; | |
| const started = this.started; | |
| started.resolve(); |
| // eslint-disable-next-line @typescript-eslint/no-invalid-void-type | ||
| private started = Promise.withResolvers<void>(); |
There was a problem hiding this comment.
The eslint-disable @typescript-eslint/no-invalid-void-type is only needed because the generic is void. If you switch the deferred to use undefined (or a small Deferred<T> helper type), you can avoid the lint suppression and keep the type intent the same (a promise that resolves with no value).
| // eslint-disable-next-line @typescript-eslint/no-invalid-void-type | |
| private started = Promise.withResolvers<void>(); | |
| private started = Promise.withResolvers<undefined>(); |
An attempt to fix #5381 where we believe the cause is that the didOpen()/didChange() notifications are being sent before PowerShell has totally finished loading.
Since these run the PowerShell parser on the OmniSharp thread pool, they essentially race the analysis of the files by PSScriptAnalyzer on the PowerShell runspace pool. So when they beat it, the return errors. But when they're beaten by it, the PSSA analysis has caused the assemblies (and so custom attributes) to be loaded, no longer erroring.
I posited we could gate the notifications instead of duplicating them like in #5402, and if the
Middlewareworks as suspected by me (and Claude) this should fix it. Morever, we now also use a properPromiseinstead of a while loop around a sleep to wait for the LSP server to be running.While all of this could just be an AI hallucination...it seems right.
@mdaneri can you please test this against your scenario?