fix(lsp): warn on missing config in :checkhealth #33087

Problem
When calling `:checkhealth vim.lsp` after the user has enabled a language
server with `vim.lsp.enable` that has no configuration a runtime error
is hit because the code expects for a configuration to exist.

Solution:
Check if a configuration was returned before parsing it, if it isn't
returned then warn the user that the server has been enabled but a
configuration was not found.

(cherry picked from commit 5554fcc286)
This commit is contained in:
Micah Halter
2025-03-28 08:46:10 -04:00
committed by github-actions[bot]
parent bbf912d72f
commit a9afa6b152

View File

@ -187,26 +187,32 @@ local function check_enabled_configs()
local config = vim.lsp.config[name] local config = vim.lsp.config[name]
local text = {} --- @type string[] local text = {} --- @type string[]
text[#text + 1] = ('%s:'):format(name) text[#text + 1] = ('%s:'):format(name)
for k, v in if not config then
vim.spairs(config --[[@as table<string,any>]]) report_warn(
do ("'%s' config not found. Ensure that vim.lsp.config('%s') was called."):format(name, name)
local v_str --- @type string? )
if k == 'name' then else
v_str = nil for k, v in
elseif k == 'filetypes' or k == 'root_markers' then vim.spairs(config --[[@as table<string,any>]])
v_str = table.concat(v, ', ') do
elseif type(v) == 'function' then local v_str --- @type string?
v_str = func_tostring(v) if k == 'name' then
else v_str = nil
v_str = vim.inspect(v, { newline = '\n ' }) elseif k == 'filetypes' or k == 'root_markers' then
end v_str = table.concat(v, ', ')
elseif type(v) == 'function' then
v_str = func_tostring(v)
else
v_str = vim.inspect(v, { newline = '\n ' })
end
if k == 'cmd' and type(v) == 'table' and vim.fn.executable(v[1]) == 0 then if k == 'cmd' and type(v) == 'table' and vim.fn.executable(v[1]) == 0 then
report_warn(("'%s' is not executable. Configuration will not be used."):format(v[1])) report_warn(("'%s' is not executable. Configuration will not be used."):format(v[1]))
end end
if v_str then if v_str then
text[#text + 1] = ('- %s: %s'):format(k, v_str) text[#text + 1] = ('- %s: %s'):format(k, v_str)
end
end end
end end
text[#text + 1] = '' text[#text + 1] = ''