Skip to content

Commit 2d19bf3

Browse files
rkeithhilldaviwil
authored andcommitted
Fix bug where bg of -1 causes crash of interactive session.
Fix bug where the user can select the bottom most stack frame during interactive session debugging and that generates an error in VSCode when it can't find <No File>. Fix PowerShell#555 ParameterBindingValidationException when invoking PSSA on an empty PS1 file.
1 parent b536aff commit 2d19bf3

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

src/PowerShellEditorServices.Protocol/DebugAdapter/StackFrame.cs

+17-8
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,28 @@ public static StackFrame Create(
6161
var sourcePresentationHint =
6262
stackFrame.IsExternalCode ? SourcePresentationHint.Deemphasize : SourcePresentationHint.Normal;
6363

64+
// When debugging an interactive session, the ScriptPath is <No File> which is not a valid source file.
65+
// We need to make sure the user can't open the file associated with this stack frame.
66+
// It will generate a VSCode error in this case.
67+
Source source = null;
68+
if (!stackFrame.ScriptPath.Contains("<"))
69+
{
70+
source = new Source
71+
{
72+
Path = stackFrame.ScriptPath,
73+
PresentationHint = sourcePresentationHint.ToString().ToLower()
74+
};
75+
}
76+
6477
return new StackFrame
6578
{
6679
Id = id,
67-
Name = stackFrame.FunctionName,
68-
Line = stackFrame.StartLineNumber,
80+
Name = (source != null) ? stackFrame.FunctionName : "Interactive Session",
81+
Line = (source != null) ? stackFrame.StartLineNumber : 0,
6982
EndLine = stackFrame.EndLineNumber,
70-
Column = stackFrame.StartColumnNumber,
83+
Column = (source != null) ? stackFrame.StartColumnNumber : 0,
7184
EndColumn = stackFrame.EndColumnNumber,
72-
Source = new Source
73-
{
74-
Path = stackFrame.ScriptPath,
75-
PresentationHint = sourcePresentationHint.ToString().ToLower()
76-
}
85+
Source = source
7786
};
7887
}
7988
}

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected Task LaunchScript(RequestContext<object> requestContext)
8686
// Is this an untitled script?
8787
Task launchTask = null;
8888

89-
if (this.scriptToLaunch.StartsWith("untitled"))
89+
if (this.scriptToLaunch.StartsWith("untitled:"))
9090
{
9191
ScriptFile untitledScript =
9292
this.editorSession.Workspace.GetFile(
@@ -253,6 +253,12 @@ protected async Task HandleLaunchRequest(
253253
launchParams.Program;
254254
#pragma warning restore 618
255255

256+
// We come through here first when debugging an untitled (unsaved) file - there is now working dir.
257+
if (workingDir.StartsWith("untitled:"))
258+
{
259+
workingDir = null;
260+
}
261+
256262
if (workingDir != null)
257263
{
258264
workingDir = PowerShellContext.UnescapePath(workingDir);

src/PowerShellEditorServices/Analysis/AnalysisService.cs

+8
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,14 @@ private async Task<PSObject[]> GetDiagnosticRecordsAsync<TSettings>(
413413
{
414414
var diagnosticRecords = new PSObject[0];
415415

416+
// When a new, empty file is created there are by definition no issues.
417+
// Furthermore, if you call Invoke-ScriptAnalyzer with an empty ScriptDefinition
418+
// it will generate a ParameterBindingValidationException.
419+
if (string.IsNullOrEmpty(scriptContent))
420+
{
421+
return diagnosticRecords;
422+
}
423+
416424
if (hasScriptAnalyzerModule
417425
&& (typeof(TSettings) == typeof(string)
418426
|| typeof(TSettings) == typeof(Hashtable)))

src/PowerShellEditorServices/Session/Host/TerminalPSHostUserInterface.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public override void WriteOutput(
131131
ConsoleColor oldBackgroundColor = System.Console.BackgroundColor;
132132

133133
System.Console.ForegroundColor = foregroundColor;
134-
System.Console.BackgroundColor = backgroundColor;
134+
System.Console.BackgroundColor = ((int)backgroundColor != -1) ? backgroundColor : oldBackgroundColor;
135135

136136
System.Console.Write(outputString + (includeNewLine ? Environment.NewLine : ""));
137137

0 commit comments

Comments
 (0)