Skip to content

Preserve custom HISTFILE in zsh shell integration#314615

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/zsh-histfile-preserve-169264
Open

Preserve custom HISTFILE in zsh shell integration#314615
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/zsh-histfile-preserve-169264

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

Fixes #169264

Summary

After #168396, the zsh shell integration rc script unconditionally resets HISTFILE to $USER_ZDOTDIR/.zsh_history before sourcing the user's .zshrc. This clobbers any HISTFILE value the user has set via ~/.zshenv or via the terminal.integrated.env.* setting, even though .zshrc has not yet had a chance to override it back. As a result env | grep HIST always shows the default location.

This applies the plan @Tyriar outlined in the issue:

  • In getShellIntegrationInjection (terminalEnvironment.ts) for the zsh case, if the incoming env already contains a HISTFILE, record it on envMixin as VSCODE_ZSH_HISTFILE_ORIG.
  • In shellIntegration-rc.zsh, if VSCODE_ZSH_HISTFILE_ORIG is set, restore HISTFILE from it (and unset the helper var) instead of forcing the default. The previous behavior is preserved when no custom HISTFILE was set.

The chosen env var name VSCODE_ZSH_HISTFILE_ORIG is namespaced to avoid colliding with anything users may have in their environment.

Test plan

  • In terminal.integrated.env.linux / .osx, set "HISTFILE": "${HOME}/something_else", open a new zsh terminal, and confirm env | grep HIST shows the custom path.
  • In ~/.zshenv, export HISTFILE="$HOME/something_else", open a new zsh terminal, and confirm env | grep HIST shows the custom path.
  • With no custom HISTFILE set, open a new zsh terminal and confirm HISTFILE still resolves to $USER_ZDOTDIR/.zsh_history (the prior default), so existing users are unaffected.
  • Confirm VSCODE_ZSH_HISTFILE_ORIG is unset after the rc script runs.

When shell integration is injected for zsh, the rc script unconditionally
reset HISTFILE to $USER_ZDOTDIR/.zsh_history before sourcing the user's
.zshrc. This clobbered any HISTFILE the user had set via .zshenv or via
the terminal.integrated.env.* setting.

Capture any incoming HISTFILE in envMixin as VSCODE_ZSH_HISTFILE_ORIG,
then restore it in shellIntegration-rc.zsh when present, falling back to
the previous default otherwise.

Fixes microsoft#169264
Copilot AI review requested due to automatic review settings May 6, 2026 01:44
@vs-code-engineering
Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@anthonykim1

Matched files:

  • src/vs/platform/terminal/node/terminalEnvironment.ts

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to ensure VS Code’s zsh shell integration does not clobber a user’s custom HISTFILE when shell integration is injected, restoring the original history file location instead of forcing $USER_ZDOTDIR/.zsh_history (fixes #169264).

Changes:

  • Capture an incoming HISTFILE value during zsh shell integration injection and pass it through as VSCODE_ZSH_HISTFILE_ORIG.
  • Update the injected zsh rc script to restore HISTFILE from VSCODE_ZSH_HISTFILE_ORIG (and unset the helper variable) when present.
  • Preserve the prior default behavior when no custom HISTFILE was detected.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/vs/workbench/contrib/terminal/common/scripts/shellIntegration-rc.zsh Restores HISTFILE from a helper env var during injected startup instead of always forcing the default path.
src/vs/platform/terminal/node/terminalEnvironment.ts Records an existing HISTFILE into envMixin for zsh injection so the rc script can restore it.

if [[ -n "$VSCODE_ZSH_HISTFILE_ORIG" ]]; then
HISTFILE="$VSCODE_ZSH_HISTFILE_ORIG"
builtin unset VSCODE_ZSH_HISTFILE_ORIG
else
envMixin['USER_ZDOTDIR'] = userZdotdir;
// Record any custom HISTFILE so the shell integration script can restore it
// instead of overwriting it with the default location (see #169264).
if (env?.HISTFILE) {
Comment on lines +258 to +262
// Record any custom HISTFILE so the shell integration script can restore it
// instead of overwriting it with the default location (see #169264).
if (env?.HISTFILE) {
envMixin['VSCODE_ZSH_HISTFILE_ORIG'] = env.HISTFILE;
}
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

Successfully merging this pull request may close these issues.

zsh HISTFILE Hardcoded after #168396 fix

3 participants