forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynchronousPowerShellTask.cs
393 lines (340 loc) · 16 KB
/
SynchronousPowerShellTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Context;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
using Microsoft.PowerShell.EditorServices.Utility;
using SMA = System.Management.Automation;
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution
{
internal class SynchronousPowerShellTask<TResult> : SynchronousTask<IReadOnlyList<TResult>>
{
private static readonly PowerShellExecutionOptions s_defaultPowerShellExecutionOptions = new();
private readonly ILogger _logger;
private readonly PsesInternalHost _psesHost;
private readonly PSCommand _psCommand;
private SMA.PowerShell _pwsh;
private PowerShellContextFrame _frame;
public SynchronousPowerShellTask(
ILogger logger,
PsesInternalHost psesHost,
PSCommand command,
PowerShellExecutionOptions executionOptions,
CancellationToken cancellationToken)
: base(logger, cancellationToken)
{
_logger = logger;
_psesHost = psesHost;
_psCommand = command;
PowerShellExecutionOptions = executionOptions ?? s_defaultPowerShellExecutionOptions;
}
public PowerShellExecutionOptions PowerShellExecutionOptions { get; }
public override ExecutionOptions ExecutionOptions => PowerShellExecutionOptions;
public override IReadOnlyList<TResult> Run(CancellationToken cancellationToken)
{
_psesHost.Runspace.ThrowCancelledIfUnusable();
PowerShellContextFrame frame = _psesHost.PushPowerShellForExecution();
try
{
_pwsh = _psesHost.CurrentPowerShell;
if (PowerShellExecutionOptions.WriteInputToHost)
{
_psesHost.WriteWithPrompt(_psCommand, cancellationToken);
}
// If we're in a breakpoint it means we're executing either interactive commands in
// a debug prompt, or our own special commands to query the PowerShell debugger for
// state that we sync with the LSP debugger. The former commands we want to send
// through PowerShell's `Debugger.ProcessCommand` so that they work as expected, but
// the latter we must not send through it else they pollute the history as this
// PowerShell API does not let us exclude them from it.
return _pwsh.Runspace.Debugger.InBreakpoint
&& (PowerShellExecutionOptions.AddToHistory || IsPromptCommand(_psCommand) || _pwsh.Runspace.RunspaceIsRemote)
? ExecuteInDebugger(cancellationToken)
: ExecuteNormally(cancellationToken);
}
finally
{
_psesHost.PopPowerShellForExecution(frame);
}
}
public override string ToString() => _psCommand.GetInvocationText();
private static bool IsPromptCommand(PSCommand command)
{
if (command.Commands.Count is not 1
|| command.Commands[0] is { IsScript: false } or { Parameters.Count: > 0 })
{
return false;
}
string commandText = command.Commands[0].CommandText;
return commandText.Equals("prompt", StringComparison.OrdinalIgnoreCase);
}
private IReadOnlyList<TResult> ExecuteNormally(CancellationToken cancellationToken)
{
_frame = _psesHost.CurrentFrame;
if (PowerShellExecutionOptions.WriteOutputToHost)
{
_psCommand.AddOutputCommand();
}
cancellationToken.Register(CancelNormalExecution);
Collection<TResult> result = null;
try
{
PSInvocationSettings invocationSettings = new()
{
AddToHistory = PowerShellExecutionOptions.AddToHistory,
};
if (PowerShellExecutionOptions.ThrowOnError)
{
invocationSettings.ErrorActionPreference = ActionPreference.Stop;
}
result = _pwsh.InvokeCommand<TResult>(_psCommand, invocationSettings);
cancellationToken.ThrowIfCancellationRequested();
}
// Allow terminate exceptions to propagate for flow control.
catch (TerminateException)
{
throw;
}
// Test if we've been cancelled. If we're remoting, PSRemotingDataStructureException
// effectively means the pipeline was stopped.
catch (Exception e) when (cancellationToken.IsCancellationRequested || e is PipelineStoppedException || e is PSRemotingDataStructureException)
{
// ExecuteNormally handles user commands in a debug session. Perhaps we should clean all this up somehow.
if (_pwsh.Runspace.Debugger.InBreakpoint)
{
StopDebuggerIfRemoteDebugSessionFailed();
}
throw new OperationCanceledException();
}
// We only catch RuntimeExceptions here in case writing errors to output was requested
// Other errors are bubbled up to the caller
catch (RuntimeException e)
{
if (e is PSRemotingTransportException)
{
_ = System.Threading.Tasks.Task.Run(
() => _psesHost.UnwindCallStack(),
CancellationToken.None)
.HandleErrorsAsync(_logger);
_psesHost.WaitForExternalDebuggerStops();
throw new OperationCanceledException("The operation was canceled.", e);
}
Logger.LogWarning($"Runtime exception occurred while executing command:{Environment.NewLine}{Environment.NewLine}{e}");
if (PowerShellExecutionOptions.ThrowOnError)
{
throw;
}
PSCommand command = new PSCommand()
.AddOutputCommand()
.AddParameter("InputObject", e.ErrorRecord.AsPSObject());
if (_pwsh.Runspace.RunspaceStateInfo.IsUsable())
{
_pwsh.InvokeCommand(command);
}
else
{
_psesHost.UI.WriteErrorLine(e.ToString());
}
}
finally
{
if (_pwsh.HadErrors)
{
_pwsh.Streams.Error.Clear();
}
}
return result;
}
private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationToken)
{
// TODO: How much of this method can we remove now that it only processes PowerShell's
// intrinsic debugger commands?
cancellationToken.Register(CancelDebugExecution);
PSDataCollection<PSObject> outputCollection = new();
// Out-Default doesn't work as needed in the debugger
// Instead we add Out-String to the command and collect results in a PSDataCollection
// and use the event handler to print output to the UI as its added to that collection
if (PowerShellExecutionOptions.WriteOutputToHost)
{
_psCommand.AddDebugOutputCommand();
// Use an inline delegate here, since otherwise we need a cast -- allocation < cast
outputCollection.DataAdded += (object sender, DataAddedEventArgs args) =>
{
for (int i = args.Index; i < outputCollection.Count; i++)
{
_psesHost.UI.WriteLine(outputCollection[i].ToString());
}
};
}
DebuggerCommandResults debuggerResult = null;
try
{
// In the PowerShell debugger, intrinsic debugger commands are made available, like
// "l", "s", "c", etc. Executing those commands produces a result that needs to be
// set on the debugger stop event args. So we use the Debugger.ProcessCommand() API
// to properly execute commands in the debugger and then call
// DebugContext.ProcessDebuggerResult() later to handle the command appropriately
//
// Unfortunately, this API does not allow us to pass in the InvocationSettings,
// which means (for instance) that we cannot instruct it to avoid adding our
// debugger implementation's commands to the history. So instead we now only call
// `ExecuteInDebugger` for PowerShell's own intrinsic debugger commands.
debuggerResult = _pwsh.Runspace.Debugger.ProcessCommand(_psCommand, outputCollection);
cancellationToken.ThrowIfCancellationRequested();
}
// Allow terminate exceptions to propagate for flow control.
catch (TerminateException)
{
throw;
}
// Test if we've been cancelled. If we're remoting, PSRemotingDataStructureException
// effectively means the pipeline was stopped.
catch (Exception e) when (cancellationToken.IsCancellationRequested || e is PipelineStoppedException || e is PSRemotingDataStructureException)
{
StopDebuggerIfRemoteDebugSessionFailed();
throw new OperationCanceledException();
}
// We only catch RuntimeExceptions here in case writing errors to output was requested
// Other errors are bubbled up to the caller
catch (RuntimeException e)
{
if (e is PSRemotingTransportException)
{
_ = System.Threading.Tasks.Task.Run(
() => _psesHost.UnwindCallStack(),
CancellationToken.None)
.HandleErrorsAsync(_logger);
_psesHost.WaitForExternalDebuggerStops();
throw new OperationCanceledException("The operation was canceled.", e);
}
Logger.LogWarning($"Runtime exception occurred while executing command:{Environment.NewLine}{Environment.NewLine}{e}");
if (PowerShellExecutionOptions.ThrowOnError)
{
throw;
}
using PSDataCollection<PSObject> errorOutputCollection = new();
errorOutputCollection.DataAdding += (object sender, DataAddingEventArgs args)
=> _psesHost.UI.WriteLine(args.ItemAdded?.ToString());
PSCommand command = new PSCommand()
.AddDebugOutputCommand()
.AddParameter("InputObject", e.ErrorRecord.AsPSObject());
_pwsh.Runspace.Debugger.ProcessCommand(command, errorOutputCollection);
}
finally
{
if (_pwsh.HadErrors)
{
_pwsh.Streams.Error.Clear();
}
}
_psesHost.DebugContext.ProcessDebuggerResult(debuggerResult);
// Optimization to save wasted computation if we're going to throw the output away anyway
if (PowerShellExecutionOptions.WriteOutputToHost)
{
return Array.Empty<TResult>();
}
// If we've been asked for a PSObject, no need to allocate a new collection
if (typeof(TResult) == typeof(PSObject)
&& outputCollection is IReadOnlyList<TResult> resultCollection)
{
return resultCollection;
}
// Otherwise, convert things over
List<TResult> results = new(outputCollection.Count);
foreach (PSObject outputResult in outputCollection)
{
if (LanguagePrimitives.TryConvertTo(outputResult, typeof(TResult), out object result))
{
results.Add((TResult)result);
}
}
return results;
}
private void StopDebuggerIfRemoteDebugSessionFailed()
{
// When remoting to Windows PowerShell,
// command cancellation may cancel the remote debug session in a way that the local debug session doesn't detect.
// Instead we have to query the remote directly
if (_pwsh.Runspace.RunspaceIsRemote)
{
_pwsh.Runspace.ThrowCancelledIfUnusable();
PSCommand assessDebuggerCommand = new PSCommand().AddScript("$Host.Runspace.Debugger.InBreakpoint");
PSDataCollection<PSObject> outputCollection = new();
_pwsh.Runspace.Debugger.ProcessCommand(assessDebuggerCommand, outputCollection);
foreach (PSObject output in outputCollection)
{
if (Equals(output?.BaseObject, false))
{
_psesHost.DebugContext.ProcessDebuggerResult(new DebuggerCommandResults(DebuggerResumeAction.Stop, evaluatedByDebugger: true));
_logger.LogWarning("Cancelling debug session due to remote command cancellation causing the end of remote debugging session");
_psesHost.UI.WriteWarningLine("Debug session aborted by command cancellation. This is a known issue in the Windows PowerShell 5.1 remoting system.");
}
}
}
}
private void CancelNormalExecution()
{
if (!_pwsh.Runspace.RunspaceStateInfo.IsUsable())
{
return;
}
// If we're signaled to exit a runspace then that'll trigger a stop,
// if we block on that stop we'll never exit the runspace (
// and essentially deadlock).
if (_frame?.SessionExiting is true)
{
_pwsh.BeginStop(null, null);
return;
}
try
{
_pwsh.Stop();
}
catch (NullReferenceException nre)
{
_logger.LogError(
nre,
"Null reference exception from PowerShell.Stop received.");
}
}
internal void MaybeAddToHistory()
{
// Do not add PSES internal commands to history. Also exclude input that came from the
// REPL (e.g. PSReadLine) as it handles history itself in that scenario.
if (PowerShellExecutionOptions is { AddToHistory: false } or { FromRepl: true })
{
return;
}
// Only add pure script commands with no arguments to interactive history.
if (_psCommand.Commands is { Count: not 1 }
|| _psCommand.Commands[0] is { Parameters.Count: not 0 } or { IsScript: false })
{
return;
}
try
{
_psesHost.AddToHistory(_psCommand.Commands[0].CommandText);
}
catch
{
// Ignore exceptions as the user can register a script-block predicate that
// determines if the command should be added to history.
}
}
private void CancelDebugExecution()
{
if (!_pwsh.Runspace.RunspaceStateInfo.IsUsable())
{
return;
}
_pwsh.Runspace.Debugger.StopProcessCommand();
}
}
}