folke / lazydev.nvim

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

bug: Duplicate fields #13

Closed axdank closed 4 months ago

axdank commented 4 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

v0.10.0

Operating system/version

Linux EndeavourOS rolling x86_64

Describe the bug

There is a duplication of definitions, and seeing require(“lazydev.workspace”).global(), it is inserted inside Lua.workspace.library, the same path as the root of the lsp. One of the cases could be because I manage some folders inside my neovim configuration as a separate plugin with lazy.nvim, and that makes it take its own path again.

image image

For the moment I solved it by patching the add function inside lazydev.workspace, so that it checks if the path does not start with the same path as the lsp client root.

    local sadd = mod.add
    mod.add = function(self, path)
      local c = self:client()
      local realpath = vim.uv.fs_realpath(path)

      if c and realpath then
        local rd = c.root_dir

        if not vim.startswith(realpath, rd) then
          sadd(self, path)
        end
      end

Steps To Reproduce

--

Expected Behavior

Add within library only paths not contained within the lsp client's root_path

RayGuo-ergou commented 4 months ago

I have the same issue but mine happens when giving @class annotation.

pavkam commented 4 months ago

Same for me for ---@class and types in many cases. Maybe it's due to the files being required from multiple places in the code?

image image

folke commented 4 months ago

I just changed it so that the workspace folder itself is not added as a library. The issue you're seeing should not be possible with default lsp-config and lazydev.

Your project root should be the LSP root, and it's lua folder should be added to the workspace libraries. The top-level root folder won't be included because of the path and pathStrict settings.

folke commented 4 months ago

What does :LspInfo say is your lsp root? And what do you see when you enable lazydev opts.debug = true?

pavkam commented 4 months ago

 Client: lua_ls (id: 1, bufnr: [7, 86])
    filetypes:       lua
    autostart:       true
    root directory:  /Users/alex/.dotfiles/.config/nvim
    cmd:             /Users/alex/.local/share/nvim/mason/bin/lua-language-server
pavkam commented 4 months ago

My luals is configured as follows:

  lua_ls = {
                    settings = {
                        Lua = {
                            workspace = {
                                checkThirdParty = false,
                            },
                            codeLens = {
                                enable = true,
                            },
                            completion = {
                                callSnippet = 'Replace',
                            },
                            doc = {
                                privateName = { '^_' },
                            },
                            hint = {
                                enable = true,
                                setType = false,
                                paramType = true,
                                paramName = 'Disable',
                                semicolon = 'Disable',
                                arrayIndex = 'Disable',
                            },
                        },
                    },
                },
folke commented 4 months ago

That's all fine. What does lazydev debug show?

pavkam commented 4 months ago
## ~/.dotfiles/.config/nvim
- **runtime** `/opt/homebrew/Cellar/neovim/0.10.0/share/nvim/runtime/lua`
- `~/.local/share/nvim/lazy/luvit-meta/library`
- **nvim** `~/.config/nvim/lua`
- **conform.nvim** `~/.local/share/nvim/lazy/conform.nvim/lu
## [single]
- **runtime** `/opt/homebrew/Cellar/neovim/0.10.0/share/nvim/runtime/lua`
- `~/.local/share/nvim/lazy/luvit-meta/library`
folke commented 4 months ago

That's all correct. Do you still see the dups?

pavkam commented 4 months ago

image yeah, changing the dir to lua did not do anything (not that I was expecting it to)

RayGuo-ergou commented 4 months ago

Yeah and I still having this issue too (updated lazydev): image

 Client: lua_ls (id: 4, bufnr: [2, 3, 4])
    filetypes:       lua
    autostart:       true
    root directory:  /home/raydev/git/dotfiles/nvim
    cmd:             /home/raydev/.local/share/nvim/mason/bin/lua-language-server
    lua_ls = {
      settings = {
        Lua = {
          workspace = { checkThirdParty = false },
          telemetry = { enable = false },
          window = {
            progressBar = false,
          },
          hint = { enable = true },
        },
      },
    },
pavkam commented 4 months ago

Just checked that I have the latest version: "lazydev.nvim": { "branch": "main", "commit": "ae12a6224c466315f6ecbe6ed1ee7c5641f21d40" },

folke commented 4 months ago

and LuaLS also up to date?

folke commented 4 months ago

I can't reproduce this. Can you create an example file that should trigger this?

axdank commented 4 months ago

debug:

image

:lua = require("lazydev.workspace").global()

<1>{
  client_id = -1,
  library = { "/home/axdank/.local/share/bob/v0.10.0/nvim-linux64/share/nvim/runtime/lua" },
  root = "global",
  settings = {},
  <metatable> = <2>{
    GLOBAL = "global",
    SINGLE = "single",
    __index = <table 2>,
    add = <function 1>,
    client = <function 2>,
    debug = <function 3>,
    enabled = <function 4>,
    find = <function 5>,
    get = <function 6>,
    get_root = <function 7>,
    global = <function 8>,
    is_special = <function 9>,
    new = <function 10>,
    single = <function 11>,
    update = <function 12>,
    workspaces = {
      ["-1global"] = <table 1>,
      ["1/mnt/hdd/axdank/dotfiles/nvim"] = {
        client_id = 1,
        library = { "/home/axdank/.config/nvim/lua", "/home/axdank/.local/share/bob/v0.10.0/nvim-linux64/share/nvim/runtime/lua", "/home/axdank/.local/share/nvim/lazy/fzf-lua/lua", "/home/axdank/.local/share/nvim/lazy/mason.nvim/lua", "/home/axdank/.local/share/nvim/lazy/lazydev.nvim/lua" },
        root = "/mnt/hdd/axdank/dotfiles/nvim",
        settings = {
          Lua = {
            diagnostics = {
              globals = { "vim", "axcore" }
            },
            runtime = {
              path = { "?.lua", "?/init.lua" },
              pathStrict = true,
              version = "LuaJIT"
            },
            workspace = {
              checkThirdParty = false,
              library = { "/home/axdank/.local/share/bob/v0.10.0/nvim-linux64/share/nvim/runtime/lua", "/home/axdank/.config/nvim/lua", "/home/axdank/.local/share/nvim/lazy/fzf-lua/lua", "/home/axdank/.local/share/nvim/lazy/mason.nvim/lua", "/home/axdank/.local/share/nvim/lazy/lazydev.nvim/lua" }
            }
          }
        },
        <metatable> = <table 2>
      },
      ["1single"] = {
        client_id = 1,
        library = {},
        root = "single",
        settings = {
          Lua = {
            diagnostics = {
              globals = { "vim", "axcore" }
            },
            runtime = {
              path = { "?.lua", "?/init.lua" },
              pathStrict = true,
              version = "LuaJIT"
            },
            workspace = {
              checkThirdParty = false,
              library = { "/home/axdank/.local/share/bob/v0.10.0/nvim-linux64/share/nvim/runtime/lua" }
            }
          }
        },
        <metatable> = <table 2>
      }
    }
  }
}
pavkam commented 4 months ago

image

RayGuo-ergou commented 4 months ago

Actually I did clone LazyVim/LazyVim to try but surprisingly it works fine without any issue...

image

folke commented 4 months ago

I think I fixed it. Problem is symlinks vs real paths. Can you verify it works?

RayGuo-ergou commented 4 months ago

works for me! thanks my hero!

folke commented 4 months ago

Great :) I also use symlinks, but it worked for me because I typically edit in my ~/dot folder which is a real folder

axdank commented 4 months ago

now it works perfect, just my problem is that I use symlinks, thx @folke

pavkam commented 4 months ago

Thanks @folke, how do you have the stamina I will never understand :)

nanozuki commented 4 months ago

@folke I find another problem. I use nix home-manager. I edit file in ~/.config/home-manager, and nix will produce these files to /nix/store/xxxxxx.lua, and link files to ~/.config/nvim. When I edit files in ~/.config/home-manager, I also get the duplication definitaion warning.

图片 图片
folke commented 4 months ago

@nanozuki that's not possible to fix I think, since I would have to check all the lua files and dereference them to their real path. Not something that I'd do.

I'd also argue that duplicates like these are a LuaLS bug, since it should know it's the same file and hence not a dup.

Either way, can you paste the contents of :LazyDev? Just to make sure it's not something else

nanozuki commented 4 months ago

I understand this is a tricky situation. I accept any result and appreciate your work.

:LazyDev print this:

settings = {
  Lua = {
    diagnostics = {
      globals = { "vim" }
    },
    format = {
      enable = false
    },
    hint = {
      enable = true
    },
    runtime = {
      path = { "?.lua", "?/init.lua" },
      pathStrict = true,
      version = "LuaJIT"
    },
    telemetry = {
      enable = false
    },
    workspace = {
      checkThirdParty = false,
      library = { "/nix/store/hhmcp0p0y2kjgjgpxs4ygna3d04cadsh-neovim-unwrapped-0.10.0/share/nvim/runtime/lua", "/Users/crows/.local/share/nvim/lazy/luvit-meta/library", "/Users/crows/.config/home-manager/configs/nvim/lua", "/Users/crows/.config/nvim/lua" }
    }
  }
}

And { debug = ture } print this:

   Info  16:37:24 notify.info lazydev.nvim ## ~/.config/home-manager/configs/nvim
- **runtime** `/nix/store/hhmcp0p0y2kjgjgpxs4ygna3d04cadsh-neovim-unwrapped-0.10.0/share/nvim/runtime/lua`
- `~/.local/share/nvim/lazy/luvit-meta/library`
- **nvim** `~/.config/home-manager/configs/nvim/lua`
- **nvim** `~/.config/nvim/lua`
   Info  16:37:24 notify.info lazydev.nvim ## [single]
- **runtime** `/nix/store/hhmcp0p0y2kjgjgpxs4ygna3d04cadsh-neovim-unwrapped-0.10.0/share/nvim/runtime/lua`
- `~/.local/share/nvim/lazy/luvit-meta/library`
folke commented 4 months ago

What's the output of

:=vim.tbl_map(vim.uv.fs_realpath, {"/Users/crows/.config/home-manager/configs/nvim/lua", "/Users/crows/.config/nvim/lua"})

nanozuki commented 4 months ago

{ "/Users/crows/.config/home-manager/configs/nvim/lua", "/Users/crows/.config/nvim/lua" }

These files are not linked.

nanozuki commented 4 months ago

And my neodev.conf's opt works fine. Just in case these will help.

{
  override = function(root_dir, options)
    if root_dir:find('config.*nvim') then
      -- enable plugins in config managers
      options.plugins = true
    end
  end,
}
folke commented 4 months ago

In that case there's little I can do (not linked).

nanozuki commented 4 months ago

Fine, thanks for your time.

I wonder why the path .config/nvim was added to "workspace.library". I didn't open any files in it.

folke commented 4 months ago

Because you require a file from your neovim config in the files you have open. lazydev then resolves that module with vim.loader to the path in your ~/.config/nvim folder

nanozuki commented 4 months ago

Is it possible to resolve the module in the current workspace first? The file I required exists in the current workspace.

In the screenshot, is .config/home-manager/configs/nvim/lua/config/configs.lua requries .config/home-manager/configs/nvim/lua/config/globals.lua. (root dir for LuaLS is .config/home-manager/configs/nvim)

After the manager produce the files, it's .config/nvim/lua/config/configs.lua requires .config/nvim/lua/config/globals.lua.

nanozuki commented 4 months ago

@folke I read the code of "lazydev," and I think we can add workspace modules when the lua ls attach to the buffer ("M.on_attach"). Do you have interested or suggestion in this? I can make a PR for this.

folke commented 4 months ago

@nanozuki can you check the new version?

nanozuki commented 4 months ago

@folke Awesome! All "duplicated" warnings are gone. Thank you, it helps a lot.

nanozuki commented 4 months ago

This version also fixed the #17, btw.

folke commented 4 months ago

great!

I also just pushed a bunch of new changes, including a lspconfig integration to fix its workspace management. There's also an API call for people that don't use lspconfig to properly deal with existing workspaces.