tmux-plugins / tpm

Tmux Plugin Manager
MIT License
11.95k stars 420 forks source link

Are TPM bin/ and scripts/ scripts supposed to work outside of tmux? (unattended install) #151

Closed davidstosik closed 4 years ago

davidstosik commented 5 years ago

While working on my dotfiles, and a way to have a mostly unattended way to setup my environment, I tripped on a problem with TPM.

Imagine this basic script ~/.dotfiles/tmux/install:

#!/usr/bin/env bash

brew install tmux
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
ln -s ~/.dotfiles/tmux/tmux.conf.symlink ~/.tmux.conf

With this basic tmux configuration file in ~/.dotfiles/tmux/tmux.conf.symlink:

# TMUX plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# Bind Ctrl-A to PREFIX, instead of Ctrl-B.
unbind C-b
set -g prefix C-a

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run -b '~/.tmux/plugins/tpm/tpm'

Now I'd like to add in my tmux setup script, the proper command that will install Tmux plugins (the unattended equivalent of prefix + I).

Digging in tpm's source code, I figured out prefix + I is bound to ~/.tmux/plugins/tpm/bindings/install_plugins, which clearly states https://github.com/tmux-plugins/tpm/blob/2ab1d9101b33f3162505aaa7f99ac32631ac4371/bindings/install_plugins#L4

I thought that I could then call ~/.tmux/plugins/tpm/bin/install_plugins from my setup script in order to install plugins. Unfortunately, when I do, I receive the following error:

unknown variable: TMUX_PLUGIN_MANAGER_PATH FATAL: Tmux Plugin Manager not configured in tmux.conf Aborting.

I dug to find this error's origin and found this function: https://github.com/tmux-plugins/tpm/blob/0128e36fbe4298692fa755bfa584bd8d905f932a/scripts/helpers/plugin_functions.sh#L11-L14

As I see the first thing it does is to start-server, I imagine the script is supposed to work outside of tmux? Unfortunately it does not really:

$ tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=
unknown variable: TMUX_PLUGIN_MANAGER_PATH

$ tmux
# Then inside tmux:
$ tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=
/Users/david-stosik/.tmux/plugins/

Reading this issue, it looks like tmux start-server does not actually start the server?

It turns out I can get the command the command the command to work outside of tmux, but only if I managed to have the tmux server running at that time:

# In terminal#1:
$ tmux

# In terminal#2 (not in tmux)
$ tmux show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=
/Users/david-stosik/.tmux/plugins/

The following script seems to work both inside and outside tmux, even if tmux server is not running previously:

$ tmux new-session -d "sleep 1" && sleep 0.1 && tmux show-environment -g TMUX_PLUGIN_MANAGER_PATH

I don't have a great recommendation, but figure this might be worth looking into. (I figure if the existing script needs tmux to be running, then start-server is unnecessary? If it's supposed work with tmux server running, then this is a bug.)


My workaround for now will be to add the following to my setup script:

tmux new-session -d "sleep 1"             # Make sure tmux server is running, but that the created session will clean itself.
sleep 0.1                                 # Wait for tmux server initialization to complete.
~/.tmux/plugins/tpm/bin/install_plugins   # Install plugins
rahul0705 commented 5 years ago

๐Ÿ‘ for this, the install steps are wrong in the documentation. At very least it should be updated with these steps.

roachsinai commented 4 years ago

I followed https://github.com/tmux-plugins/tpm/blob/master/docs/managing_plugins_via_cmd_line.md expect to run ~/.tmux/plugins/tpm/bin/update_plugins all on zsh directly.

But I got:

unknown variable: TMUX_PLUGIN_MANAGER_PATH
FATAL: Tmux Plugin Manager not configured in tmux.conf
Aborting.

tmux new-session -d "sleep 1" && sleep 0.1 && ~/.tmux/plugins/tpm/bin/update_plugins all works!

Thanks a lot!

bruno- commented 4 years ago

Per author's detailed explanation I do believe this was a problem at some point. However, testing this line

tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=

with tmux versions 3.0a and 3.1b without tmux running I can't reproduce the problem.

I also just tried updating an individual plugin (on tmux 3.1b) and it worked fine:

/Users/bruno/data/var/tmux/plugins/tpm/bin/update_plugins tmux-pain-control
roachsinai commented 4 years ago

@bruno- I'm on Manjaro got that problem, too.

hmanx ~ ยป tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=
unknown variable: TMUX_PLUGIN_MANAGER_PATH
bruno- commented 4 years ago

Ok, I figured it out:

The issue seems to be with the last line from .tmux.conf: run -b '~/.tmux/plugins/tpm/tpm' The -b flag causes the issue. Things work fine when the -b flag is removed.

@roachsinai can you please try removing -b from run -b '~/.tmux/plugins/tpm/tpm' in your .tmux.conf and see if this then works

tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=
roachsinai commented 4 years ago

Yeah, confirmed that. ~/.tmux/plugins/tpm/bin/update_plugins all works if -b removed from tmux.conf.

bruno- commented 4 years ago

Ok, for now the fix is to remove -b from the last line in your tmux.conf. Have it look like this: run '~/.tmux/plugins/tpm/tpm'.

-b was added in #144. We'll revert that one unless there are some good reasons to keep -b.

bruno- commented 4 years ago

144 was reverted. I think this issue should now be fixed. If not, please reopen.

davidstosik commented 3 years ago

Hello @bruno-, sorry for the late answer, but I'm working on my dotfiles and found a link back to this issue! ๐Ÿ˜…

It looks like the problem is solved by removing the -b flag.

With a fresh install of tmux, the following script seems to get me started well! ๐Ÿ‘๐Ÿป

TPM="$HOME/.tmux/plugins/tpm"
mkdir -p "$TPM"
git clone https://github.com/tmux-plugins/tpm "$TPM"
cat <<TMUXCONF > "$HOME/.tmux.conf"
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
run '$HOME/.tmux/plugins/tpm/tpm'
TMUXCONF
${TPM}/bin/install_plugins