wez / wezterm

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

Wezterm changes current working directory on nvim lsp attach #5552

Open rijulkap opened 2 weeks ago

rijulkap commented 2 weeks ago

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

Windows

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

No response

WezTerm version

wezterm 20240203-110809-5046fc22

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

No, and I'll explain why below

Describe the bug

I've been experimenting with using Neovim (0.10) with Wezterm for writing python code.

When the python LSP (stable Pyright 1.1.367) used attaches to my current neovim buffer, the Wezterm current working directory also seems to change.

Wezterm then detects the current foreground process and changes it to also changes to node.exe (which is what pyright runs on). Spawning a new tab now causes the new tab to open in the directory pyright is installed in.

Opening a terminal in neovim causes it to open in the correct directory (the original cwd, and not the node directory)

This does not seem to be the case on Linux machines as using the same config on WSL seems to work as intended.

To Reproduce

  1. Open Neovim, configured to start LSP servers (in this case specifically pyright) depending on file type
  2. Create a python file eg: temp.py
  3. Open a new tab

Configuration

local wezterm = require("wezterm")
local act = wezterm.action

local config = {}
-- Use config builder object if possible
if wezterm.config_builder then
    config = wezterm.config_builder()
end

if wezterm.target_triple == "x86_64-pc-windows-msvc" then
    config.default_prog = { "pwsh.exe", "-NoLogo" }
end

config.window_close_confirmation = "AlwaysPrompt"
config.default_workspace = "main"

config.use_fancy_tab_bar = false
config.status_update_interval = 1000
config.tab_bar_at_bottom = false
wezterm.on("update-status", function(window, pane)
    -- Workspace name
    local stat = window:active_workspace()

    if window:active_key_table() then
        stat = window:active_key_table()
    end
    if window:leader_is_active() then
        stat = "LDR"
    end

    local basename = function(s)
        return string.gsub(s, "(.*[/\\])(.*)", "%2")
    end

    local reduce_filepath = function(filepath)
        -- Split the file path into its components
        local parts = {}
        for part in string.gmatch(filepath, "[^/\\]+") do
            table.insert(parts, part)
        end

        -- Check if the path has more than 3 parts (2 parents + 1 file/folder)
        if #parts > 3 then
            -- Keep only the last 3 parts and add double dots before the outermost folder
            parts = { "..", parts[#parts - 2], parts[#parts - 1], parts[#parts] }
        end

        -- Reconstruct the reduced file path
        return table.concat(parts, "/")
    end

    -- Current working directory
    local cwd = pane:get_current_working_dir()
    cwd = cwd and reduce_filepath(cwd.file_path)

    -- Current command
    local cmd = pane:get_foreground_process_name()
    cmd = cmd and basename(cmd) or ""

    -- Left status (left of the tab line)
    window:set_left_status(wezterm.format({
        { Text = "  " },
        { Text = wezterm.nerdfonts.oct_table .. "  " .. stat },
        { Text = " |" },
    }))

    -- Right status
    window:set_right_status(wezterm.format({
        { Text = wezterm.nerdfonts.md_folder .. "  " .. cwd },
        { Text = " | " },
        { Text = wezterm.nerdfonts.fa_code .. "  " .. cmd },
        "ResetAttributes",
        { Text = " | " },
        { Text = wezterm.nerdfonts.md_clock .. "  " .. time },
        { Text = "  " },
    }))
end)

return config

Expected Behavior

The new tab is created in the original current working directory. The cwd variable from the config above is the original cwd

Logs

No response

Anything else?

No response

akthe-at commented 2 weeks ago

You should share your lsp config and let us know how you are going about activating your venv. Are you doing that manually? Using a plug-in? I just ask because I can not reproduce this on Windows (where I work on the daily with wezterm and python)

rijulkap commented 2 weeks ago

Good point.

I've configured powershell to activate my venv on every new instance. I currently use a a neovim plugin via lazy (lspconfig) to configure my LSP server. The configuring values are left as default.

Here's a snippet of relevant config code:

{ 
    'neovim/nvim-lspconfig',
    dependencies = {
      'williamboman/mason.nvim',
      'williamboman/mason-lspconfig.nvim',
      'WhoIsSethDaniel/mason-tool-installer.nvim',

      { 'folke/neodev.nvim', opts = {} },
    },
    config = function()
      local capabilities = vim.lsp.protocol.make_client_capabilities()
      capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())

      local servers = {
        pyright = {}
      }

      require('mason').setup()

      local ensure_installed = vim.tbl_keys(servers or {})

      require('mason-tool-installer').setup { ensure_installed = ensure_installed }

      require('mason-lspconfig').setup {
        handlers = {
          function(server_name)
            local server = servers[server_name] or {}
            server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
            require('lspconfig')[server_name].setup(server)
          end,
        },
      }
}

Do you use Powershell as your default shell within Wezterm?

akthe-at commented 2 weeks ago

Can you show the push script too? Any chance you have the plugin venv-selector installed?

As for your question I use pwsh.exe with wezterm but not powershell.exe

rijulkap commented 2 weeks ago

I use Anaconda to manage my python envs. My Conda environment is set up within powershell using the 'conda init' command.

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "C:\Users\***\miniconda3\Scripts\conda.exe") {
    (& "C:\Users\***\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion

Nope, i do not have the that plugin installed, and I too am using pwsh.exe, not powershell.