Skip to content

Commit 70d100b

Browse files
committed
Move startDebugger notification to PsesInternalHost.OnDebuggerStopped
1 parent 624fe30 commit 70d100b

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/PowerShellEditorServices/Services/PowerShell/Debugging/PowerShellDebugContext.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
using System;
55
using System.Management.Automation;
66
using System.Threading;
7+
using System.Threading.Tasks;
78
using Microsoft.Extensions.Logging;
89
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
9-
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
10-
using System.Threading.Tasks;
1110

1211
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging
1312
{
@@ -38,24 +37,30 @@ internal class PowerShellDebugContext : IPowerShellDebugContext
3837
{
3938
private readonly ILogger _logger;
4039

41-
private readonly ILanguageServerFacade _languageServer;
42-
4340
private readonly PsesInternalHost _psesHost;
4441

4542
public PowerShellDebugContext(
4643
ILoggerFactory loggerFactory,
47-
ILanguageServerFacade languageServer,
4844
PsesInternalHost psesHost)
4945
{
5046
_logger = loggerFactory.CreateLogger<PowerShellDebugContext>();
51-
_languageServer = languageServer;
5247
_psesHost = psesHost;
5348
}
5449

5550
public bool IsStopped { get; private set; }
5651

52+
/// <summary>
53+
/// Tracks the state of the LSP debug server (not the PowerShell debugger).
54+
/// </summary>
5755
public bool IsDebugServerActive { get; set; }
5856

57+
/// <summary>
58+
/// Tracks if the PowerShell session started the debug server itself (true), or if it was
59+
/// started by an LSP notification (false). Essentially, this marks if we're responsible for
60+
/// stopping the debug server (and thus need to send a notification to do so).
61+
/// </summary>
62+
public bool OwnsDebugServerState { get; set; }
63+
5964
public DebuggerStopEventArgs LastStopEventArgs { get; private set; }
6065

6166
public event Action<object, DebuggerStopEventArgs> DebuggerStopped;
@@ -165,13 +170,6 @@ public void HandleBreakpointUpdated(BreakpointUpdatedEventArgs breakpointUpdated
165170

166171
private void RaiseDebuggerStoppedEvent()
167172
{
168-
if (!IsDebugServerActive)
169-
{
170-
// NOTE: The language server is not necessarily connected, so this must be
171-
// conditional access. This shows up in unit tests.
172-
_languageServer?.SendNotification("powerShell/startDebugger");
173-
}
174-
175173
DebuggerStopped?.Invoke(this, LastStopEventArgs);
176174
}
177175

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using System;
@@ -112,7 +112,7 @@ public PsesInternalHost(
112112
Name = hostInfo.Name;
113113
Version = hostInfo.Version;
114114

115-
DebugContext = new PowerShellDebugContext(loggerFactory, languageServer, this);
115+
DebugContext = new PowerShellDebugContext(loggerFactory, this);
116116
UI = hostInfo.ConsoleReplEnabled
117117
? new EditorServicesConsolePSHostUserInterface(loggerFactory, _readLineProvider, hostInfo.PSHost.UI)
118118
: new NullPSHostUI();
@@ -852,7 +852,16 @@ private bool LastKeyWasCtrlC()
852852

853853
private void OnDebuggerStopped(object sender, DebuggerStopEventArgs debuggerStopEventArgs)
854854
{
855+
if (!DebugContext.IsDebugServerActive)
856+
{
857+
// If the we've hit a breakpoint and the debug server is not active, then we need to
858+
// start it (and own stopping it later).
859+
DebugContext.OwnsDebugServerState = true;
860+
_languageServer?.SendNotification("powerShell/startDebugger");
861+
}
862+
855863
DebugContext.SetDebuggerStopped(debuggerStopEventArgs);
864+
856865
try
857866
{
858867
CurrentPowerShell.WaitForRemoteOutputIfNeeded();

0 commit comments

Comments
 (0)