folke / lazydev.nvim

Faster LuaLS setup for Neovim
Apache License 2.0
711 stars 7 forks source link

bug: Error executing vim.schedule lua callback: .../.local/share/nvim/lazy/lazydev.nvim/lua/lazydev/buf.lua:151: bad argument #1 to 'ipairs' (table expected, got nil) #10

Closed yavorski closed 5 months ago

yavorski commented 5 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.10

Operating system/version

Arch Linux 6.9.3-arch1-1

Describe the bug

The plugin throws error when starting with nvim ~/.config/nvim/init.lua, but it works correctly when I am editing the init.lua in my repository directory. I think it throws when there isn't a lua directory e.g. ~/.config/nvim/lua/, e.g. when you have only single init.lua file there. Here is the error.

Error executing vim.schedule lua callback: .../.local/share/nvim/lazy/lazydev.nvim/lua/lazydev/buf.lua:151: bad argument #1 to 'ipairs' (table expected, got nil)
stack traceback:
    [C]: in function 'ipairs'
    .../.local/share/nvim/lazy/lazydev.nvim/lua/lazydev/buf.lua:151: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

Steps To Reproduce

  1. delete lua dir if any rm -r ~/.config/nvim/lua/
  2. start nvim with nvim ~/.config/nvim/init.lua

Expected Behavior

To not throw error and to work correctly.

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  "folke/lazydev.nvim",
  -- add any other plugins here
  {
    "folke/lazydev.nvim",
    dependencies = {{ "bilal2453/luvit-meta" }},
    ft = "lua",
    opts = {
      library = { "luvit-meta/library" }
    }
  }
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
-- nothing else
folke commented 5 months ago

Should be fixed, but would be great if you can confirm!

yavorski commented 5 months ago

It does not throw, but it is not working either... Also it is printing directories now. It works when I create the ~/.config/nvim/lua/ dir manually. See screenshots.

screenshot-02-06-2024-23-56-25

screenshot-02-06-2024-23-55-11

folke commented 5 months ago

Just removed that debug. Now I understand what you mean with init.lua. Will check

folke commented 5 months ago

What does :=vim.lsp.buf.list_workspace_folders() show?

folke commented 5 months ago

I think this is the problem: https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/server_configurations/lua_ls.lua#L22

Your LuaLS is in single file mode, no workspace.

When you create the lua folder, you're in a workspace.

folke commented 5 months ago

Although, even in that case it still works for me....

yavorski commented 5 months ago

What does :=vim.lsp.buf.list_workspace_folders() show?

Empty table {} I have added workaround to my config now to create the dir if it does not exists, so it's not a big deal.

Lazy.use {
  "folke/lazydev.nvim",
  -- dir = "~/dev/open-sos/lazydev.nvim",
  dependencies = {{ "bilal2453/luvit-meta" }},
  ft = "lua",
  opts = {
    library = { "luvit-meta/library" }
  },
  init = function()
    local luadir = vim.fn.expand("~/.config/nvim/lua/")
    if not vim.loop.fs_stat(luadir) then
      vim.fn.mkdir(luadir)
    end
  end
}

This is my lua_ls setup if it does matter

require("lspconfig").lua_ls.setup({
  capabilities = LSP.capabilities(),
  settings = {
    Lua = {
      format = { enable = true },
      runtime = { version = "LuaJIT" },
      diagnostics = { globals = { "vim" } },
      completion = { callSnippet = "Replace" },
      workspace = { checkThirdParty = "Disable" }
    }
  }
})
folke commented 5 months ago

And this? :=vim.lsp.get_clients({name = "lua_ls"})[1].root_dir

yavorski commented 5 months ago

nil,

screenshot-03-06-2024-01-08-18

folke commented 5 months ago

Should work now with single file support

yavorski commented 5 months ago

It is working now. Thank you!