Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ public async Task StartAsync()

// Set the workspace path from the parameters.
WorkspaceService workspaceService = languageServer.Services.GetService<WorkspaceService>();
if (initializeParams.WorkspaceFolders is not null)
{
workspaceService.WorkspaceFolders.AddRange(initializeParams.WorkspaceFolders);
}
workspaceService.AddWorkspaceFolders(initializeParams.WorkspaceFolders);

// Parse initialization options.
JObject initializationOptions = initializeParams.InitializationOptions as JObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ public WorkspaceService(ILoggerFactory factory)

public IEnumerable<string> WorkspacePaths => WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath());

/// <summary>
/// Adds the given workspace folders, ignoring any that are null or lack a URI.
/// </summary>
/// <remarks>
/// Some LSP clients send workspace folders without a URI on initialize. Adding such a
/// folder would later throw a <see cref="NullReferenceException"/> when its URI is
/// dereferenced (e.g. when resolving the initial working directory or a relative path),
/// breaking the handshake. See
/// https://github.com/PowerShell/PowerShellEditorServices/issues/2300.
/// </remarks>
public void AddWorkspaceFolders(IEnumerable<WorkspaceFolder> workspaceFolders)
{
foreach (WorkspaceFolder workspaceFolder in GetValidWorkspaceFolders(workspaceFolders))
{
WorkspaceFolders.Add(workspaceFolder);
}
}
Comment thread
andyleejordan marked this conversation as resolved.
Resolved

/// <summary>
/// Filters workspace folders down to those usable by the service: non-null folders with a
/// non-null URI. The <c>workspaceFolders</c> field is optional in LSP, so the collection
/// itself may also be null.
/// </summary>
/// <param name="workspaceFolders">The workspace folders from the initialize parameters.</param>
/// <returns>The folders that have a non-null URI, or an empty sequence.</returns>
internal static IEnumerable<WorkspaceFolder> GetValidWorkspaceFolders(IEnumerable<WorkspaceFolder> workspaceFolders)
=> workspaceFolders?.Where(static folder => folder?.Uri is not null)
?? Enumerable.Empty<WorkspaceFolder>();

/// <summary>
/// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it.
/// <para>IMPORTANT: Not all documents have a backing file e.g. untitled: scheme documents. Consider using
Expand Down
51 changes: 51 additions & 0 deletions test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,57 @@ internal static WorkspaceService FixturesWorkspace()
};
}

// Regression test for https://github.com/PowerShell/PowerShellEditorServices/issues/2300:
// a client that sends a workspace folder without a URI on initialize used to crash the
// server with a NullReferenceException when the URI was later dereferenced.
[Fact]
public void AddWorkspaceFoldersIgnoresNullAndUrilessFolders()
{
WorkspaceService workspace = new(NullLoggerFactory.Instance);

// Null collection is a no-op rather than a throw.
workspace.AddWorkspaceFolders(null);
Assert.Empty(workspace.WorkspaceFolders);

workspace.AddWorkspaceFolders(new[]
{
new WorkspaceFolder { Uri = DocumentUri.FromFileSystemPath(s_workspacePath), Name = "valid" },
new WorkspaceFolder { Name = "missing-uri" },
null
});

WorkspaceFolder folder = Assert.Single(workspace.WorkspaceFolders);
Assert.Equal("valid", folder.Name);

// The downstream dereferences that previously threw now succeed.
Assert.Equal(s_workspacePath, Assert.Single(workspace.WorkspacePaths));
}

[Fact]
public void GetValidWorkspaceFoldersReturnsEmptyWhenNull()
=> Assert.Empty(WorkspaceService.GetValidWorkspaceFolders(null));

[Fact]
public void GetValidWorkspaceFoldersReturnsEmptyWhenEmpty()
=> Assert.Empty(WorkspaceService.GetValidWorkspaceFolders(new Container<WorkspaceFolder>()));

[Fact]
public void GetValidWorkspaceFoldersSkipsNullFoldersAndNullUris()
{
WorkspaceFolder valid = new()
{
Uri = DocumentUri.FromFileSystemPath(s_workspacePath),
Name = "valid"
};

Container<WorkspaceFolder> folders = new(
null,
new WorkspaceFolder { Name = "missing-uri" },
valid);

Assert.Equal(valid, Assert.Single(WorkspaceService.GetValidWorkspaceFolders(folders)));
}

[Fact]
public void HasDefaultForWorkspacePaths()
{
Expand Down
Loading