Skip to content

Fix incorrect parser errors from unloaded assemblies#5450

Open
andyleejordan wants to merge 1 commit intomainfrom
gate-notifications
Open

Fix incorrect parser errors from unloaded assemblies#5450
andyleejordan wants to merge 1 commit intomainfrom
gate-notifications

Conversation

@andyleejordan
Copy link
Copy Markdown
Member

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.

@mdaneri can you please test this against your scenario?

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.
@andyleejordan andyleejordan marked this pull request as ready for review April 6, 2026 20:23
Copilot AI review requested due to automatic review settings April 6, 2026 20:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/didChange notifications 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;
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
this.languageClient = undefined;
this.languageClient = undefined;
const started = this.started;
started.resolve();

Copilot uses AI. Check for mistakes.
Comment on lines +131 to +132
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
private started = Promise.withResolvers<void>();
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
private started = Promise.withResolvers<void>();
private started = Promise.withResolvers<undefined>();

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PowerShell Custom Attribute Types Unresolved on Initial File Open

3 participants