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

Some shells don't remember history #151105

Open
Sampath-Lokuge opened this issue Jun 2, 2022 · 24 comments
Open

Some shells don't remember history #151105

Sampath-Lokuge opened this issue Jun 2, 2022 · 24 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. windows VS Code on Windows issues
Milestone

Comments

@Sampath-Lokuge
Copy link

Sampath-Lokuge commented Jun 2, 2022

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version:
  • OS Version:

Steps to Reproduce:

  1. Open VS code
  2. Please see the below images.

vs1

When I pressed the top arrow key It shows like so. But no idea from where that CLI is coming. I would like to have npx cap open android CLI here.

vs2

@Tyriar
Copy link
Member

Tyriar commented Jun 2, 2022

This type of issue has been reported a few times and I think we've closed them all out, let's track in this one. This works fine in pwsh, I believe this is because pwsh persists history to disk as you type, whereas git bash does it when the process shuts down. Something about how we're shutting down the terminal processes doesn't let git bash persist to disk.

Maybe the problem code is here where we kill all processes before closing the conpty pty: https://github.com/microsoft/node-pty/blob/1674722e1caf3ff4dd52438b70ed68d46af83a6d/src/windowsPtyAgent.ts#L161-L168

We tweaked what happens here a bunch to try stop processes leaking which is why we go through and kill all processes in GetConsoleProcessList. Maybe we could try calling native.kill() and then giving it some time to finish before going through and killing the process list?

@Tyriar Tyriar added bug Issue identified by VS Code Team member as probable bug upstream Issue identified as 'upstream' component related (exists outside of VS Code) windows VS Code on Windows issues terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. labels Jun 2, 2022
@Tyriar Tyriar added this to the Backlog milestone Jun 2, 2022
@Tyriar Tyriar changed the title No history CLI on Windows 10 Some shells don't remember history on Windows Jun 2, 2022
@Sampath-Lokuge
Copy link
Author

Ok sure. It would be great if we'll have a solution here. But this is working perfectly fine on my Macbook Air. So we would like to have this on Windows PC too. Thanks in advance.

@Okeanos
Copy link
Contributor

Okeanos commented Oct 18, 2022

As far as I understand the problem at its root for Git bash integration it indeed appears to be related to:

Something about how we're shutting down the terminal processes doesn't let git bash persist to disk.

However, the VSCode integration of Git Bash has further, compounding issues:

The Git Bash Windows Terminal Profile Fragment vs. VS Code's definition reveals that VSCode doesn't start the Git Bash as interactive shell. Only interactive Bash sessions will even attempt to save the history by default; a login shell is not sufficient for that!

However, as I outlined in the discussion post … that is sadly not enough and the export PROMPT_COMMAND="history -a" workaround is the closest thing to a fix for now I know of.

meganrogge pushed a commit that referenced this issue Nov 9, 2022
Bash sessions should be started as interactive (`-i`) sessions to enable history
commands by default.
This is the same setup used by the official Git for Windows JSON fragment
extension for Windows Terminal.

This does not fix the issue at this time, however, because bash signal handling
on Windows for quit/close/kill signals does not work as expected.
@Tyriar
Copy link
Member

Tyriar commented Dec 2, 2022

The situation might be better in newer versions of Windows when microsoft/terminal#14282 ships. We'll probably need to tweak node-pty's process kill logic though.

@starball5
Copy link

starball5 commented Apr 6, 2023

This type of issue has been reported a few times and I think we've closed them all out

A couple I found by googling "site:github.com/microsoft/vscode/issues shell closing window history": #37273, #85831, #114164. All about Git Bash. Also related: vscode-docs f0876ad, which changed docs to say this is by-design, and to workaround using export PROMPT_COMMAND='history -a'.

Today I encountered a Stack Overflow question about a user experiencing the same issue with a Zsh shell.

I found I that I experience the same issue with a Bash shell on Ubuntu with VS Code 1.77.1 installed as a Snap. My Bash shell in the VS Code interactive terminal is an interactive, non-login shell. Opening a login sub-shell with bash -l makes no difference: History is not saved to the histfile when closing the VS Code window. I have not set export PROMPT_COMMAND='history -a'.

Let me know if I should create a new issue ticket that is not specific to Windows (since this one currently is).

Loosely related in the Window Terminal repo: microsoft/terminal#7826.

@meganrogge meganrogge changed the title Some shells don't remember history on Windows Some shells don't remember history Apr 19, 2023
@meganrogge meganrogge removed upstream Issue identified as 'upstream' component related (exists outside of VS Code) windows VS Code on Windows issues labels Apr 19, 2023
@akpircher
Copy link

akpircher commented Apr 19, 2023

I have this problem on VSCode in MacOS/Darwin.

The root cause is easily replicated by setting terminal.integrated.persistentSessionReviveProcess setting to "never", do some commands in the terminal, quit VSCode without exiting the terminal, and then re-open ANY new terminal. The history is not there.

When exiting VSCode, if the persist setting is set to "never" or is "onExit" and the window is closed, then the terminal should follow the normal exit procedures and logout properly.

@akpircher
Copy link

This is an older issue, but I'm hoping that this gets fixed reasonably soon. VSCode is practically unusable for me as it is.

@jasperkelder
Copy link

Same problem here for zsh on MacOS, not always though. Haven't figured out when or why it happens, but sometimes the integrated terminal in VSCode only shows history of the regular terminal.

@Okeanos
Copy link
Contributor

Okeanos commented Apr 22, 2023

Shells generally have different behaviour concerning histories and VSCode simply exacerbates it by not closing them correctly, especially on Windows in particular (afaik).

Bash will generally only save the history for the last interactive (-i) login (--login) shell that has been properly terminated. Otherwise the export PROMPT_COMMAND='history -a' workaround is required, regardless of VSCode or not. See interactive behavour in the manual.

In case of ZSH there are options such as setting SHARE_HISTORY that may improve the experience beyond what Bash offers natively for example.

To verify what goes wrong on macOS and help debugging efforts you should verify the Terminal setup in VSCode and add that information here.

@jasperkelder
Copy link

jasperkelder commented Apr 30, 2023

The SHARE_HISTORY option seems to work for me as a fix. Closing the terminal by typing "exit" also seems to work. This wasn't necessary earlier though.

As far as I know I'm using defaults for the terminal. I just checked it with a VSCode Insiders download without any plugin or setting changes and I had the same problem there.

Tldr, for now use "exit" every time to save history, or:
open ~/.zshrc
And add:
setopt SHARE_HISTORY

@sfboss
Copy link

sfboss commented May 13, 2023

I had this issue and seemed to only impact vscode (terminal in macos commands were saved and subsequently available in vscode later)... the comment mentioned above by Okeanos to add "export PROMPT_COMMAND='history -a'" (I added this in to ~/.bash_profile) finally seemed to be the thing that made VScode terminal bash sessions remember history regardless of how VSCode was exited and/or which folder I opened, basically making it seem normal again. Of note, I did have a messy bash_profile and this command was already in there, but cleaning up the garbage and leaving the command revived it. Mentioning that in case anyone says they have the command already in there, maybe a troubleshooting step to backup file and leave only that command and work backwards from there.

I did several other troubleshooting steps like clearing the history in ~/.bash_history so I can't say absolutely that this specifically fixed it but am about 98% sure. Hope the details help whomever try and resolve.

@starball5
Copy link

@sfboss export PROMPT_COMMAND='history -a' is a documented workaround. see also my prior comment, what states that. As far as I understand, this issue ticket is about fixing the problem instead of using workarounds.

@sfboss
Copy link

sfboss commented May 14, 2023 via email

@starball5
Copy link

Wouldn’t they appreciate the information to try and resolve the issue?

They wrote the docs that state that that is a known workaround. I.e. I'm saying they already know what you're telling them.

@migalv
Copy link

migalv commented Jun 2, 2023

I'm getting the same issue on Mac M1 Ventura 13.2 with VSCode v1.78.2 and zsh shell. This started happening when I updated VSCode to the latest version.

@meganrogge
Copy link
Contributor

meganrogge commented Jun 2, 2023

Recent fix in this area #181508, can you repro in insider's?

@migalv
Copy link

migalv commented Jun 3, 2023

Hello thanks for the update and the PR @meganrogge. Could you help me figure out how I can test it?

@meganrogge
Copy link
Contributor

You can install vscode insiders to test it

@ginolegigot
Copy link

fwiw, in linux, i figured vscode internal terminal created a .zsh_history when the commands from my native terminal were stored in .zhistory.
Setting an "export HISTFILE=.zhistory" in .zshrc fixed the issue.

@KaivG
Copy link

KaivG commented Jan 26, 2024

What is the workaround for windows with git bash? Typing exit manually seems to work, but it is obviously tedious. Where do I have to set export PROMPT_COMMAND='history -a' on windows for git bash?

@timmywil
Copy link

@KaivG

What is the workaround for windows with git bash? Typing exit manually seems to work, but it is obviously tedious. Where do I have to set export PROMPT_COMMAND='history -a' on windows for git bash?

You should be able to set it in ~/.bash_profile or ~/.bashrc. Find your git bash home with cd ~ && pwd. If neither exists already, pick one and make it. Then restart the shell.

@KaivG
Copy link

KaivG commented Jan 27, 2024

@KaivG

What is the workaround for windows with git bash? Typing exit manually seems to work, but it is obviously tedious. Where do I have to set export PROMPT_COMMAND='history -a' on windows for git bash?

You should be able to set it in ~/.bash_profile or ~/.bashrc. Find your git bash home with cd ~ && pwd. If neither exists already, pick one and make it. Then restart the shell.

Thanks that worked! Neither file existed, so I created ~/.bash_profile and saved PROMPT_COMMAND='history -a' in it and now the history is kept. Thanks a lot!

@Amnesiac9
Copy link

Amnesiac9 commented Apr 28, 2024

This is non-intuitive behavior to have within vscode even if it is emulating something on linux. Would be nice to have it as default for consistency.

@developer55
Copy link

First of all, thanks to @KaivG for guiding me to the following solution.
This works for me, on Windows 10, Git installed and git bash set to VSCODE as default when starting a new terminal. It may not work for you.
SOLUTION:

  1. Create a file "~/.bashrc" in C:\Users\myuser.bashrc or the path where you have the .bash_history file of your Git installation
  2. In .bashrc write in the following line:
    export PROMPT_COMMAND='history -a'
  3. Save the file.
  4. Open VSCODE, and inside it open a git bash terminal. A warning message will appear and Git will generate a ~/.bash_profile in C:\Users\myuser with the following content:
    #generated by Git for Windows
    test -f ~/.profile && . ~/.profile
    test -f ~/.bashrc && . ~/.bashrc
    There will also be a .bash_history file that should already be there. It is in that .bash_history where commands written in git bash will be stored when using the GIT bash terminal built into VSCODE.

@Tyriar Tyriar added the windows VS Code on Windows issues label Sep 23, 2024
@meganrogge meganrogge removed their assignment Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. windows VS Code on Windows issues
Projects
None yet
Development

No branches or pull requests