Neovim+Lazy.nvim环境下lspconfig模块未找到问题求助
Neovim配置GDScript LSP引发模块未找到错误的解决办法
我当前使用Neovim + Lua + Lazy.nvim,已安装nvim-lspconfig。为启用GDScript支持,添加了对应的LSP配置代码,但重启Neovim后出现「模块未找到」错误,导致除原生命令外的所有自定义命令失效。尝试过ChatGPT,但给出的方案均针对Vim-Plug,无法解决我的问题。
相关配置代码
我的GDScript配置代码
-- General lsp setups for Language servers not supported by Mason. local lsp = require("lspconfig") lsp.godot.setup { cmd = { "nc", "localhost", "6008" }, filetypes = { "gd", "gdscript", "gdscript3" }, root_dir = function(startpath) return M.search_ancestors(startpath, matcher) end }
我的nvim-lspconfig配置文件(基于kickstart.nvim)
return { -- LSP Configuration & Plugins 'neovim/nvim-lspconfig', dependencies = { -- Automatically install LSPs to stdpath for neovim {'williamboman/mason.nvim', config = true}, 'williamboman/mason-lspconfig.nvim', -- Useful status updates for LSP -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})` { 'j-hui/fidget.nvim', tag = 'legacy', opts = {} }, -- Additional lua configuration, makes nvim stuff amazing! 'folke/neodev.nvim', }, config = function () local on_attach = function(_, bufnr) -- NOTE: Remember that lua is a real programming language, and as such it is possible -- to define small helper and utility functions so you don't have to repeat yourself -- many times. -- -- In this case, we create a function that lets us more easily define mappings specific -- for LSP related items. It sets the mode, buffer and description for us each time. local nmap = function(keys, func, desc) if desc then desc = 'LSP: ' .. desc end vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) end nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame') nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction') nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition') nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation') nmap('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition') nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') -- See `:help K` for why this keymap nmap('K', vim.lsp.buf.hover, 'Hover Documentation') nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation') -- Lesser used LSP functionality nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') nmap('<leader>wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') -- Create a command `:Format` local to the LSP buffer vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) vim.lsp.buf.format() end, { desc = 'Format current buffer with LSP' }) end end }
错误原因分析
你的GDScript配置里存在两个致命问题:
root_dir函数中使用了未定义的变量M和matcher,这会直接触发Lua的「未定义全局变量」错误,导致整个LSP配置加载失败,进而影响所有依赖LSP的自定义命令。- 若你的GDScript配置是单独的文件,没有正确集成到Lazy.nvim的nvim-lspconfig加载流程中,也会导致加载顺序错误引发模块问题。
修复步骤
1. 修正GDScript的root_dir配置
替换掉未定义的M.search_ancestors,可以选择以下两种方案:
方案A:使用nvim-lspconfig自带的根目录检测逻辑
nvim-lspconfig的godot LSP默认会识别包含project.godot或.git文件的目录作为项目根目录,显式调用该逻辑的代码如下:
local lsp = require("lspconfig") local util = require("lspconfig.util") lsp.godot.setup { cmd = { "nc", "localhost", "6008" }, filetypes = { "gd", "gdscript", "gdscript3" }, root_dir = util.root_pattern("project.godot", ".git") }
方案B:简化根目录逻辑
如果不需要复杂的根目录检测,直接返回当前工作目录:
local lsp = require("lspconfig") lsp.godot.setup { cmd = { "nc", "localhost", "6008" }, filetypes = { "gd", "gdscript", "gdscript3" }, root_dir = function() return vim.fn.getcwd() end }
2. 将GDScript配置整合到Lazy.nvim的nvim-lspconfig流程中
确保GDScript的配置在nvim-lspconfig的config函数内执行,保证加载顺序正确。修改后的完整配置如下:
return { -- LSP Configuration & Plugins 'neovim/nvim-lspconfig', dependencies = { {'williamboman/mason.nvim', config = true}, 'williamboman/mason-lspconfig.nvim', { 'j-hui/fidget.nvim', tag = 'legacy', opts = {} }, 'folke/neodev.nvim', }, config = function () -- 加载所需模块 local lsp = require("lspconfig") local util = require("lspconfig.util") -- GDScript LSP配置 lsp.godot.setup { cmd = { "nc", "localhost", "6008" }, filetypes = { "gd", "gdscript", "gdscript3" }, root_dir = util.root_pattern("project.godot", ".git"), on_attach = on_attach -- 复用全局的on_attach函数 } local on_attach = function(_, bufnr) local nmap = function(keys, func, desc) if desc then desc = 'LSP: ' .. desc end vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) end nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame') nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction') nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition') nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation') nmap('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition') nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') nmap('K', vim.lsp.buf.hover, 'Hover Documentation') nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation') nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') nmap('<leader>wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) vim.lsp.buf.format() end, { desc = 'Format current buffer with LSP' }) end -- 其他LSP配置可在此添加 end }
3. 验证修复效果
重启Neovim后,打开任意.gd文件,执行:LspInfo命令确认godot LSP已正常启动,同时测试自定义命令(如<leader>rn、:Format)是否可正常使用。
内容的提问来源于stack exchange,提问作者Yunky
相关产品推荐
相关产品推荐

