Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: wezterm CLI commands seem to run through shell, but it shouldn't #261

Open
1 task done
mister-choo opened this issue Nov 25, 2024 · 16 comments
Open
1 task done
Assignees
Labels
bug Something isn't working

Comments

@mister-choo
Copy link

Similar Issues

  • Before filing, I have searched for similar issues.

Neovim Version

NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1731601260

Multiplexer Integration

Wezterm

Multiplexer Version

wezterm version: 20241119-101432-4050072d x86_64-unknown-linux-gnu
Window Environment: X11 GNOME Shell
Lua Version: Lua 5.4

Steps to Reproduce

  1. Open nvim
  2. Create a wezterm split
  3. Navigate to split with direction keys

Expected Behavior

Navigation from wezterm pane to nvim pane to take the same amount of time as from nvim pane to wezterm

Actual Behavior

Navigating from nvim pane to wezterm pane happens with about 2 sec delay

Minimal Configuration to Reproduce

local root = vim.fn.fnamemodify('./.repro', ':p')

-- set stdpaths to use .repro
for _, name in ipairs({ 'config', 'data', 'state', 'cache' }) do
  vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name
end

-- bootstrap lazy
local lazypath = root .. '/plugins/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    'git',
    'clone',
    '--filter=blob:none',
    '--single-branch',
    'https://github.com/folke/lazy.nvim.git',
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  -- do not remove the colorscheme! it makes testing nicer
  'folke/tokyonight.nvim',
  'mrjones2014/smart-splits.nvim',
  -- add any other pugins here
}

require('lazy').setup(plugins, {
  root = root .. '/plugins',
})

require('smart-splits').setup({
  -- add any options here
})

vim.keymap.set('n', '<C-Left>', require('smart-splits').move_cursor_left)
vim.keymap.set('n', '<C-Down>', require('smart-splits').move_cursor_down)
vim.keymap.set('n', '<C-Up>', require('smart-splits').move_cursor_up)
vim.keymap.set('n', '<C-Right>', require('smart-splits').move_cursor_right)

-- add anything else here
vim.opt.termguicolors = true
-- do not remove the colorscheme! it makes testing nicer
vim.cmd([[colorscheme tokyonight]])

wezterm config:

local wezterm = require 'wezterm'
local config = {}
if wezterm.config_builder then
    config = wezterm.config_builder()
end

local function is_vim(pane)
    return pane:get_user_vars().IS_NVIM == 'true'
    -- return false
end

local direction_keys = {
    LeftArrow = 'Left',
    DownArrow = 'Down',
    UpArrow = 'Up',
    RightArrow = 'Right',
}

config.leader = { key = "a", mods = "CTRL" }

local function split_nav(key)
    return {
        key = key,
        mods = 'CTRL',
        action = wezterm.action_callback(function(win, pane)
            if is_vim(pane) then
                win:perform_action({ SendKey = { key = key, mods = 'CTRL' }, }, pane)
            else
                win:perform_action({ ActivatePaneDirection = direction_keys[key] }, pane)
            end
        end),
    }
end

config.keys = {
    {
        key = "v",
        mods = "LEADER",
        action = wezterm.action { SplitHorizontal = {
            domain = "CurrentPaneDomain" } }
    },
    split_nav('LeftArrow'),
    split_nav('RightArrow'),
    split_nav('DownArrow'),
    split_nav('UpArrow'),
}

return config

Additional Details and/or Screenshots

No response

@mister-choo mister-choo added the bug Something isn't working label Nov 25, 2024
@mister-choo
Copy link
Author

I found discussions mentioning this issue, but they were all closed. However I found this behavior to be unchanged for about a year now

@mrjones2014
Copy link
Owner

Like we've done with the other issues, could you test the performance of wezterm cli adjust-pane-size --amount 3 Left? You should be able to do this with the time command.

@mister-choo
Copy link
Author

Like we've done with the other issues, could you test the performance of wezterm cli adjust-pane-size --amount 3 Left? You should be able to do this with the time command.

Why adjust-pane-size? I'm not really using it here

@mrjones2014
Copy link
Owner

Just to narrow down if the issue is the plugin or the wezterm cli, you can try wezterm activate-pane-direction Left instead

@mister-choo
Copy link
Author

Ran this script:

set N 100
set total_time 0

for i in (seq 1 $N)
        set start_time (date +%s%N)
        wezterm cli activate-pane-direction Right
        set end_time (date +%s%N)
        set elapsed (math "($end_time - $start_time)/1000000000")
        set total_time (math "$total_time + $elapsed")
end

set average_time (math "$total_time / $N")

echo "Ran $N times."
echo "Average execution time: $average_time seconds."

And this is the output:
Ran 100 times.
Average execution time: 0.117161 seconds.

@mister-choo
Copy link
Author

Sure, maybe wezterm cli executes not as fast as it could, but I think the problem is that on one single move it here is how much wezterm_exec executes:

wezterm_exec: 
{ "wezterm", "cli", "list", "--format", "json" }
wezterm_exec: 
{ "wezterm", "cli", "list", "--format", "json" }
wezterm_exec: 
{ "wezterm", "cli", "list", "--format", "json" }
wezterm_exec: 
{ "wezterm", "cli", "list", "--format", "json" }
wezterm_exec: 
{ "wezterm", "cli", "list", "--format", "json" }
wezterm_exec: 
{ "wezterm", "cli", "activate-pane-direction", "Right" }
wezterm_exec: 
{ "wezterm", "cli", "list", "--format", "json" }

@mister-choo
Copy link
Author

Investigated this some more and looks like it's a problem with fish:
$ time wezterm cli activate-pane-direction Right

Executed in 122.03 millis fish external
usr time 3.97 millis 439.00 micros 3.53 millis
sys time 13.34 millis 79.00 micros 13.26 millis

In different shells there isn't a delay and time command add some strange time which isn't usr or sys time

@mrjones2014
Copy link
Owner

Hmm, I'm on Fish shell as well and have never been able to reproduce the performance issues.

Additionally, according to :h system()

If {cmd} is a List it runs directly (no 'shell').

So the shell shouldn't even be involved.

But, if you want to try it, you can try changing the wezterm_exec function to run the command like bash --noprofile --norc -c "wezterm cli whatever-command-here" instead of directly invoking wezterm.

We could also see if changing vim.fn.system(command) to vim.system(command):wait() makes any difference (looks like that's a newer API that's been added since I wrote this code).

@mister-choo
Copy link
Author

Hmm, I'm on Fish shell as well and have never been able to reproduce the performance issues.

Additionally, according to :h system()

If {cmd} is a List it runs directly (no 'shell').

So the shell shouldn't even be involved.

But, if you want to try it, you can try changing the wezterm_exec function to run the command like bash --noprofile --norc -c "wezterm cli whatever-command-here" instead of directly invoking wezterm.

We could also see if changing vim.fn.system(command) to vim.system(command):wait() makes any difference (looks like that's a newer API that's been added since I wrote this code).

Yes, I tested with the new api, but performance was the same
Issue was with my startup speed of fish, which I fixed and there's no delay now.
However it's still really weird, as it shouldn't be involved at all, from what I investigated I couldn't find the route cause
Maybe it's some kind of integration layer thing with wezterm.. but honestly I'm stumped

Now that I'm testing wezterm cli activate-pane-direction also improved after I improved startup time for fish

@mrjones2014
Copy link
Owner

However it's still really weird, as it shouldn't be involved at all

Yeah, this is the part that I'm still stumped by. I'm passing the command to vim.fn.system(command) as a list, which according to the help docs, means that it shouldn't involve the shell at all.

@mrjones2014
Copy link
Owner

I wonder if using uv.spawn() would change anything.

@mrjones2014 mrjones2014 changed the title [Bug]: Noticable delay on split pane navigation [Bug]: wezterm CLI commands seem to run through shell, but it shouldn't Nov 26, 2024
@georgeguimaraes
Copy link

georgeguimaraes commented Dec 10, 2024

Thanks, I thought I was going crazy changing my config, thinking it was my problem. Tks for digging into this.

Btw, i'm using wezterm, zsh and Neovim. No tmux.
My zsh takes a while to load as well (~1 second)

@georgeguimaraes
Copy link

I've removed everything from my .zshrc (compinit, zsh plugins, powerlevel10k, asdf, fzf integration) and smart-splits moved instantly from Neovim to my wezterm pane.

So yeah, it appears that zsh is being called somewhere.

@mrjones2014
Copy link
Owner

I really do not understand why. According to the Neovim help docs:

{cmd} is treated as in |jobstart()|:
If {cmd} is a List it runs directly (no 'shell').
If {cmd} is a String it runs in the 'shell', like this: >vim
  call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}'])

And my code is:

local function wezterm_exec(cmd)
  local command = vim.deepcopy(cmd)
  table.insert(command, 1, config.wezterm_cli_path)
  table.insert(command, 2, 'cli')
  return vim.fn.system(command)
end

It's a list of arguments... why is the shell involved???

@mister-choo
Copy link
Author

I really do not understand why. According to the Neovim help docs:

{cmd} is treated as in |jobstart()|:
If {cmd} is a List it runs directly (no 'shell').
If {cmd} is a String it runs in the 'shell', like this: >vim
  call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}'])

And my code is:

local function wezterm_exec(cmd)
  local command = vim.deepcopy(cmd)
  table.insert(command, 1, config.wezterm_cli_path)
  table.insert(command, 2, 'cli')
  return vim.fn.system(command)
end

It's a list of arguments... why is the shell involved???

This is probably a wezterm issue. wezterm cli might use shell in some way (weren't able to reproduce). Or observing an environment variable creates slowdown somehow

@georgeguimaraes
Copy link

Interesting @mister-choo.

It does seem that wezterm does some stuff finding a GUI to connect to and maybe spawning a shell:
https://github.com/wez/wezterm/blob/main/wezterm/src/cli/mod.rs#L168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants