Skip to content

Commit 0173ade

Browse files
committed
Fix for PowerShell#66, Get-Variable -Scope doesn't always work.
Ideally we should be using CallStackFrame.GetFrameVariables() but it currently seems to be broken. We've been informed that using Get-Variable -Scope <num> isn't great because scope numbers and frames don't always line up. For instance, dot source introducing a new stack frame but not a new scope. But until GetFrameVariables is fixed in a future drop, we have no option but to use Get-Variable -Scope <num>. This will result in stack frames that have no Auto or Local variables. But the good news is that the debug host will no longer crash. :-) BTW this commit introduces a new optional parameter on PowerShellContext.ExecuteCommand(). While there is a parameter to suppress "output" there was no parameter to suppress "errors" from being written to the debug console. I see no benefit in littering the user's debug console with a bunch of "get-variable : The scope number '#' exceeds the number of active scopes". So there is now a "sendErrorToHost" parameter that defaults to true. However, even when that parameter is set to false, the error info shows up in the DebugAdapter.log file.
1 parent 3c5cceb commit 0173ade

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/PowerShellEditorServices/Debugging/DebugService.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,19 @@ private async Task<VariableContainerDetails> FetchVariableContainer(
360360
new VariableContainerDetails(this.nextVariableId++, "Scope: " + scope);
361361
this.variables.Add(scopeVariableContainer);
362362

363-
var results = await this.powerShellContext.ExecuteCommand<PSVariable>(psCommand);
364-
foreach (PSVariable psvariable in results)
363+
var results = await this.powerShellContext.ExecuteCommand<PSVariable>(psCommand, sendErrorToHost: false);
364+
if (results != null)
365365
{
366-
var variableDetails = new VariableDetails(psvariable) { Id = this.nextVariableId++ };
367-
this.variables.Add(variableDetails);
368-
scopeVariableContainer.Children.Add(variableDetails.Name, variableDetails);
369-
370-
if ((autoVariables != null) && AddToAutoVariables(psvariable, scope))
366+
foreach (PSVariable psvariable in results)
371367
{
372-
autoVariables.Children.Add(variableDetails.Name, variableDetails);
368+
var variableDetails = new VariableDetails(psvariable) {Id = this.nextVariableId++};
369+
this.variables.Add(variableDetails);
370+
scopeVariableContainer.Children.Add(variableDetails.Name, variableDetails);
371+
372+
if ((autoVariables != null) && AddToAutoVariables(psvariable, scope))
373+
{
374+
autoVariables.Children.Add(variableDetails.Name, variableDetails);
375+
}
373376
}
374377
}
375378

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,17 @@ public Task<RunspaceHandle> GetRunspaceHandle()
208208
/// <param name="sendOutputToHost">
209209
/// If true, causes any output written during command execution to be written to the host.
210210
/// </param>
211+
/// <param name="sendErrorToHost">
212+
/// If true, causes any errors encountered during command execution to be written to the host.
213+
/// </param>
211214
/// <returns>
212215
/// An awaitable Task which will provide results once the command
213216
/// execution completes.
214217
/// </returns>
215218
public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
216219
PSCommand psCommand,
217-
bool sendOutputToHost = false)
220+
bool sendOutputToHost = false,
221+
bool sendErrorToHost = true)
218222
{
219223
RunspaceHandle runspaceHandle = null;
220224
IEnumerable<TResult> executionResult = null;
@@ -324,8 +328,11 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
324328
LogLevel.Error,
325329
"Runtime exception occurred while executing command:\r\n\r\n" + e.ToString());
326330

327-
// Write the error to the host
328-
this.WriteExceptionToHost(e);
331+
if (sendErrorToHost)
332+
{
333+
// Write the error to the host
334+
this.WriteExceptionToHost(e);
335+
}
329336
}
330337
finally
331338
{

0 commit comments

Comments
 (0)