Skip to content

Fix command history while debugging #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/PowerShellEditorServices/Console/ConsoleReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,11 @@ private async Task<string> ReadLine(bool isCommandLine, CancellationToken cancel
command.AddCommand("Get-History");

currentHistory =
await this.powerShellContext.ExecuteCommand<PSObject>(
command,
false,
false) as Collection<PSObject>;
new Collection<PSObject>(
(await this.powerShellContext.ExecuteCommand<PSObject>(
command,
false,
false)).ToList());

if (currentHistory != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
{
IEnumerable<TResult> executionResult = null;

using (var nestedPipeline = currentRunspace.CreateNestedPipeline())
string historyString = psCommand.Commands[0].CommandText;
using (var nestedPipeline = currentRunspace.CreateNestedPipeline(historyString, sendOutputToHost))
{
nestedPipeline.Commands.Clear();
foreach (var command in psCommand.Commands)
{
nestedPipeline.Commands.Add(command);
Expand Down
27 changes: 27 additions & 0 deletions src/PowerShellEditorServices/Session/PowerShell4Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ namespace Microsoft.PowerShell.EditorServices.Session
{
internal class PowerShell4Operations : IVersionSpecificOperations
{
private static SortedSet<string> s_noHistoryCommandNames = new SortedSet<string>(StringComparer.OrdinalIgnoreCase)
{
"prompt",
"Set-PSDebuggerAction",
"Get-PSDebuggerStopArgs",
"Set-PSDebugMode",
"TabExpansion2"
};

public void ConfigureDebugger(Runspace runspace)
{
#if !PowerShellv3
Expand Down Expand Up @@ -54,6 +63,24 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
};
}

// There's no way to tell the debugger not to add the command to history. It does however,
// check if the first command is in a static list of commands that shouldn't be added
// to history. We use that here to get around that limitation.
if (!sendOutputToHost && !s_noHistoryCommandNames.Contains(psCommand.Commands[0].CommandText))
{
var newCommand = new PSCommand()
.AddCommand("prompt")
.AddCommand("Microsoft.PowerShell.Core\\Out-Null")
.AddStatement();

foreach (Command command in psCommand.Commands)
{
newCommand.AddCommand(command);
}

psCommand = newCommand;
}

DebuggerCommandResults commandResults =
currentRunspace.Debugger.ProcessCommand(
psCommand,
Expand Down
4 changes: 3 additions & 1 deletion src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1490,10 +1490,12 @@ private SessionDetails GetSessionDetailsInRunspace(Runspace runspace)
{
powerShell.Runspace = runspace;
powerShell.Commands = command;
var invocationSettings = new PSInvocationSettings();
invocationSettings.AddToHistory = false;

return
powerShell
.Invoke()
.Invoke(null, invocationSettings)
.FirstOrDefault();
}
});
Expand Down