Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.zshrc not sourced in tmux loaded in Intellij

So I'm encountering some very weird behavior which I cannot explain. I recently hopped on the tmux bandwagon and set up my shell configuration to automatically launch tmux when I open a terminal window. Around the same time, I started getting a bug, where opening a new terminal session would correctly launch tmux but then display:

zsh-newuser-install:  startup files exist, aborting.

Use the argument -f if you want to force the function to be run again.
douglasparker@gryphon /home/douglasparker% 

Now I have a pretty in-depth configuration, with my own PS1 which it clearly is not using. However I could fix this by simply running:

douglasparker@gryphon /home/douglasparker% source ~/.zshrc
douglasparker@gryphon (~)
[17-10-11 11:35:59]$ 

So simply sourcing zsh's run configuration fixes it and loads everything it should, which makes no sense as that should be sourced automatically but just isn't.

I also discovered that this only happens when the first tmux session is started from the terminal embedded in Intellij. If I open terminal normally (or even in Android Studio) it works correctly and sources ~/.zshrc automatically. However opening from Intellij specifically yields this bug. The interesting part is that if Intellij opens first, then any subsequent terminals opening tmux will show the same problem. But, if I open another terminal first and leave that open, then launching a terminal in Intellij works correctly.

By "first" I mean that there is no existing tmux server. If I close exit all open tmux windows (such that tmux ls yields "failed to connect to server"), then if the next session I open comes from a generic terminal then I'm fine and will continue to be until all tmux windows are closed again. However, if the first tmux window comes from Intellij then this bug surfaces and will continue for all terminals until I exit all the tmux windows again.

The minimum configuration to reproduce this I have found is the following ~/.zshrc file:

#!/bin/bash

# If this is a raw terminal, just open tmux and then exit
# The inner session will re-source this script and load the configuration
if ! { [ "${TERM}" = "screen" ] && [ -n "${TMUX}" ]; } then
  # Open tmux immediately
  echo "Sourced ~/.zshrc: Not in tmux" >> tmux.log
  tmux
  exit
else
  # Must already be within tmux
  echo "Sourced ~/.zshrc: In tmux" >> tmux.log
  echo hello
fi

All this does is check if it is within tmux. If not, then it opens tmux, and exits after tmux stops. Opening a session within that should source this script a second time, and print hello to the console within tmux.

If there are no tmux sessions open and I launch a normal terminal window (of if there is an existing tmux session from a normal terminal window and I launch a terminal in Intellij) then I get the expected output:

hello
douglasparker@gryphon ~% cat tmux.log
Sourced ~/.zshrc: Not in tmux
Sourced ~/.zshrc: In tmux
douglasparker@gryphon ~% 

If there are no tmux sessions open and I launch a terminal window within Intellij, then I get the output:

zsh-newuser-install:  startup files exist, aborting.

Use the argument -f if you want to force the function to be run again.
douglasparker@gryphon /home/douglasparker% cat tmux.log
Sourced ~/.zshrc: Not in tmux
douglasparker@gryphon /home/douglasparker% 

This is all printed within a tmux session in Intellij. So it is correctly sourcing the ~/.zshrc file once and launching tmux, however it simply does not source ~/.zshrc a second time within tmux under these very specific conditions and I have no idea why.

I also tried changing my shell to bash temporarily and found that the issue did not reproduce on bash. So this problem seems to come from integration between zsh, tmux, and Intellij.

I realize this is an incredibly specific problem, but does anyone know what could be causing this kind of issue? Looking up the zsh error didn't really help me find the cause of this. Is there something special that Intellij does with its terminal windows which might cause this behavior? Is there any documented reason why zsh would refuse to source ~/.zshrc?

like image 446
Douglas Parker Avatar asked Oct 11 '17 19:10

Douglas Parker


1 Answers

Based on https://youtrack.jetbrains.com/issue/IDEA-165272, in Intellij, unchecking Settings > Tools > Terminal > Shell Integration fixed this issue for me.

I have no idea what that setting actually does and can't find any documentation on it, but disabling it solves the problem.

Thanks CrazyCoder, you're a life-saver!

like image 103
Douglas Parker Avatar answered Oct 26 '22 20:10

Douglas Parker