Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Nvim's runtime #29

Closed
lewis6991 opened this issue Feb 5, 2025 · 19 comments
Closed

Support for Nvim's runtime #29

lewis6991 opened this issue Feb 5, 2025 · 19 comments
Labels
bug Something isn't working

Comments

@lewis6991
Copy link

As mentioned in #28, to load Neovim's runtime into LuaLS, the following is used:

  "workspace": {
    "library": [
      "$VIMRUNTIME",
    ],
  },

However, since env vars do not work yet, I can use a hardcoded path:

  "workspace": {
    "library": [
      "/usr/local/share/nvim/runtime",
    ],
  },

This appears to sort of work, however the main object provided by the Neovim runtime, the vim global, isn't being properly loaded.

In LuaLS:

Image

However in Emmylua_ls:

Image

It also seems to be stuck diagnosing files:

Image

Not sure if that is related.

@CppCXY
Copy link
Owner

CppCXY commented Feb 5, 2025

Is there any code bade and relevant configuration available?

@CppCXY
Copy link
Owner

CppCXY commented Feb 5, 2025

Image

It seems that the result of this expression cannot be parsed.

@lewis6991
Copy link
Author

I'm not sure what kind of configuration you need, but you could try this project https://github.com/lewis6991/gitsigns.nvim which is clean in LuaLS and contains a .luarc.json.

If using LSP through Neovim, you can use:

init.lua:

vim.lsp.config('luals', {
  cmd = { 'lua-language-server' },
  filetypes = { 'lua' },
  root_markers = { '.luarc.json' }
})

vim.lsp.config('emmylua_ls', {
  cmd = { 'emmylua_ls' },
  filetypes = { 'lua' },
  root_markers = { '.luarc.json' }
})

-- Select which to enable
-- vim.lsp.enable('luals')
vim.lsp.enable('emmylua_ls')
cd gitsigns.nvim
[create init.lua]
nvim -u init.lua lua/gitsigns.lua

If you have Neovim installed, you can get the value $VIMRUNTIME with:

nvim --headless -c 'echo $VIMRUNTIME' -c q

@lewis6991
Copy link
Author

I've just tried 0.4.6 and this is still a problem. The vim global is unknown.

@lewis6991
Copy link
Author

Note from the log:

[2025-02-07 12:02:06 +00:00 INFO emmylua_ls::handlers::initialized] add workspace root: "/Users/lewrus01/projects/gitsigns.nvim"
[2025-02-07 12:02:06 +00:00 INFO emmylua_ls::handlers::initialized] add library: "/Users/lewrus01/projects/gitsigns.nvim/lua"
[2025-02-07 12:02:06 +00:00 INFO emmylua_ls::handlers::initialized] add library: "/usr/local/share/nvim/runtime"

So the correct library path is being added.

@CppCXY CppCXY reopened this Feb 7, 2025
@CppCXY
Copy link
Owner

CppCXY commented Feb 7, 2025

From the perspective of code analysis, this is a recursive definition of the vim variable. For Lua, I cannot assume that vim has only this one definition; it might have a real definition elsewhere. Since I cannot analyze the code based on loading order or some assumed sequence (the code is actually analyzed out of order), I should assume that the pattern X = X or {} is equivalent to X = {}.

@CppCXY CppCXY added the bug Something isn't working label Feb 7, 2025
@ycycyyc
Copy link
Contributor

ycycyyc commented Feb 7, 2025

It works well for me, but I must use a hardcoded path /usr/local/nvim-main/share/nvim/runtime
Image

If the following is used

"workspace": {
    "library": [
      "$VIMRUNTIME",
    ],
  },

the library path is wrong:

[2025-02-07 21:24:53 +08:00 INFO emmylua_ls::handlers::initialized] add library: "/data/yc/.local/share/nvim/lazy_178550383/gitsigns.nvim//usr/local/nvim-main/share/nvim/runtime"

@lewis6991
Copy link
Author

Are you using nvim 0.11 or an older release. The issue appears to be how vim is defined.

@ycycyyc
Copy link
Contributor

ycycyyc commented Feb 7, 2025

version:
NVIM v0.11.0-dev-1706+g5371659524

--- `emmylua_ls`
vim.lsp.config("emmylua_ls", {
  cmd = { "emmylua_ls" },
  filetypes = { "lua" },
  root_markers = { ".luarc.json" },
})

vim.lsp.enable "emmylua_ls"

@CppCXY
Copy link
Owner

CppCXY commented Feb 7, 2025

I have specifically addressed this part of the inference, and you can either compile it from the source code or wait for my next release.

@ycycyyc
Copy link
Contributor

ycycyyc commented Feb 7, 2025

I found another problem. After nvim exits, emmy lua_ls is still running.

@CppCXY
Copy link
Owner

CppCXY commented Feb 7, 2025

I found another problem. After nvim exits, emmy lua_ls is still running.

Isn't the language server process a child process of nvim? If the parent process exits, shouldn't the child process be terminated directly? Additionally, I have also handled the shutdown message. Let's examine the exchanged protocol with the debug log level enabled.

@ycycyyc
Copy link
Contributor

ycycyyc commented Feb 7, 2025

emmylua_ls indeed is a child process of nvim.

debug log info:

Image

But emmylua_ls is still running.

yc       32417     1  2 22:22 ?        00:00:01 emmylua_ls --log-level debug

@CppCXY
Copy link
Owner

CppCXY commented Feb 7, 2025

emmylua_ls indeed is a child process of nvim.

debug log info:

Image But `emmylua_ls` is still running.
yc       32417     1  2 22:22 ?        00:00:01 emmylua_ls --log-level debug

My code directly uses the LSP server example from the rust-analyzer library, and I can't figure out why.

@lewis6991
Copy link
Author

I just compiled from source and this is still an issue.

[2025-02-07 15:32:59 +00:00 DEBUG lsp_server::stdio] sending message Request(
    Request {
        id: RequestId(
            I32(
                87,
            ),
        ),
        method: "textDocument/hover",
        params: Object {
            "position": Object {
                "character": Number(12),
                "line": Number(13),
            },
            "textDocument": Object {
                "uri": String("file:///Users/lewrus01/projects/gitsigns.nvim/lua/gitsigns/actions.lua"),
            },
        },
    },
)
[2025-02-07 15:32:59 +00:00 DEBUG lsp_server::msg] > {"jsonrpc":"2.0","id":87,"result":{"contents":[{"language":"lua","value":"(global) vim: unknown"}],"range":{"end":{"character":15,"line":13},"start":{"character":12,"line":13}}}}

@CppCXY
Copy link
Owner

CppCXY commented Feb 8, 2025

It work well, on my vscode

Image

Additionally, the require function in emmylua is strict mode and will only start requiring from the relevant root directory.

except add .emmyrc.json

Image

@lewis6991
Copy link
Author

lewis6991 commented Feb 10, 2025

Ok I've got it to work now. The problem was due to an extra directory I had that is created to run LuaLS.

If you run make luals and try again you should get vim: unknown. The make command will create a deps/lua-language-server-3.13.6-Darwin-arm64 which contains all of LuaLS. This confuses emmylua for some reason.


Side note: even with vim being reognised, it doesn't seem to know the type of vim.F which is just a Lua module that exports functions.

@CppCXY
Copy link
Owner

CppCXY commented Feb 10, 2025

Ok I've got it to work now. The problem was due to an extra directory I had that is created to run LuaLS.

If you run make luals and try again you should get vim: unknown. The make command will create a deps/lua-language-server-3.13.6-Darwin-arm64 which contains all of LuaLS. This confuses emmylua for some reason.

Side note: even with vim being reognised, it doesn't seem to know the type of vim.F which is just a Lua module that exports functions.

You can recompile from the source code again. Previously, I configured require in strict mode by default, which means it must be calculated from the relevant root directory.

@lewis6991
Copy link
Author

lewis6991 commented Feb 10, 2025

There are no updates. My latest commit is:

* b545b40 CppCXY             2025-02-10  (HEAD -> main, origin/main, origin/HEAD) Improve table infer

EDIT:

I removed "requirePath": false from the .emmyrc.json which you suggested to include, and it works.

@CppCXY CppCXY closed this as completed Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants