Skip to content

Commit 3bd9a26

Browse files
committed
Merge pull request #178 from rkeithhill/rkeithhill/robust-launch-handling
This is a *proposed* change to address the issue that the launch requ…
2 parents 3605e55 + f1be4b6 commit 3bd9a26

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class DebugAdapter : DebugAdapterBase
2020
{
2121
private EditorSession editorSession;
2222
private OutputDebouncer outputDebouncer;
23+
private bool isConfigurationDoneRequestComplete;
24+
private bool isLaunchRequestComplete;
2325
private string scriptPathToLaunch;
2426
private string arguments;
2527

@@ -65,6 +67,23 @@ protected override void Initialize()
6567
this.SetRequestHandler(EvaluateRequest.Type, this.HandleEvaluateRequest);
6668
}
6769

70+
protected Task LaunchScript(RequestContext<object> requestContext)
71+
{
72+
return editorSession.PowerShellContext
73+
.ExecuteScriptAtPath(this.scriptPathToLaunch, this.arguments)
74+
.ContinueWith(
75+
async (t) => {
76+
Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");
77+
78+
await requestContext.SendEvent(
79+
TerminatedEvent.Type,
80+
null);
81+
82+
// Stop the server
83+
this.Stop();
84+
});
85+
}
86+
6887
protected override void Shutdown()
6988
{
7089
// Make sure remaining output is flushed before exiting
@@ -81,6 +100,23 @@ protected override void Shutdown()
81100

82101
#region Built-in Message Handlers
83102

103+
protected async Task HandleConfigurationDoneRequest(
104+
object args,
105+
RequestContext<object> requestContext)
106+
{
107+
// The order of debug protocol messages apparently isn't as guaranteed as we might like.
108+
// Need to be able to handle the case where we get the configurationDone request after the
109+
// launch request.
110+
if (this.isLaunchRequestComplete)
111+
{
112+
this.LaunchScript(requestContext);
113+
}
114+
115+
this.isConfigurationDoneRequestComplete = true;
116+
117+
await requestContext.SendResult(null);
118+
}
119+
84120
protected async Task HandleLaunchRequest(
85121
LaunchRequestArguments launchParams,
86122
RequestContext<object> requestContext)
@@ -114,14 +150,24 @@ protected async Task HandleLaunchRequest(
114150
Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
115151
}
116152

117-
// NOTE: We don't actually launch the script in response to this
118-
// request. We wait until we receive the configurationDone request
119-
// to actually launch the script under the debugger. This gives
120-
// us and VSCode a chance to finish configuring all the types of
121-
// breakpoints.
153+
// We may not actually launch the script in response to this
154+
// request unless it comes after the configurationDone request.
155+
// If the launch request comes first, then stash the launch
156+
// params so that the subsequent configurationDone request handler
157+
// can launch the script.
122158
this.scriptPathToLaunch = launchParams.Program;
123159
this.arguments = arguments;
124160

161+
// The order of debug protocol messages apparently isn't as guaranteed as we might like.
162+
// Need to be able to handle the case where we get the launch request after the
163+
// configurationDone request.
164+
if (this.isConfigurationDoneRequestComplete)
165+
{
166+
this.LaunchScript(requestContext);
167+
}
168+
169+
this.isLaunchRequestComplete = true;
170+
125171
await requestContext.SendResult(null);
126172
}
127173

@@ -133,31 +179,6 @@ protected Task HandleAttachRequest(
133179
throw new NotImplementedException();
134180
}
135181

136-
protected async Task HandleConfigurationDoneRequest(
137-
object args,
138-
RequestContext<object> requestContext)
139-
{
140-
// Execute the given PowerShell script and send the response.
141-
// Note that we aren't waiting for execution to complete here
142-
// because the debugger could stop while the script executes.
143-
Task executeTask =
144-
editorSession.PowerShellContext
145-
.ExecuteScriptAtPath(this.scriptPathToLaunch, this.arguments)
146-
.ContinueWith(
147-
async (t) => {
148-
Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");
149-
150-
await requestContext.SendEvent(
151-
TerminatedEvent.Type,
152-
null);
153-
154-
// Stop the server
155-
this.Stop();
156-
});
157-
158-
await requestContext.SendResult(null);
159-
}
160-
161182
protected Task HandleDisconnectRequest(
162183
object disconnectParams,
163184
RequestContext<object> requestContext)

0 commit comments

Comments
 (0)