Skip to content

Commit 6e6012b

Browse files
committed
Fix missing OutputEvents in DebugAdapter
This change fixes an issue from the latest refactoring which causes the DebugAdapter to not send OutputEvents when output is written while script is running. This was reported in PowerShell/vscode-powershell#49. This change also introduces a new test to ensure that the DebugAdapter is writing output.
1 parent 9fa1fea commit 6e6012b

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public DebugAdapter(ChannelBase serverChannel) : base(serverChannel)
2929
this.editorSession = new EditorSession();
3030
this.editorSession.StartSession();
3131
this.editorSession.DebugService.DebuggerStopped += this.DebugService_DebuggerStopped;
32+
this.editorSession.PowerShellContext.OutputWritten += this.powerShellContext_OutputWritten;
3233
}
3334

3435
protected override void Initialize()
@@ -356,6 +357,17 @@ await this.SendEvent(
356357
});
357358
}
358359

360+
async void powerShellContext_OutputWritten(object sender, OutputWrittenEventArgs e)
361+
{
362+
await this.SendEvent(
363+
OutputEvent.Type,
364+
new OutputEventBody
365+
{
366+
Output = e.OutputText + (e.IncludeNewLine ? "\r\n" : string.Empty),
367+
Category = (e.OutputType == OutputType.Error) ? "stderr" : "stdout"
368+
});
369+
}
370+
359371
#endregion
360372
}
361373
}

test/PowerShellEditorServices.Test.Host/DebugAdapterTests.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ await this.SendRequest(
5959
{
6060
Path = DebugScriptPath
6161
},
62-
Lines = new int[] { 5, 9 }
62+
Lines = new int[] { 5, 7 }
6363
});
6464

6565
Task<StoppedEventBody> breakEventTask = this.WaitForEvent(StoppedEvent.Type);
@@ -74,7 +74,24 @@ await this.SendRequest(
7474
await this.SendRequest(ContinueRequest.Type, new object());
7575
stoppedDetails = await breakEventTask;
7676
Assert.Equal(DebugScriptPath, stoppedDetails.Source.Path);
77-
Assert.Equal(9, stoppedDetails.Line);
77+
Assert.Equal(7, stoppedDetails.Line);
78+
79+
// Abort script execution
80+
Task terminatedEvent = this.WaitForEvent(TerminatedEvent.Type);
81+
await this.SendRequest(DisconnectRequest.Type, new object());
82+
await terminatedEvent;
83+
}
84+
85+
[Fact]
86+
public async Task DebugAdapterReceivesOutputEvents()
87+
{
88+
Task<OutputEventBody> outputEventTask = this.WaitForEvent(OutputEvent.Type);
89+
await this.LaunchScript(DebugScriptPath);
90+
91+
// Wait for an output event
92+
OutputEventBody outputDetails = await outputEventTask;
93+
Assert.Equal("Output 1", outputDetails.Output);
94+
Assert.Equal("stdout", outputDetails.Category);
7895

7996
// Abort script execution
8097
Task terminatedEvent = this.WaitForEvent(TerminatedEvent.Type);
@@ -113,7 +130,11 @@ private Task<TParams> WaitForEvent<TParams>(EventType<TParams> eventType)
113130
eventType,
114131
(p, ctx) =>
115132
{
116-
eventTask.SetResult(p);
133+
if (!eventTask.Task.IsCompleted)
134+
{
135+
eventTask.SetResult(p);
136+
}
137+
117138
return Task.FromResult(true);
118139
},
119140
true); // Override any existing handler

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
Get-Process -bla
2-
3-
$i = 1
1+
$i = 1
42

53
while ($i -le 500000)
64
{

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public async Task DebuggerStopsOnBreakpoints()
103103
BreakpointDetails[] breakpoints =
104104
await this.debugService.SetBreakpoints(
105105
this.debugScriptFile,
106-
new int[] { 5, 9 });
106+
new int[] { 5, 7 });
107107
await this.AssertStateChange(PowerShellContextState.Ready);
108108

109109
Task executeTask =
@@ -114,7 +114,7 @@ await this.debugService.SetBreakpoints(
114114
await this.AssertDebuggerStopped(this.debugScriptFile.FilePath, 5);
115115
this.debugService.Continue();
116116

117-
await this.AssertDebuggerStopped(this.debugScriptFile.FilePath, 9);
117+
await this.AssertDebuggerStopped(this.debugScriptFile.FilePath, 7);
118118

119119
// Abort script execution early and wait for completion
120120
this.debugService.Abort();

0 commit comments

Comments
 (0)