wezterm icon indicating copy to clipboard operation
wezterm copied to clipboard

Wezterm changes current working directory on nvim lsp attach

Open rijulkap opened this issue 8 months ago • 4 comments

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

rijulkap avatar Jun 13 '24 08:06 rijulkap