Skip to content

Commit 087f583

Browse files
committed
Fix #323: DSC IntelliSense can cause language service to crash
This change fixes an issue which appears sometimes when getting IntelliSense results within a DSC resource. The completion results return a replacement range of -1 which causes an exception in our BufferRange class. This fix catches and logs this exception so that the session may continue. Also fixes PowerShell/vscode-powershell#391.
1 parent ed05df2 commit 087f583

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/PowerShellEditorServices/Language/LanguageService.cs

+26-16
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,33 @@ await this.powerShellContext.GetRunspaceHandle(
102102

103103
if (commandCompletion != null)
104104
{
105-
CompletionResults completionResults =
106-
CompletionResults.Create(
107-
scriptFile,
108-
commandCompletion);
109-
110-
// save state of most recent completion
111-
mostRecentCompletions = completionResults;
112-
mostRecentRequestFile = scriptFile.Id;
113-
mostRecentRequestLine = lineNumber;
114-
mostRecentRequestOffest = columnNumber;
115-
116-
return completionResults;
117-
}
118-
else
119-
{
120-
return new CompletionResults();
105+
try
106+
{
107+
CompletionResults completionResults =
108+
CompletionResults.Create(
109+
scriptFile,
110+
commandCompletion);
111+
112+
// save state of most recent completion
113+
mostRecentCompletions = completionResults;
114+
mostRecentRequestFile = scriptFile.Id;
115+
mostRecentRequestLine = lineNumber;
116+
mostRecentRequestOffest = columnNumber;
117+
118+
return completionResults;
119+
}
120+
catch(ArgumentException e)
121+
{
122+
// Bad completion results could return an invalid
123+
// replacement range, catch that here
124+
Logger.Write(
125+
LogLevel.Error,
126+
$"Caught exception while trying to create CompletionResults:\n\n{e.ToString()}");
127+
}
121128
}
129+
130+
// If all else fails, return empty results
131+
return new CompletionResults();
122132
}
123133

124134
/// <summary>

0 commit comments

Comments
 (0)