tmux-python / libtmux

⚙️ Python API / wrapper for tmux
https://libtmux.git-pull.com
MIT License
1.01k stars 104 forks source link

libtmux is picking up the wrong tmux version #396

Open mdeguzis opened 2 years ago

mdeguzis commented 2 years ago

See: https://github.com/tmux-python/tmuxp/issues/794

Started new server, protocol mismatch. tmuxp seems to be using /usr/bin/tmux instead of what I have aliased.

$ tmux -V
tmux.orig 2.8

$ /usr/bin/tmux -V
tmux 1.8

The problem for me is https://github.com/tmux-python/libtmux/blob/master/libtmux/common.py#L241 Your search paths are then placing the system paths AFTER the custom patch which tmux pulls up:

241     def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
242         tmux_bin = which(
243             "tmux",
244             default_paths=kwargs.get(
245                 "tmux_search_paths",
246                 ["/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin"],
247             ),
248             append_env_path=kwargs.get("append_env_path", True),
249         )

This really should be the other way around.

$ which tmux
tmux=/apollo/env/envImprovement/bin/tmux

I'd hate to have to fork this, so can we just perform a subprocess.run which tmux instead? Why do we need to search paths?

tmuxp:

-------------------------
environment:
        dist: Linux-5.4.204-124.362.amzn2int.x86_64-x86_64-with-glibc2.2.5
        arch: x86_64
        uname: Linux; dev-dsk-deguzim-1a-fa82568b.us-east-1.amazon.com; 5.4.204-124.362.amzn2int.x86_64
        version: #1 SMP Wed Jul 13 03:24:31 UTC 2022
-------------------------

tmux version: 1.8
libtmux version: 0.12.0
tmuxp version: 1.12.1
tmux path: /usr/bin/tmux
tmuxp path: /home/deguzim/.local/lib/python3.7/site-packages/tmuxp
shell: /bin/zsh
-------------------------
tmux sessions:

        protocol version mismatch (client 7, server 8)
tmux windows:

        protocol version mismatch (client 7, server 8)
tmux panes:

        protocol version mismatch (client 7, server 8)
tmux global options:

        protocol version mismatch (client 7, server 8)
tmux window options:

        protocol version mismatch (client 7, server 8)
mdeguzis commented 2 years ago

The problem is how this code detects the version. It needs to be using regex, not list indices:

$ tmuxp freeze --force -f yaml --yes --quiet
DEBUG:
['/apollo/env/envImprovement/bin/tmux', '-V']
DEBUG:
b'tmux.orig 2.8\n'
b''

This code needs to go away in favor of regex:

version = proc.stdout[0].split("tmux ")[1]

Test

>>> ver = "tmux 1.8"
>>> import re
>>> re.search('[0-9]{1}.[0-9]', ver).group(0)
'2.8'

>>> ver = "tmux 2.3"
>>> re.search('[0-9]{1}.[0-9]', ver).group(0)
'2.3'

Tmux versions can have a letter after it as well, so this regex should work

# tmux 3.3a · tmux 3.3 · tmux 3.2a · tmux 3.2 · tmux 3.1c · tmux 3.1b · tmux
[0-9]{1}.[0-9]

Will you accept a CR for this?

mdeguzis commented 2 years ago

Fixed tmuxp:

$ tmuxp freeze --force -f yaml --yes --quiet
Save to: /home/deguzim/.tmuxp/work.yaml [/home/deguzim/.tmuxp/work.yaml]: 

Submitted PR: https://github.com/tmux-python/libtmux/pull/397

tony commented 2 years ago

@mdeguzis can you look at the tests? Can you elaborate a bit more on the PR and what it's fixing?

How are you installing tmux? e.g. why is the name of it tmux.orig?

mdeguzis commented 2 years ago

It's just how tmux from our company is done, I don't see why a regex that picks up the version N.N is a bad thing, it should not be using .split to pull the version and accessing an index. I have 0 problems when using version detection based on regex. Our company probably appends orig, since the system tmux exists and is very old (1.8).

As far as the tests, throwing different strings at it like tmux.orig 2.8 would be ideal.

rockandska commented 2 years ago

Why do we need to search paths?

Not directly related to the issue, but I had the same concern about the reason to force the paths instead of relaying on PATH env

tony commented 2 years ago

It's just how tmux from our company is done, I don't see why a regex that picks up the version N.N is a bad thing, it should not be using .split to pull the version and accessing an index. I have 0 problems when using version detection based on regex. Our company probably appends orig, since the system tmux exists and is very old (1.8).

@mdeguzis We welcome PRs that improve support and compatibility

On that note, are you still committed to doing #397? Since this is the first time I've seen this case - it needs a champion to take it to the finish line (while preserving the behavior that works)

tony commented 2 years ago

@rockandska

Not directly related to the issue, but I had the same concern about the reason to force the paths instead of relaying on PATH env

I'm happy to have better support in this regard. Are you open to making a PR for this simplification?

tony commented 2 years ago

@mdeguzis Does v0.15.0a1 change anything with this?

v0.15.0a1: GitHub release, git tag, PyPI package

pip install libtmux==0.15.0a1
mdeguzis commented 2 years ago
$ pip3 install libtmux==0.15.0a1
Defaulting to user installation because normal site-packages is not writeable
Collecting libtmux==0.15.0a1
  Using cached libtmux-0.15.0a1-py3-none-any.whl (33 kB)
Installing collected packages: libtmux
  Attempting uninstall: libtmux
    Found existing installation: libtmux 0.14.2
    Uninstalling libtmux-0.14.2:
      Successfully uninstalled libtmux-0.14.2
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.                                                        
tmuxp 1.13.1 requires libtmux<0.15.0,>=0.14.0, but you have libtmux 0.15.0a1 which is incompatible.

What tag of tmuxp works for this lib version?

tony commented 2 years ago

@mdeguzis

pip install libtmux==0.15.0a1 --pre

Does that work?

If not, https://github.com/tmux-python/tmuxp/pull/805 may be needed

pip install --user 'git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'
pipx install --suffix=@libtmux-v0.15 'tmuxp @ git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'
tony commented 2 years ago

@mdeguzis P.S. If you do above, you may need to pip uninstall libtmux and pip uninstall tmuxp first.

mdeguzis commented 2 years ago
pip3 uninstall libtmux
pip3 uninstall tmuxp

$ pip3 install --user 'git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'a                                                                                                        
Collecting git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15a
  Cloning https://github.com/tmux-python/tmuxp.git (to revision libtmux-v0.15a) to /tmp/pip-req-build-o6j9st1k
  Running command git clone --quiet https://github.com/tmux-python/tmuxp.git /tmp/pip-req-build-o6j9st1k
  warning: You appear to have cloned an empty repository.
  WARNING: Did not find branch or tag 'libtmux-v0.15a', assuming revision or ref.
  error: subprocess-exited-with-error

  × git rev-parse HEAD did not run successfully.
  │ exit code: 128
  ╰─> [2 lines of output]
      HEAD
      fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
      Use '--' to separate paths from revisions, like this:
      'git <command> [<revision>...] -- [<file>...]'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× git rev-parse HEAD did not run successfully.
│ exit code: 128
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

$ pip3 install --suffix=@libtmux-v0.15 'tmuxp @ git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'

Usage:   
  /usr/bin/python3 -m pip install [options] <requirement specifier> [package-index-options] ...
  /usr/bin/python3 -m pip install [options] -r <requirements file> [package-index-options] ...
  /usr/bin/python3 -m pip install [options] [-e] <vcs project url> ...
  /usr/bin/python3 -m pip install [options] [-e] <local project path> ...
  /usr/bin/python3 -m pip install [options] <archive url/path> ...

no such option: --suffix

In the past I would just checkout master/mainline/branch and do pip3 install -e .. I'll defer to you on what I should be running.

tony commented 2 years ago

@mdeguzis Typo, trailing a: pip3 install --user 'git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'a

Do: pip3 install --user 'git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'

It's not master since it's a PR for libtmux-v0.15: https://github.com/tmux-python/tmuxp/pull/805 image

libtmux v0.15 isn't in master yet

mdeguzis commented 2 years ago
$  pip3 install --user 'git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15'
Collecting git+https://github.com/tmux-python/tmuxp.git@libtmux-v0.15
  Cloning https://github.com/tmux-python/tmuxp.git (to revision libtmux-v0.15) to /tmp/pip-req-build-7ekjpwtu
  Running command git clone --quiet https://github.com/tmux-python/tmuxp.git /tmp/pip-req-build-7ekjpwtu
  warning: You appear to have cloned an empty repository.
  WARNING: Did not find branch or tag 'libtmux-v0.15', assuming revision or ref.
  error: subprocess-exited-with-error

  × git rev-parse HEAD did not run successfully.
  │ exit code: 128
  ╰─> [2 lines of output]
      HEAD
      fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
      Use '--' to separate paths from revisions, like this:
      'git <command> [<revision>...] -- [<file>...]'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× git rev-parse HEAD did not run successfully.
│ exit code: 128
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.