Skip to content

Commit d46589e

Browse files
jborean93TylerLeonhardt
authored andcommitted
Fix uncaught exception when SafeToString returns null (#1140)
1 parent f20e5be commit d46589e

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ private static string GetValueStringAndType(object value, bool isExpandable, out
194194
else
195195
{
196196
string valueToString = value.SafeToString();
197-
if (valueToString.Equals(objType.ToString()))
197+
if (valueToString == null || valueToString.Equals(objType.ToString()))
198198
{
199-
// If the ToString() matches the type name, then display the type
199+
// If the ToString() matches the type name or is null, then display the type
200200
// name in PowerShell format.
201201
string shortTypeName = objType.Name;
202202

test/PowerShellEditorServices.Test.Shared/Debugging/VariableTest.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function Test-Variables {
1212
$classVar.Name = "Test"
1313
$classVar.Number = 42;
1414
$enumVar = $ErrorActionPreference
15+
$nullString = [NullString]::Value
1516
$psObjVar = New-Object -TypeName PSObject -Property @{Name = 'John'; Age = 75}
1617
$psCustomObjVar = [PSCustomObject] @{Name = 'Paul'; Age = 73}
1718
$procVar = Get-Process system

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,34 @@ await this.debugService.SetLineBreakpointsAsync(
815815
this.powerShellContext.AbortExecution();
816816
}
817817

818+
[Fact]
819+
public async Task DebufferVariableNullStringDisplaysCorrectly()
820+
{
821+
await this.debugService.SetLineBreakpointsAsync(
822+
this.variableScriptFile,
823+
new[] { BreakpointDetails.Create("", 18) });
824+
825+
// Execute the script and wait for the breakpoint to be hit
826+
Task executeTask =
827+
this.powerShellContext.ExecuteScriptStringAsync(
828+
this.variableScriptFile.FilePath);
829+
830+
await this.AssertDebuggerStopped(this.variableScriptFile.FilePath);
831+
832+
StackFrameDetails[] stackFrames = debugService.GetStackFrames();
833+
834+
VariableDetailsBase[] variables =
835+
debugService.GetVariables(stackFrames[0].LocalVariables.Id);
836+
837+
var nullStringVar = variables.FirstOrDefault(v => v.Name == "$nullString");
838+
Assert.NotNull(nullStringVar);
839+
Assert.True("[NullString]".Equals(nullStringVar.ValueString));
840+
Assert.True(nullStringVar.IsExpandable);
841+
842+
// Abort execution of the script
843+
this.powerShellContext.AbortExecution();
844+
}
845+
818846
[Fact]
819847
public async Task DebuggerVariablePSObjectDisplaysCorrectly()
820848
{

0 commit comments

Comments
 (0)