fix(lsp): use root_dir as cmd CWD #40331

Problem:
cmd given as string[] always starts using Nvim's CWD, which is arbitrary.

Solution:
If cmd_cwd is not given, use root_dir as CWD.

BREAKING CHANGE: LSP commands given as string arrays now use `root_dir` as
the process working directory when `cmd_cwd` is unset.

Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
This commit is contained in:
Barrett RuthandJustin M. Keyes authored and GitHub committed 2026-06-26 14:12:33 -04:00
1 parent bc3757b7bf
commit 85718f9874
4 files changed
+55 -5

No files matched your search

+46
View File
@@ -2932,6 +2932,52 @@ describe('LSP', function()
end)
describe('cmd', function()
it('cmd string[] CWD', function()
local root_dir = tmpname(false)
local cmd_cwd = tmpname(false)
mkdir(root_dir)
mkdir(cmd_cwd)
local cwd = exec_lua(function()
return assert(vim.uv.cwd())
end)
local cases = {
{
desc = 'cmd_cwd takes precedence',
config = { name = 'cwd-test-cmd-cwd', root_dir = root_dir, cmd_cwd = cmd_cwd },
expected_cwd = cmd_cwd,
},
{
desc = 'root_dir is used when cmd_cwd is unset',
config = { name = 'cwd-test-root-dir', root_dir = root_dir },
expected_cwd = root_dir,
},
{
desc = 'current cwd is used when cmd_cwd and root_dir are unset',
config = { name = 'cwd-test-current-cwd' },
expected_cwd = cwd,
},
}
for _, case in ipairs(cases) do
local outfile = tmpname(false)
local config = vim.tbl_extend('force', {
cmd = {
n.nvim_prog,
'--clean',
'--headless',
('+call writefile([getcwd()], %q)'):format(outfile),
'+qa!',
},
}, case.config)
exec_lua(function(conf)
assert(vim.lsp.start(conf, { attach = false }))
end, config)
t.assert_log(vim.pesc(case.expected_cwd), outfile)
end
end)
it('connects to lsp server via rpc.connect using ip address', function()
exec_lua(create_tcp_echo_server)
exec_lua(function()