Skip to content

Commit 2037bd1

Browse files
committed
Code action requests from other extensions crash the language server
This change fixes PowerShell/vscode-powershell#408 which states that code action requests sent for diagnostic markers created by other extensions cause our language server to crash. This was due to us not handling the case where irrelevant diagnostics might (mistakenly?) get sent to us to resolve their code actions.
1 parent 1fce255 commit 2037bd1

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/Diagnostics.cs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public class Diagnostic
7171
/// Gets or sets the diagnostic message.
7272
/// </summary>
7373
public string Message { get; set; }
74+
75+
/// <summary>
76+
/// Gets or sets the source of the diagnostic message.
77+
/// </summary>
78+
public string Source { get; set; }
7479
}
7580
}
7681

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server
2727
public class LanguageServer : LanguageServerBase
2828
{
2929
private static CancellationTokenSource existingRequestCancellation;
30+
private readonly static string DiagnosticSourceName = "PowerShellEditorServices";
3031

3132
private bool profilesLoaded;
3233
private EditorSession editorSession;
@@ -994,7 +995,9 @@ protected async Task HandleCodeActionRequest(
994995
{
995996
foreach (var diagnostic in codeActionParams.Context.Diagnostics)
996997
{
997-
if (markerIndex.TryGetValue(diagnostic.Code, out correction))
998+
if (string.Equals(diagnostic.Source, DiagnosticSourceName, StringComparison.CurrentCultureIgnoreCase) &&
999+
!string.IsNullOrEmpty(diagnostic.Code) &&
1000+
markerIndex.TryGetValue(diagnostic.Code, out correction))
9981001
{
9991002
codeActionCommands.Add(
10001003
new CodeActionCommand
@@ -1335,9 +1338,9 @@ private static Diagnostic GetDiagnosticFromMarker(ScriptFileMarker scriptFileMar
13351338
Severity = MapDiagnosticSeverity(scriptFileMarker.Level),
13361339
Message = scriptFileMarker.Message,
13371340
Code = Guid.NewGuid().ToString(),
1341+
Source = DiagnosticSourceName,
13381342
Range = new Range
13391343
{
1340-
// TODO: What offsets should I use?
13411344
Start = new Position
13421345
{
13431346
Line = scriptFileMarker.ScriptRegion.StartLineNumber - 1,

0 commit comments

Comments
 (0)