Refresh diagnostics for open PowerShell docs#5402
Refresh diagnostics for open PowerShell docs#5402mdaneri wants to merge 3 commits intoPowerShell:mainfrom
Conversation
Force re-analysis of open PowerShell documents after the language server starts. Adds DidOpenTextDocument and DidCloseTextDocument notification types and a refreshOpenPowerShellDocumentDiagnostics() helper that filters open documents (powershell, file/untitled), logs the action, and sends didClose followed by didOpen (including languageId, version and text) to the language client. The method is invoked after the session reports it has started; it no-ops if the client isn't running or there are no matching open documents.
There was a problem hiding this comment.
Pull request overview
This PR addresses false diagnostics that appear when PowerShell documents are initially opened before the language server is fully initialized. The solution refreshes analysis for all open PowerShell documents once the language client reaches the Running state.
Changes:
- Added custom notification types for
textDocument/didOpenandtextDocument/didClose - Implemented
refreshOpenPowerShellDocumentDiagnostics()method to replay close/open events for already-open PowerShell documents - Called the refresh method after the language client is confirmed running during session startup
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Prefix this.languageClient.sendNotification calls with the void operator to explicitly discard the returned Promise and avoid floating-promise/unused-Promise warnings. Applied to notifications sent for DidCloseTextDocumentNotificationType and DidOpenTextDocumentNotificationType when iterating open PowerShell documents.
andyleejordan
left a comment
There was a problem hiding this comment.
If I'm reading this right, you're essentially sending mock close and open notifications for PowerShell docs after the LSP server starts. Which makes sense to work around the issue, but since that means we've identified the root case (these notifications are being sent too early) can we investigate if there are VS Code / LSP Client APIs to use to delay sending those notifications until we're ready? What I'm saying is, I suspect there's a more correct way to fix this.
|
I’m sure there’s a better approach. It just goes beyond what I know about how VS Code operates. |
|
I'll take a look. |
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.
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.
PR Summary
Related Issue
Fixes #5381
Testing
PR Checklist
Note: Tick the boxes below that apply to this pull request by putting an
xbetween the square brackets.Please mark anything not applicable to this PR
NA.WIP:to the beginning of the title and remove the prefix when the PR is ready