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

4

9

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?

Mcgaha answered 19/12, 2021 at 8:3 Comment(3)
I don't know vscode, but can you put a full command line into the terminal setting? In this case I would do a zsh -x, to better see what zsh is actually doing on startup. What happens if you try this? Another point to consider: Do you accidentally have ZDOTDIR set (i.e. do you get something when you do a echo $ZDOTDIR?).Gaea
The ZDOTDIR was the point, Thanks @GaeaMcgaha
Farhad, you have made the edit to my answer, although I approved that, but please don't make these kinds of edits, since the question already have python tag, so anyone can see from there, I have used pre tag and strong tag for focus on particular thing :) given you some points.Hort
M
3

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.

Mcgaha answered 26/12, 2021 at 9:55 Comment(3)
$HOME is the default for ZDOTDIR, so you could equally well simply unset the variable.Gaea
Yes that's a better ideaMcgaha
That worked for me. Also: relogin from your TTYWunder
L
10

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)

Laski answered 8/8, 2022 at 17:2 Comment(3)
It seems that with the recent update the zsh shell is not executed as a login shell hence it stopped reading my ~/.zprofile which defined a different ZDOTDIR than default and so was not able to find the .zshrc. This answer fixed it for me.Silvers
this breaks the restored terminals (or 'persistent sessions' you name it) featureReeher
Thank you! Asked GitHub copilot repeatedly about this but it kept pointing me to bad settings. This answer, including the link to the github issue worked a charm for me.Pointillism
M
4

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.

Mccombs answered 26/3, 2023 at 14:51 Comment(1)
ln -s ../.zshrc $ZDOTDIR/.zshrc ; surely if $ZDOTDIR doesn't point to your home folder this is simply bad advice and the correct advice would be to figure out why it is misconfigured?Studnia
M
3

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.

Mcgaha answered 26/12, 2021 at 9:55 Comment(3)
$HOME is the default for ZDOTDIR, so you could equally well simply unset the variable.Gaea
Yes that's a better ideaMcgaha
That worked for me. Also: relogin from your TTYWunder
Y
0

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.

Yam answered 7/8 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.