From 8212c3a5134f1715bf7aef0b740fb78ffcbc0884 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sun, 14 Feb 2016 17:27:19 -0700 Subject: [PATCH] Fixes #155 - selecting outermost scope crashes debug host. Because we use AddScript, that adds another scope not associated with any filename. That resulted in a StackFrame ScriptPath with a null value. Later, VSCode sent us a request for soruce with a sourceReference value of null. That caused the JSON deserialization of that message to throw and that crashed the debug host. I made two changes. First, when we construct the StackFrameDetails, if the PowerShell CallStackFrame.ScriptName is null, we use the string "" instead for ScriptPath. The second change was to ExecuteScriptAtPath. Essentially, if you do not pass args we now use AddCommand(scriptPath) which does not add the extra stack frame. If you do use args then we still use AddScript. You will see the extra outermost stack frame of ", ". What is somewhat useful about this is that you can open Script variables to look at $MyInvocation.Line to see how the script was invoked with the args. --- .../Debugging/StackFrameDetails.cs | 2 +- .../Session/PowerShellContext.cs | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/PowerShellEditorServices/Debugging/StackFrameDetails.cs b/src/PowerShellEditorServices/Debugging/StackFrameDetails.cs index d228a32bf..fe4a24f32 100644 --- a/src/PowerShellEditorServices/Debugging/StackFrameDetails.cs +++ b/src/PowerShellEditorServices/Debugging/StackFrameDetails.cs @@ -64,7 +64,7 @@ static internal StackFrameDetails Create( { return new StackFrameDetails { - ScriptPath = callStackFrame.ScriptName, + ScriptPath = callStackFrame.ScriptName ?? "", FunctionName = callStackFrame.FunctionName, LineNumber = callStackFrame.Position.StartLineNumber, ColumnNumber = callStackFrame.Position.StartColumnNumber, diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index d869e57e0..d0aa07666 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -455,18 +455,22 @@ public async Task> ExecuteScriptString( /// A Task that can be awaited for completion. public async Task ExecuteScriptAtPath(string scriptPath, string arguments = null) { - // If we don't escape wildcard characters in the script path, the script can - // fail to execute if say the script name was foo][.ps1. - // Related to issue #123. - string escapedScriptPath = EscapePath(scriptPath, escapeSpaces: true); + PSCommand command = new PSCommand(); if (arguments != null) { - escapedScriptPath += " " + arguments; - } + // If we don't escape wildcard characters in the script path, the script can + // fail to execute if say the script name was foo][.ps1. + // Related to issue #123. + string escapedScriptPath = EscapePath(scriptPath, escapeSpaces: true); + string scriptWithArgs = escapedScriptPath + " " + arguments; - PSCommand command = new PSCommand(); - command.AddScript(escapedScriptPath); + command.AddScript(scriptWithArgs); + } + else + { + command.AddCommand(scriptPath); + } await this.ExecuteCommand(command, true); }