Skip to content
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

History stack not empty #20025

Closed
5 tasks done
Ricky-Tigg opened this issue Jul 25, 2023 · 16 comments
Closed
5 tasks done

History stack not empty #20025

Ricky-Tigg opened this issue Jul 25, 2023 · 16 comments

Comments

@Ricky-Tigg
Copy link

Prerequisites

Steps to reproduce

> Set-PSReadlineOption -HistorySaveStyle SaveNothing
> Remove-Item (Get-PSReadlineOption).HistorySavePath
  1. Execute at least one command;
  2. Close tool's current session then start a new one;
  3. As same user as in previous session, press down-arrowand up-arrow in order to call the elements of the history stack.

Expected behavior

History stack to be empty as a consequence of step _1_.

Actual behavior

History stack not empty.

Error details

No response

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

@Ricky-Tigg Ricky-Tigg added the Needs-Triage The issue is new and needs to be triaged by a work group. label Jul 25, 2023
@237dmitry
Copy link

History stack not empty.

In the (Get-PSReadlineOption).HistorySavePath or in an history of the current session? These are different entities of history. PSReadline history is rather history log or backup and a session history is the object with id, status and datetime and timespan values.

@Ricky-Tigg
Copy link
Author

Isn't step 1 already enough to empty all entities of history thus empty all history stacks of all times?

@237dmitry
Copy link

Isn't step 1 already enough to empty all entities of history thus empty all history stacks of all times?

No. It relates only on PSReadline history. I think you could add Clear-History to your prompt function to delete every previous command. In this case in history would be the only just entered command. I was not trying myself.

@Ricky-Tigg
Copy link
Author

As sole goals:

  • No history stack available at start of new session.
  • Commands entered in current session temporarily saved in history stack.
  • Temporarily saved history stack emptied at exit of session according to step 1.

That makes Clear-History unsuitable.

@237dmitry
Copy link

History stack not empty

Commands entered in current session temporarily saved in history stack.

That makes Clear-History unsuitable.

??

@Ricky-Tigg
Copy link
Author

What did you think you would achieve by manipulating irrationally someone's writings. That's a pattern you won't observe with a genuine developer. Indeed your ??; that what you get when you play that game. How could you assume that a reader other than you would not notice your trick? You just made yourself a reputation.

@237dmitry
Copy link

by manipulating irrationally someone's quotes

How I could understand you?

I asked you:

In the (Get-PSReadlineOption).HistorySavePath or in an history of the current session?

Your answer:

Isn't step 1 already enough to empty all entities of history thus empty all history stacks of all times?

Did you do mean all history stacks of all times, current history and saved one?

I understood that you do not need a history at all, but how else to understand?

@mklement0
Copy link
Contributor

mklement0 commented Jul 26, 2023

Update: This comment originally lacked proper framing: it tries to provide general information about how PSReadLine manages its history. I cannot reproduce the symptom described in the initial post.

Set-PSReadlineOption -HistorySaveStyle SaveNothing:

  • applies to the current session only.

    • The PSReadLine module currently does not have its own persistence mechanism; any non-default settings you want to persist, i.e. settings you want to apply to all future sessions, must be added as Set-PSReadLineOption calls to a profile file, such as $PROFILE.
  • only governs whether interactively submitted commands are persisted (saved to the history file).

    • Recalling commands submitted in the current session, in-memory always works by default, irrespective of the (Get-PSReadlineOption).HistorySavePath setting.

      • However, you can also set Set-PSReadLineOption -MaximumHistoryCount 1 (0 is not allowed) to limit in-session recall to just one command in-session - this, however, seemingly does not limit how many commands are stored in the persisted history (if maintained, as controlled via -HistorySaveStyle)

      • To categorically prevent commands from even getting added to the history, you can write a handler (script block) that suppresses any addition to the history via the -AddToHistoryHandler setting.

        • The following handler prevents any command from getting added to the history, both in-memory and persisted:
        Set-PSReadLineOption -AddToHistoryHandler { $false }
    • On Windows , even unloading the PSReadLine module (Remove-Module PSReadLine) does not prevent the availability of a session-level history (recall of commands with Up-arrow, for commands submitted after the Remove-Module call), because Windows console windows maintain their own current-session-only history.

@Ricky-Tigg
Copy link
Author

In the (Get-PSReadlineOption).HistorySavePath or in an history of the current session?

I can observe myself that

  • it wasn't answered to in my next comment. as i understood it partially; that (Get-PSReadlineOption).HistorySavePath i could not yet interpret correctly while that history of the current session i could.
  • being non-answered must had bothered me because in a next comment of Jul 25, 2023, 8:30 PM GMT+3, i attempted to answer globally with explicit conditions.

No history stack available at start of new session.

Didn't this imply all history stacks of all times, which itself implied all those saved.

Commands entered in current session temporarily saved in history stack.
Temporarily saved history stack emptied at exit of session according to step 1.

Didn't this imply history of the current session is needed, hence must be saved in order to be available in the current session and at last must be emptied at the end of session so that the condition No history stack available at start of new session. can be met.

What we learned from the comment history of the present ticket is that all this activity happened before your comment of Jul 25, 2023, 9:30 PM GMT+3.

@Ricky-Tigg
Copy link
Author

I needed a proper answer, so I had to investigate to find a source that provided it. Finally I was able to find it, although provided by a non-Microsoft source. I make a note here for myself for record purpose.

  1. Before ending each session | Remove-Item -Confirm -LiteralPath C:\Users\$Env:UserName\appdata\roaming\microsoft\windows\powershell\PSReadLine\ConsoleHost_history.txt
  2. End the session by exiting the console from the UI, not by Exit in command line since it would be saved thus present in history when starting a new session.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Jul 27, 2023
@mklement0
Copy link
Contributor

mklement0 commented Jul 27, 2023

For what it's worth, I cannot reproduce the symptoms described in your initial post.

My previous comment (since-amended) was basically an attempt to generally describe how PSReadLine manages its history

As for your latest solution:

  • Better to use (Get-PSReadlineOption).HistorySavePath, as in your initial post.

  • I see -Confirm in there, so you want to be prompted as to whether you want to delete the persisted history? And you're planning to invoke this command manually before exiting every session?

Either way, let me add some more general information for those interested in (possibly selectively) preventing PSReadLine from recording or saving history:

  • To prevent PSReadLine from saving the command history to a file, place the following in your $PROFILE file.
# To be safe, remove any existing history file, not just one that predates this configuration, but also
# ones that may have been created when PowerShell was started with -NoProfile
if (Test-Path (Get-PSReadlineOption).HistorySavePath) { Remove-Item -Force (Get-PSReadlineOption).HistorySavePath }

# Tell PSReadLine not to *save* (persist) the command history, but make the history of 
# the *current session's* commands available in-memory, in-session.
Set-PSReadlineOption -HistorySaveStyle SaveNothing
  • To prevent PSReadLine from adding commands to the history altogether (whether in-memory-only or persisted; may also be added to $PROFILE):

    • Set-PSReadLineOption's -AddToHistoryHandler parameter allows to specify a script block that is invoked before PSReadLine adds a newly submitted command to its history, and gives you three basic choices: whether not to add the command at hand, whether to add it to the in-memory (non-persistent) history only, or - the default - to add the command to both the in-memory and the persisted (saved) history.

    • Thus, to prevent any commands from being added to the history (both in-memory and persisted), use:

      Set-PSReadLineOption -AddToHistoryHandler { $false }
    • To selectively prevent commands from being added to the history, use something like the following; this example prevents lines that start with exit from being added:

      Set-PSReadLineOption -AddToHistoryHandler { param($command) $command -notmatch '^\s*exit\b' }

@Ricky-Tigg
Copy link
Author

It's statistically good. At least one developer who was unable to reproduce the symptom described in this report. However unknown is still your demonstration. I can't wait to read, probably like anyone here, how you managed to not get a non-empty history stack at the start of new sessions using those very commands presented in this report.

As for the very case I was reasonably expecting one of yours to recommend the use of a permanent alias for the command Remove-Item -LiteralPath C:\Users\$Env:UserName\appdata\roaming\microsoft\windows\powershell \PSReadLine\ConsoleHost_history.txt, be it without -Confirm or not. But no.

I naively assumed it would be as simple as Unix/Linux made it. I had to learn that it would have been too good to be true. So we won't know neither how you would have done, by choosing to keep things simple. Yet too often i can testify how some developers easily became addictive to cultivate the Art of doing things by complicating them even when they could be done with simplicity..

@mklement0
Copy link
Contributor

mklement0 commented Jul 27, 2023

@Ricky-Tigg:

At least one developer who was unable to reproduce the symptom described in this report.

  • Indeed: I followed your repro steps, and couldn't reproduce your symptom in v7.3.6, with PSReadLine 2.2.6 . That's the best as I can do as one voice.

  • If you indeed see the symptom, you shouldn't have closed this report, given that the symptom would imply a bug.

    • You'll then have to get others to agree with you, based on their ability to reproduce the symptom.
  • As a courtesy, I've provided background information on how PSReadLine manages its history, and how you might better solve your problem. I don't know what a "permanent alias" is, but it is placing commands such as the ones shown in my previous comment in your $PROFILE file that effectively makes the configuration persistent (for sessions in which profile loading isn't suppressed, via -NoProfile).

@Ricky-Tigg
Copy link
Author

No need to bother to pretend. Who is still unaware today of how everything is destined to end at Microsoft? The correct answer, and nothing else. This is what I and any reader were entitled to expect. Even I, novice in regard to code culture practises at Microsoft, I am able to manage answering properly, then by fully meeting the requirements here set, which i do now.

I have to assume the reader is fully aware of those nameless things that are called in Microsoft's jargon PowerShell, Windows Defender; let's stop here, i have not time to lose by citing its whole production. Fancy names, we get it.

Methodology

Check whether the Controlled folder access property is enabled:

> Get-MpPreference | Select-Object -ExpandProperty EnableControlledFolderAccess
2

When it is, as administrator, add the PowerShell 7+ application as allowed in regard to Controlled folder access.

> Add-MpPreference -ControlledFolderAccessAllowedApplications "C:\Program Files\PowerShell\7\pwsh.exe"

Paths to each of the four profiles for the Windows PowerShell console:

> ($PROFILE | Select-Object -ExcludeProperty Length | Format-List | Out-String).Trim()
AllUsersAllHosts       : C:\Program Files\PowerShell\7\PROFILE.ps1
AllUsersCurrentHost    : C:\Program Files\PowerShell\7\Microsoft.PowerShell_PROFILE.ps1
CurrentUserAllHosts    : C:\Users\vince\Documents\PowerShell\PROFILE.ps1
CurrentUserCurrentHost : C:\Users\vince\Documents\PowerShell\Microsoft.PowerShell_PROFILE.ps1

Determine the existence of a profile; for instance $PROFILE:

> Test-Path $PROFILE
False

Then no profile exits yet for $PROFILE's file.

Create a new profile (is persistent), for instance for $PROFILE which can be referred as well as $PROFILE.CurrentUserCurrentHost:

> New-Item -force -type file -path $PROFILE
    Directory: C:\Users\[name]\Documents\PowerShell
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            8.15.2023    11.07              0 Microsoft.PowerShell_PROFILE.ps1

Create a new persistent alias, for instance for this profile:. To achieve this task, select preferably a text editor with support for code language syntax coloring to edit this profile; notepad++ will do.

Model of alias:

function [function_name] {
 # to be run
}

Set-Alias [alias_name] [function_name]

Alias that I have applied on my system; shown for convenience:

function CurrentUserPowerShellHistoryCompleteVacuum {
 Remove-Item -Confirm -LiteralPath C:\Users\$Env:UserName\appdata\roaming\microsoft\windows\powershell\PSReadLine\ConsoleHost_history.txt
 Write-Output ""
 Write-Output 'Now, if you chose to empty the history, end this session by closing this terminal within UI.'
}

Set-Alias vacuum CurrentUserPowerShellHistoryCompleteVacuum

Once the profile saved, end your session by closing your terminal within UI. The modification will take effect at start of a new terminal session. At last, ensure the alias is operational by executing it; for instance vacuum in my system.

> vacuum

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target
"C:\Users\[name]\appdata\roaming\microsoft\windows\powershell\PSReadLine\ConsoleHost_history.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Now, if you chose to empty the history, end this session by closing this terminal within UI.

@mklement0
Copy link
Contributor

mklement0 commented Aug 15, 2023

@Ricky-Tigg, to spare you frustration in the future, let me try to delineate what this repo is and isn't about:

  • It is about:

    • Driving the development of PowerShell, based on bug reports and feature requests.
  • It is not:

    • A support forum.
    • A forum for off-topic rants and posting solutions to very specific, personal use cases.

@Ricky-Tigg
Copy link
Author

Is it so? But if only it could only be that. But it is still [and you can go on for each of your It is about: and It is not:].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants