From 009dba470b6c510fa351955ee5432f1f4f570dc2 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 6 Apr 2017 21:15:43 -0700 Subject: [PATCH] Fix occasional crash when requesting IntelliSense while debugging This change fixes an issue where calling TabExpansion2 while debugging will sometimes cause a crash due to it returning an ErrorRecord instead of a CommandCompletion. The fix is to expect the possibility of the ErrorRecord and log it when it occurs. Resolves #427. --- .../Language/AstOperations.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Language/AstOperations.cs b/src/PowerShellEditorServices/Language/AstOperations.cs index aa27bead4..67533903a 100644 --- a/src/PowerShellEditorServices/Language/AstOperations.cs +++ b/src/PowerShellEditorServices/Language/AstOperations.cs @@ -90,9 +90,24 @@ static public async Task GetCompletions( command.AddParameter("PositionOfCursor", cursorPosition); command.AddParameter("Options", null); - commandCompletion = - (await powerShellContext.ExecuteCommand(command, false, false)) + PSObject outputObject = + (await powerShellContext.ExecuteCommand(command, false, false)) .FirstOrDefault(); + + if (outputObject != null) + { + ErrorRecord errorRecord = outputObject.BaseObject as ErrorRecord; + if (errorRecord != null) + { + Logger.WriteException( + "Encountered an error while invoking TabExpansion2 in the debugger", + errorRecord.Exception); + } + else + { + commandCompletion = outputObject.BaseObject as CommandCompletion; + } + } } else if (powerShellContext.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.Available)