Farhad
Farhad

Reputation: 324

Vscode terminal not loading .zshrc on startup! How can I fix this?

I have code package installed my on Pop!_OS 21.10, and recently after opening the integrated terminal, I noticed that it does not load my .zshrc.

Here are my settings:

 "terminal.integrated.defaultProfile.linux": "zsh",

Note: Everything works fine when I run source .zshrc after opening the integrated terminal, but it does not load the profile automatically. Anyone knows why?

Upvotes: 12

Views: 20414

Answers (6)

Remi Kristelijn
Remi Kristelijn

Reputation: 41

In my case the nvm plugin in VSC caused the issue, uninstalling solved it

Upvotes: 0

marcelocra
marcelocra

Reputation: 2503

Double check if you are using the correct zsh binary.

I had two zsh binaries and VSCode was using the wrong one, which wouldn't load the .zshrc.

Upvotes: 0

ajinzrathod
ajinzrathod

Reputation: 960

TL; DR

If you installed VS Code using Flathub but are comfortable with using a .deb package, you might want to switch.

On my system, VS Code was initially installed from Flathub, and it didn't detect Zsh. I resolved this by uninstalling the Flathub version and installing VS Code using the .deb package, which then successfully detected Zsh.

Explanation

When you install VS Code from different sources (like Flathub or a .deb package), the configuration and environment setup can vary. why Zsh might not be detected when installing from Flathub:

Flatpak: Applications installed via Flatpak are sandboxed, meaning they have limited access to the host system. This can affect how environment variables and shell configurations are accessed. Flatpak applications might not inherit environment variables and configurations.

.deb Package: When installed via a .deb package, VS Code has full access to the system environment, including shell configurations.

Upvotes: 1

Sedat Kilinc
Sedat Kilinc

Reputation: 2961

I had the same problem and had also to manually execute

source ~/.zshrc 

And long story short I solved it with a symlink by executing

ln -s ../.zshrc $ZDOTDIR/.zshrc

Explanation how I came up with this solution and why I prefer this solution over two other possible solutions that are working and have been proposed above:

Following the documentation in

https://code.visualstudio.com/docs/terminal/shell-integration

I've added the following line at the end of my ~/.zshrc

[[ "$TERM_PROGRAM" == "vscode" ]] && . "$(code --locate-shell-integration-path zsh)"

which sources a script whenever VSCode gets started.

But that alone didn't fix that particular problem.

When looking at that script by executing

less "$(code --locate-shell-integration-path zsh)"

I found following line that caused this problem

. $USER_ZDOTDIR/.zshrc

with $USER_ZDOTDIR pointing to ~/.zsh The variable $USER_ZDOTDIR even sets the variable $ZDOTDIR So setting

USER_ZDOTDIR=${HOME}

would have solved it, but since $ZDOTDIR is very crucial and pointing it to ${HOME} could break things by missing the ${HOME}/.zsh/.zshenv with $ZDOTDIR/.zshenv pointing to it.

For example, in my $ZDOTDIR/.zshenv scripts are sourced which paths are added to the $PATH variable in (e.g. $HOME/.cargo/bin)

To have a fast look on that script I recommend executing

cat "$(code --locate-shell-integration-path zsh)" | grep -E '\bUSER_ZDOTDIR\b'

results in (Attention: these are not consecutive lines)

ZDOTDIR=$USER_ZDOTDIR
    HISTFILE=$USER_ZDOTDIR/.zsh_history
    if [[ $options[norcs] = off  && -f $USER_ZDOTDIR/.zshrc ]]; then
        ZDOTDIR=$USER_ZDOTDIR
        . $USER_ZDOTDIR/.zshrc
if [[ $options[login] = off && $USER_ZDOTDIR != $VSCODE_ZDOTDIR ]]; then
    ZDOTDIR=$USER_ZDOTDIR

(Attention: these are not consecutive lines) with

cat "$(code --locate-shell-integration-path zsh)" | grep -E '\bUSER_ZDOTDIR\b' -A 2 -B 2

you get these lines with some context! And by increasing the numbers at the end you can display more context

But you can see the line causing the problem

. $USER_ZDOTDIR/.zshrc

and

cat "$(code --locate-shell-integration-path zsh)" | grep -E '\bZDOTDIR\b' # -A 1 -B 1

you can have a look concerning the effects of $ZDOTDIR. Remove the # and change the number after -A and -B to include more or less context.

You also don't have to be concerned about your $HISTFILE in spite of the line

HISTFILE=$USER_ZDOTDIR/.zsh_history

which stays the same with this solution as long it's set in your ~/.zshrc file and overwrites this assignment.


Off-Topic but potentially useful

While searching for the solution which fits the most I found out that you could have a different history files for VSCode only by adding also

[[ "$TERM_PROGRAM" == "vscode" ]] && HISTFILE=${ZDOTDIR}/.vscode_history

which can be very useful for shell-intensive projects like npm, yarn etc. are.

But the commands have to be executed inside the VSCode-Shell to end up in said history file.

Since I have created⁽¹⁾ two aliases which are useful for myself and possible for others, too

You can also have history files which are only assigned to a single project.

All the following extra history files are contained in the $ZDOTDIR folder

This alias temporarily sets $HISTFILE according to the folder you're in and should only executed in the project's root folder

alias set-project-histfile='eval "[[ \"\$TERM_PROGRAM\" == \"vscode\" ]] && HISTFILE=\${ZDOTDIR}/\$(echo \$PWD | rev  | cut -d '/' -f1  | rev)_history"'

while the following alias writes the $HISTFILE assignment which is like above also only in effect when using the VSCode terminal at the end of $HOME/.zshrc making it permanent until this line is deleted manually

alias set-project-histfile-permanent='echo "[[ \"\$TERM_PROGRAM\" == \"vscode\" ]] && HISTFILE=\${ZDOTDIR}/\$(echo \$PWD | rev  | cut -d '/' -f1  | rev)_history" >> ${HOME}/.zshrc'

Keep in mind that this $HISTFILE assignment has the effect that each project you're working with VSCode has its own history file in the $ZDOTDIR folder.

Examples:

$ZDOTDI/projectFoo_history
$ZDOTDIR/projectBar_history
$ZDOTDIR/projectFoobar_history
$ZDOTDIR/Fugazi_history
…
$HOME/.zsh/Fugazi_history

and the command you execute in VSCode's terminal end up in these history file instead of the usual one.

Upvotes: 5

Victor
Victor

Reputation: 3828

The current version of VSCode may ignore your .zshrc in the following situation:

  • the option Terminal › Integrated › Shell Integration: Enabled (terminal.integrated.shellIntegration.enabled) is checked
  • the environment variable $ZDOTDIR is empty

Consider the comment and add the following line to your settings.json:

"terminal.integrated.profiles.osx": { "zsh": { "path": "/bin/zsh", "args": ["-l", "-i"] } }

(osx in the setting's name is for MacOS, linux is for Linux)

Upvotes: 15

Farhad
Farhad

Reputation: 324

I have already defined ZDOTDIR variable incorrectly pointing to .cache in my .zshrc, which made code to look over there for my config file.

The problem is solved now by defining ZDOTDIR as my $HOME directory.

Update:

-- $HOME is the default so I just deleted my definition.

Upvotes: 3

Related Questions