3

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?

Farhad
  • 75
  • 1
  • 10
  • 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`?). – user1934428 Dec 21 '21 at 08:57
  • The `ZDOTDIR` was the point, Thanks @user1934428 – Farhad Dec 26 '21 at 09:55
  • 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. – Sunderam Dubey Aug 13 '22 at 07:02

3 Answers3

6

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)

Victor
  • 3,669
  • 3
  • 37
  • 42
  • 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. – Dcompoze Sep 23 '22 at 17:12
  • this breaks the restored terminals (or 'persistent sessions' you name it) feature – Eliav Louski Jun 06 '23 at 16:04
2

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.

Farhad
  • 75
  • 1
  • 10
1

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.

Sedat Kilinc
  • 2,843
  • 1
  • 22
  • 20