Error when lazy loading nvim-cmp on InsertEnter event
Hi, I'm not sure whether this is a bug or if it's a mistake in my configuration, but upon adding event = InsertEnter to nvim-cmp's plugin spec, I get the following error: https://pastebin.com/CXgmACiv.
The weird thing is that this error seemingly comes up randomly each time I restart Neovim. Sometimes it gives the error, sometimes everything works perfectly with the lazy loading.
Here's my nvim-cmp spec: https://github.com/WilsonOh/nvim/blob/lazy/lua/core/plugins/cmp.lua.
May I know how I should go about debugging this? Thank you!
Can you provide a minimal init as instructed in the issue template? Otherwise it's hard for me to debug this
I had a similar error, seemingly arising from cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()), whose traceback is like what you get
Error 07:54:53 PM notify.error lazy.nvim Failed to run
configfor nvim-autopairs
The reason is that I did not have pcalls to ensure that nvim-autopairs is installed before calling it in the configuration for nvim-cmp; in general nvim-cmp, nvim-lsp and nvim-autopairs call one another to have the completion play together with the parentheses, so you may want to check that you have pcalls in all your files to gracefully handle it (and avoid circular dependencies). Not sure if this is your problem too, but worth a suggestion.
Can you provide a minimal init as instructed in the issue template? Otherwise it's hard for me to debug this
Sorry about that! I tried to create a minimal init like this: https://pastebin.com/0uHyUEh0 but I couldn't reproduce the error.
However as @gennaro-tedesco's has suggested, the error came from this this snippet in my nvim-autopairs setup:
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
local cmp = require("cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
commenting out that snippet from my config resulted in the error going away. I tried wrapping the requires in pcalls as suggested like so:
local ok0, npairs = pcall(require, "nvim-autopairs")
if not ok0 then
return
end
npairs.setup({})
local ok1, cmp_autopairs = pcall(require, "nvim-autopairs.completion.cmp")
if not ok1 then
return
end
local ok2, cmp = pcall(require, "cmp")
if not ok2 then
return
end
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
but that didn't solve the issue. Maybe I'm doing it incorrectly? The first line of error says:
Error 11:11:35 PM msg_show.lua_error Error detected while processing InsertEnter Autocommands for "*":
11:11:35 PM msg_show Error executing lua callback: ...l/share/nvim/lazy/cmp-nvim-lsp/lua/cmp_nvim_lsp/init.lua:106: attempt to call field 'register_source' (a nil value)
stack traceback:
...l/share/nvim/lazy/cmp-nvim-lsp/lua/cmp_nvim_lsp/init.lua:106: in function <...l/share/nvim/lazy/cmp-nvim-lsp/lua/cmp_nvim_lsp/init.lua:95>
cmp-nvim-lsp is listed as a dependency in both nvim-lspconfig and nvim-cmp's specs so could that be the issue? But that's the same as well in the minimal init so I'm out of ideas.
Can you remove all the deps instead and let lazy handle them automatically? So just move the deps top-level and set lazy=true. Lazy should then automatically resolve lua dependencies.
Looking at the error there does indeed seem some sort of circular requires happening though, so not 100% sure this would solve it.
Can you remove all the deps instead and let lazy handle them automatically? So just move the deps top-level and set
lazy=true. Lazy should then automatically resolve lua dependencies.Looking at the error there does indeed seem some sort of circular requires happening though, so not 100% sure this would solve it.
Do you mean something like this?
from
local M = {
"hrsh7th/nvim-cmp",
event = "InsertEnter",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-emoji",
"hrsh7th/cmp-cmdline",
"dmitmel/cmp-cmdline-history",
"hrsh7th/cmp-path",
"saadparwaiz1/cmp_luasnip",
},
}
and
local M = {
"neovim/nvim-lspconfig",
name = "lsp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"simrat39/rust-tools.nvim",
"p00f/clangd_extensions.nvim",
"mfussenegger/nvim-jdtls",
"b0o/schemastore.nvim",
{ "williamboman/mason.nvim", commit = "3ccd16" },
"williamboman/mason-lspconfig.nvim",
"jayp0521/mason-nvim-dap.nvim",
},
}
to
{ "hrsh7th/cmp-nvim-lsp", lazy = true },
{ "simrat39/rust-tools.nvim", lazy = true },
{ "p00f/clangd_extensions.nvim", lazy = true },
{ "mfussenegger/nvim-jdtls", lazy = true },
{ "b0o/schemastore.nvim", lazy = true },
{ "williamboman/mason-lspconfig.nvim", lazy = true },
{ "jayp0521/mason-nvim-dap.nvim", lazy = true },
{ "williamboman/mason.nvim", commit = "3ccd16", lazy = true },
{ "hrsh7th/cmp-nvim-lsp", lazy = true },
{ "hrsh7th/cmp-buffer", lazy = true },
{ "hrsh7th/cmp-emoji", lazy = true },
{ "hrsh7th/cmp-cmdline", lazy = true },
{ "dmitmel/cmp-cmdline-history", lazy = true },
{ "hrsh7th/cmp-path", lazy = true },
{ "saadparwaiz1/cmp_luasnip", lazy = true },
I'm still getting the error
ï™™ Error 12:02:59 AM msg_show.lua_error Error detected while processing InsertEnter Autocommands for "*":
12:02:59 AM msg_show Error executing lua callback: ...l/share/nvim/lazy/cmp-nvim-lsp/lua/cmp_nvim_lsp/init.lua:106: attempt to call field 'register_source' (a nil value)
stack traceback:
...l/share/nvim/lazy/cmp-nvim-lsp/lua/cmp_nvim_lsp/init.lua:106: in function <...l/share/nvim/lazy/cmp-nvim-lsp/lua/cmp_nvim_lsp/init.lua:95>
ï™™ Error 12:02:59 AM notify.error lazy.nvim Failed to run `config` for nvim-cmp
/home/wilsonoh/.config/nvim/lua/core/plugins/cmp.lua:42: attempt to index field 'PreselectMode' (a nil value)
# stacktrace:
- cmp.lua:42
nvim-cmp and nvim-autopairs are both lazy-loaded on InsertEnter
Is it necessary to indicate "hrsh7th/cmp-nvim-lsp", as dependency in both neovim/nvim-lspconfig and hrsh7th/nvim-cmp? Just have it as dependency of the latter. In my case I do
{
"neovim/nvim-lspconfig",
event = "BufReadPre",
config = ...
},
{
"hrsh7th/nvim-cmp",
event = "InsertEnter",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline",
"saadparwaiz1/cmp_luasnip",
{
"windwp/nvim-autopairs",
config =...
},
},
},
where the lsp configuration contains
local cmq_ok, _ = pcall(require, "cmp_nvim_lsp")
if not cmq_ok then
return
end
and cmp configuration, in turn
local pairs_ok, cmp_autopairs = pcall(require, "nvim-autopairs.completion.cmp")
if not pairs_ok then
return
end
and it works. However as folke suggested I could probably get rid of the pcalls altogether (I am also still in the phase of understanding the mechanics of this plugin so don't take this as golden truth).
Basically, with lazy Lua dependencies are not needed.
Ok so it seems like moving this
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
local cmp = require("cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
from plugins/autopairs.lua to plugins/cmp.lua did the trick. I didn't even have to add nvim-autopairs as a dependency on nvim-cmp...
I guess now I have to look into removing dependencies and letting lazy handle it all.
Thanks @gennaro-tedesco and @folke!