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

Error in Neovim: “attempt to compare string with number” #609

Closed
klmr opened this issue Apr 1, 2023 · 10 comments · Fixed by #614
Closed

Error in Neovim: “attempt to compare string with number” #609

klmr opened this issue Apr 1, 2023 · 10 comments · Fixed by #614

Comments

@klmr
Copy link

klmr commented Apr 1, 2023

Based on the error stack traceback, this is related to #481. Unfortunately #565 did not fix it, and the error persists in the current HEAD revision (adc2189).

To reproduce the issue, create a file containing the following and open it in Neovim with LSP configured to use ‘languageserver’:

a =
b = 2

Here’s the full error message:

Error executing vim.schedule lua callback: /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:431: attempt to compare string with number
stack traceback:
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:431: in function 'add'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:468: in function 'get_diagnostics'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:1162: in function 'show'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:724: in function 'set'
        /opt/local/share/nvim/runtime/lua/vim/lsp/diagnostic.lua:206: in function 'handler'
        /opt/local/share/nvim/runtime/lua/vim/lsp.lua:1049: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
@klmr klmr changed the title Error in NeoVim: “attempt to compare string with number” Error in Neovim: “attempt to compare string with number” Apr 1, 2023
@randy3k
Copy link
Member

randy3k commented Apr 2, 2023

It sounds like an issue of lintr reporting the row number and column number: https://github.com/REditorSupport/languageserver/blob/master/R/diagnostics.R#L17

We may need to check if the numbers are NAs?

@klmr
Copy link
Author

klmr commented Apr 2, 2023

Are you able to reproduce the issue? Otherwise I am happy to patch my local ‘languageserver’ version with instrumentation code. Unfortunately I know absolutely nothing about the Lua/client side of things.

@randy3k
Copy link
Member

randy3k commented Apr 3, 2023

It has been a while since I used neovim and it seems that neovim supports LSP officially. Could you share how you configure neovim to setup R languageserver?

@klmr
Copy link
Author

klmr commented Apr 3, 2023

Here’s a minimal configuration (the config is macOS but on a Linux system using the path ~/.config/nvim instead of ~/Library/Preferences/nvim should have the sam effect):

cat ~/Library/Preferences/nvim/init.lua
vim.opt.runtimepath:append('~/Library/Preferences/nvim/mason.nvim')
vim.opt.runtimepath:append('~/Library/Preferences/nvim/nvim-lspconfig')

require('mason').setup()
require('lspconfig').r_language_server.setup{}

Afterwards, do the following inside the nvim config path to initialise the LSP support plugins:

git clone --depth 1 git@github.com:williamboman/mason.nvim.git
git clone --depth 1 git@github.com:neovim/nvim-lspconfig.git

nvim +:MasonInstall\ r-languageserver

And restart Neovim afterwards.

… in theory these plugins are not necessary but in practice the manual nvim LSP configuration is so complicated that it is recommended to use these plugins.

@klmr
Copy link
Author

klmr commented Apr 10, 2023

Hi, I went ahead and locally modified diagnostic_range to include logging the return value (via dput). And, indeed, for the snippet above I am getting the following values after this line:

cols <- code_point_to_unit(text, c(result$ranges[[1]][1] - 1, result$ranges[[1]][2]))

result == structure(
  list(
    filename = "/path/to/test.r",
    line_number = 2L,
    column_number = 0L,
    type = "style",
    message = "Indentation should be 4 spaces but is 0 spaces.",
    line = "b = 2",
    ranges = list(c(0L, 4L)),
    linter = "indentation_linter"
  ),
  class = c("lint", "list")
)

text == "b = 2"

cols == c(4, NA)

So there’s a NA length, just as you suspected. Does this help?

@milanglacier
Copy link

Here’s a minimal configuration (the config is macOS but on a Linux system using the path ~/.config/nvim instead of ~/Library/Preferences/nvim should have the sam effect):

cat ~/Library/Preferences/nvim/init.lua
vim.opt.runtimepath:append('~/Library/Preferences/nvim/mason.nvim')
vim.opt.runtimepath:append('~/Library/Preferences/nvim/nvim-lspconfig')

require('mason').setup()
require('lspconfig').r_language_server.setup{}

Afterwards, do the following inside the nvim config path to initialise the LSP support plugins:

git clone --depth 1 git@github.com:williamboman/mason.nvim.git
git clone --depth 1 git@github.com:neovim/nvim-lspconfig.git

nvim +:MasonInstall\ r-languageserver

And restart Neovim afterwards.

… in theory these plugins are not necessary but in practice the manual nvim LSP configuration is so complicated that it is recommended to use these plugins.

If languageserver package (install.packages("languageserver") is installed in the R in the PATH, this config should be enough:

vim.opt.runtimepath:append('~/Library/Preferences/nvim/nvim-lspconfig')

require('lspconfig').r_language_server.setup{}

remember git clone --depth 1 git@github.com:neovim/nvim-lspconfig.git.

There's no need to use mason to install r-language-server

@milanglacier
Copy link

milanglacier commented Apr 18, 2023

with neovim 0.9, I can't reproduce the problem. I guess maybe neovim 0.9 has improved the robustness of error handling.

In fact if you go to the source code of in the latest commit of neovim the function add in the top level of the traceback has been moved to line 396.

stack traceback:
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:431: in function 'add'

@klmr
Copy link
Author

klmr commented Apr 20, 2023

@milanglacier I am running nvim v0.9.0 and I can still reproduce this bug. The line numbers in the stack trace did change slightly, but the line number now is not 396, it’s 406:

Error executing vim.schedule lua callback: /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:406: attempt to compare string with number
stack traceback:
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:406: in function 'add'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:422: in function 'add_all_diags'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:443: in function 'get_diagnostics'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:1191: in function 'show'
        /opt/local/share/nvim/runtime/lua/vim/diagnostic.lua:703: in function 'set'
        /opt/local/share/nvim/runtime/lua/vim/lsp/diagnostic.lua:236: in function 'handler'
        /opt/local/share/nvim/runtime/lua/vim/lsp.lua:1057: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

randy3k added a commit that referenced this issue Apr 20, 2023
@randy3k
Copy link
Member

randy3k commented Apr 20, 2023

@klmr Could you check if #614 fixes the issue?

remotes::install_github("REditorSupport/languageserver#614")

@chunyunma
Copy link

Sadly, the same error occurred for me today, even after applying the #614 patch.
neovim version = 0.9.1
The error would only appear when I am working .Rmd files, but not with .R file type.

chunyunma added a commit to chunyunma/dotfiles-nvim that referenced this issue Sep 6, 2023
Replaced `nvim-lsp-installer` with `mason`.
Try to clean up `lsp` config.
Disabled `r_language_server` for now until an error gets fixed.
<REditorSupport/languageserver#609>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants