Skip to content

Fix NullReferenceException in OnInitialize when initialize includes workspaceFolders#2305

Closed
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-nullreferenceexception-linux
Closed

Fix NullReferenceException in OnInitialize when initialize includes workspaceFolders#2305
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-nullreferenceexception-linux

Conversation

Copilot AI commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

An LSP initialize whose params include a workspaceFolders array could throw a NullReferenceException inside PsesLanguageServer.OnInitialize, leaving the handshake incomplete and the server unusable for clients that send workspaceFolders.

The handler added every folder verbatim and later dereferenced WorkspaceFolder.Uri (in OnInitialize and in WorkspaceService.GetRelativePath/FindFileInWorkspace/WorkspacePaths). An absent/uninitialized collection or a folder entry without a Uri therefore produced an NRE.

Changes

  • PsesLanguageServer.GetValidWorkspaceFolders (new internal helper): treats an absent collection or any folder lacking a Uri as "no folder", filtering them out before AddRange so nothing with a null Uri reaches the workspace.
  • OnInitialize: feeds workspace folders through the helper instead of adding them directly.
  • InitialWorkingDirectory fallback: null-guards the Uri dereference (FirstOrDefault()?.Uri?.GetFileSystemPath()).
  • Tests: cover null collection, empty collection, and a mix of null folder / null-Uri folder / valid folder.
internal static IEnumerable<WorkspaceFolder> GetValidWorkspaceFolders(IEnumerable<WorkspaceFolder> workspaceFolders)
    => workspaceFolders?.Where(static folder => folder?.Uri is not null)
        ?? Enumerable.Empty<WorkspaceFolder>();

Copilot AI changed the title [WIP] Fix NullReferenceException in OnInitialize for Linux with workspaceFolders Fix NullReferenceException in OnInitialize when initialize includes workspaceFolders Jun 15, 2026
Copilot AI requested a review from andyleejordan June 15, 2026 21:53
@andyleejordan

Copy link
Copy Markdown
Member

Closing in favor of #2308, which consolidates the fix for #2300 (filters null/URI-less workspace folders at the WorkspaceService ingestion point, with direct unit tests).

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.

2 participants