Skip to content

Commit 8212c3a

Browse files
committed
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 "<No File>" 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 "<ScriptBlock>, <NoFile>". 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.
1 parent e094024 commit 8212c3a

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/PowerShellEditorServices/Debugging/StackFrameDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static internal StackFrameDetails Create(
6464
{
6565
return new StackFrameDetails
6666
{
67-
ScriptPath = callStackFrame.ScriptName,
67+
ScriptPath = callStackFrame.ScriptName ?? "<No File>",
6868
FunctionName = callStackFrame.FunctionName,
6969
LineNumber = callStackFrame.Position.StartLineNumber,
7070
ColumnNumber = callStackFrame.Position.StartColumnNumber,

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,22 @@ public async Task<IEnumerable<object>> ExecuteScriptString(
455455
/// <returns>A Task that can be awaited for completion.</returns>
456456
public async Task ExecuteScriptAtPath(string scriptPath, string arguments = null)
457457
{
458-
// If we don't escape wildcard characters in the script path, the script can
459-
// fail to execute if say the script name was foo][.ps1.
460-
// Related to issue #123.
461-
string escapedScriptPath = EscapePath(scriptPath, escapeSpaces: true);
458+
PSCommand command = new PSCommand();
462459

463460
if (arguments != null)
464461
{
465-
escapedScriptPath += " " + arguments;
466-
}
462+
// If we don't escape wildcard characters in the script path, the script can
463+
// fail to execute if say the script name was foo][.ps1.
464+
// Related to issue #123.
465+
string escapedScriptPath = EscapePath(scriptPath, escapeSpaces: true);
466+
string scriptWithArgs = escapedScriptPath + " " + arguments;
467467

468-
PSCommand command = new PSCommand();
469-
command.AddScript(escapedScriptPath);
468+
command.AddScript(scriptWithArgs);
469+
}
470+
else
471+
{
472+
command.AddCommand(scriptPath);
473+
}
470474

471475
await this.ExecuteCommand<object>(command, true);
472476
}

0 commit comments

Comments
 (0)