(Am on a roll this week! After a long dry spell this is my 4th post in 3 days / 5th post this month…)
Since earlier this week whenever I’d launch VSCode or VSCodium (yes I started using the latter now) I’d always get a message saying its unable to resolve my shell environment. Like here.
I tracked it down to the fact that earlier this week I had started launching tmux
by default on my desktops. Previously I would only launch it when SSHing into a machine but I figured it’s good to have it always launch on my desktops so I can easily connect to them from elsewhere. This was obviously interfering with VSCode. Interestingly, I had added the tmux
launching code in .bash_profile
so as to limit it to login sessions, but I guess VSCode launches bash as a login shell.
What to do here? Inspired by this GitHub issue I added the following line to my .bash_profile
and disabled the bit that launches tmux
.
1 |
env > ~/Downloads/blah.txt |
This dumps the environment variables that are present when the shell is launched by VSCode. Here I found a variable VSCODE_RESOLVING_ENVIRONMENT
that was set to 1. Boom, that’s my entry!
Before I go further, here’s how I currently launch tmux
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Machines where I always want to launch tmux tmux_by_default=( "machine1" "machine2" ) # A function to join the array # https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-a-bash-array-into-a-delimited-string function join_by { local IFS="$1"; shift; echo "$*"; } # So join_by "|" "${tmux_by_default[@]}" will create "machine1|machine2" which I can then regex over :) if [[ $(hostname) =~ $(join_by "|" "${tmux_by_default[@]}") ]] || [[ ! -z "$SSH_TTY" ]]; then # Check if tmux is installed, else warn that if command -v tmux &> /dev/null; then # Check if I am in a tmux session already. If I am not in one then $TMUX is empty. if [[ -z "$TMUX" ]]; then if tmux ls | grep -qv attached; then # attach to the existing session and when it exits if the exit code is 0 then exit the shell (C-b d in tmux has exit code 0) exec tmux at && exit else # create a new session and when it exits if the exit code is 0 then exit the shell (C-b d in tmux has exit code 0) exec tmux new-session && exit fi fi else echo -e "tmux missing!" fi fi |
For certain machines it always launches, for the rest only if I am connecting via SSH. So I made one change:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Machines where I always want to launch tmux tmux_by_default=( "machine1" "machine2" ) # A function to join the array # https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-a-bash-array-into-a-delimited-string function join_by { local IFS="$1"; shift; echo "$*"; } # So join_by "|" "${tmux_by_default[@]}" will create "machine1|machine2" which I can then regex over :) if ([[ $(hostname) =~ $(join_by "|" "${tmux_by_default[@]}") ]] && [[ -z "$VSCODE_RESOLVING_ENVIRONMENT" ]]) || ([[ ! -z "$SSH_TTY" ]]); then # Check if tmux is installed, else warn that if command -v tmux &> /dev/null; then # Check if I am in a tmux session already. If I am not in one then $TMUX is empty. if [[ -z "$TMUX" ]]; then if tmux ls | grep -qv attached; then # attach to the existing session and when it exits if the exit code is 0 then exit the shell (C-b d in tmux has exit code 0) exec tmux at && exit else # create a new session and when it exits if the exit code is 0 then exit the shell (C-b d in tmux has exit code 0) exec tmux new-session && exit fi fi else echo -e "tmux missing!" fi fi |
Now the section that always launches it on certain machines also checks if the VSCODE_RESOLVING_ENVIRONMENT
variable is empty and only then launches.
Sweet!
ps. I knew about VSCodium but didn’t really look into switching to it. Then I came across this blog post and that resonated with me so I started making the switch. I still have VSCode around coz a couple of the extensions I use only work with it, but I primarily use VSCodium otherwise. I recommend others too give it a shot. I also recommend the Night Owl theme, it’s available on both.