Skip to content

Commit 9c0b58e

Browse files
Fix cluttered debug history
This change fixes an issue where background commands would be added to PowerShell's history. This was due to a limitation in the PowerShell debugger where the only way commands are excluded is if the command is part of a hard coded set of commands. As a workaround, a command from that set (prompt) is added as the first statement. Resolves PowerShell/vscode-powershell#873
1 parent b536aff commit 9c0b58e

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/PowerShellEditorServices/Session/PowerShell3Operations.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
3333
{
3434
IEnumerable<TResult> executionResult = null;
3535

36-
using (var nestedPipeline = currentRunspace.CreateNestedPipeline())
36+
string historyString = psCommand.Commands[0].CommandText;
37+
using (var nestedPipeline = currentRunspace.CreateNestedPipeline(historyString, sendOutputToHost))
3738
{
39+
nestedPipeline.Commands.Clear();
3840
foreach (var command in psCommand.Commands)
3941
{
4042
nestedPipeline.Commands.Add(command);

src/PowerShellEditorServices/Session/PowerShell4Operations.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ namespace Microsoft.PowerShell.EditorServices.Session
1313
{
1414
internal class PowerShell4Operations : IVersionSpecificOperations
1515
{
16+
private static SortedSet<string> s_noHistoryCommandNames = new SortedSet<string>(StringComparer.OrdinalIgnoreCase)
17+
{
18+
"prompt",
19+
"Set-PSDebuggerAction",
20+
"Get-PSDebuggerStopArgs",
21+
"Set-PSDebugMode",
22+
"TabExpansion2"
23+
};
24+
1625
public void ConfigureDebugger(Runspace runspace)
1726
{
1827
#if !PowerShellv3
@@ -54,6 +63,24 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
5463
};
5564
}
5665

66+
// There's no way to tell the debugger not to add the command to history. It does however,
67+
// check if the first command is in a static list of commands that shouldn't be added
68+
// to history. We use that here to get around that limitation.
69+
if (!sendOutputToHost && !s_noHistoryCommandNames.Contains(psCommand.Commands[0].CommandText))
70+
{
71+
var newCommand = new PSCommand()
72+
.AddCommand("prompt")
73+
.AddCommand("Microsoft.PowerShell.Core\\Out-Null")
74+
.AddStatement();
75+
76+
foreach (Command command in psCommand.Commands)
77+
{
78+
newCommand.AddCommand(command);
79+
}
80+
81+
psCommand = newCommand;
82+
}
83+
5784
DebuggerCommandResults commandResults =
5885
currentRunspace.Debugger.ProcessCommand(
5986
psCommand,

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,10 +1490,12 @@ private SessionDetails GetSessionDetailsInRunspace(Runspace runspace)
14901490
{
14911491
powerShell.Runspace = runspace;
14921492
powerShell.Commands = command;
1493+
var invocationSettings = new PSInvocationSettings();
1494+
invocationSettings.AddToHistory = false;
14931495

14941496
return
14951497
powerShell
1496-
.Invoke()
1498+
.Invoke(null, invocationSettings)
14971499
.FirstOrDefault();
14981500
}
14991501
});

0 commit comments

Comments
 (0)