wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
16.72k stars 749 forks source link

User vars become unset after detach and re-attach to multiplexer Unix domain #5832

Open relausen opened 1 month ago

relausen commented 1 month ago

What Operating System(s) are you seeing this problem on?

macOS

Which Wayland compositor or X11 Window manager(s) are you using?

No response

WezTerm version

20240719-081740-643d85f8

Did you try the latest nightly build to see if the issue is better (or worse!) than your current version?

Yes, and I updated the version box above to show the version of the nightly that I tried

Describe the bug

Context

The problem

Using the IS_NVIM user var in my WezTerm Lua code to control if keys should be interpreted by WezTerm or relayed to the terminal (and by extension to Neovim) generally works like a charm. But if I have Neovim running when detaching from the multiplexer (or just quit WezTerm), and subsequently attach again, the IS_NVIM user var is no longer set, meaning that the keystrokes I want to pass through to the shell/Neovim are not passed through.

I have tried using the WEZTERM_PROG user var set by shell integration, and that exposes the same problem, confirmed by logging the value from my WezTerm Lua code.

This indicates - as suggested by @wez in this comment - that user vars are not synchronized on attach after a detach.

To Reproduce

  1. Start WezTerm using the config below, and ensure that shell integration is set up
  2. Set the IS_NVIM user var to true: __wezterm_set_user_var IS_NVIM "true"
  3. To ascertain that the WezTerm config works: Run cat and type ^H and ^K and see that these are sent to the shell/cat. Do not exit cat.
  4. Detach from the muxer or quit WezTerm
  5. Attach or start WezTerm again, going back to the running cat process. Type ^H and ^K again and observe that they are no longer sent to the shell

Configuration

The smallest relevant subset of my config:

local wezterm = require("wezterm")
local config = wezterm.config_builder()

local function is_vim(pane)
  return pane:get_user_vars().IS_NVIM == "true"
end

local direction_keys = {
  h = "Left",
  j = "Down",
  k = "Up",
  l = "Right",
}

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 = {
  split_nav("h"),
  split_nav("j"),
  split_nav("k"),
  split_nav("l"),
}

config.unix_domains = {
  {
    name = "unix",
  },
}
config.default_gui_startup_args = { "connect", "unix" }

return config

Expected Behavior

I expect the user vars to be retained between detach/attach, making the UX consistent between newly started sessions and re-attached sessions.

Logs

No response

Anything else?

No response

kwertyops commented 1 month ago

I see this has already been acknowledged, but I'm just jumping in here because by coincidence this is my first day using wezterm, and I already ran into this bug.

I was following along with this tutorial to get it set up: https://alexplescan.com/posts/2024/08/10/wezterm/

In that post it uses config to add the hostname to the top-right of the window with wezterm.hostname(). It works great except that wezterm.hostname() doesn't change to reflect remote hosts during SSH, for example (in my opinion it should, but that's a different issue).

I did some digging and the only way I could figure out to have different SSH windows display their corresponding hostname was to set a user var in my remote host's bashrc/zshrc. Doing so works perfectly (!) except:

  1. I can't figure out how to reset it in the same window when disconnecting from the host (I could be okay with this because wezterm connect opens its own new window anyways)
  2. If I detach from a multiplexed connection, and then reattach, the user var is not set so the hostname does not match in the reattached session (obviously because bashrc doesn't get run again).

This is less an issue for me than relausen I think, because it's not a workflow thing, just a quality of life thing.. but it appears to be the same issue.

This may not be the place for it but since I'm here I'll also ask -- I'm curious if anyone has better ideas for an SSH-sensitive hostname display configuration than using user vars?