nvimtools / none-ls.nvim

null-ls.nvim reloaded / Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash upon making diagnostics with column index out of range

XanX3601 opened this issue · comments

FAQ

  • I have checked the FAQ and it didn't resolve my problem.

Issues

  • I have checked existing issues and there are no issues with the same problem.

Neovim Version

0.9.4

Dev Version?

  • I am using a stable Neovim release version, or if I am using a dev version of Neovim I have confirmed that my issue is reproducible on a stable version.

Operating System

Macos

Minimal Config

-- this template is borrowed from nvim-lspconfig
local on_windows = vim.loop.os_uname().version:match("Windows")

local function join_paths(...)
    local path_sep = on_windows and "\\" or "/"
    local result = table.concat({ ... }, path_sep)
    return result
end

vim.g.loaded_remote_plugins = ""
vim.cmd([[set runtimepath=$VIMRUNTIME]])

local temp_dir = vim.loop.os_getenv("TEMP") or "/tmp"

vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))

local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")

local null_ls_config = function()
    local null_ls = require("null-ls")
    -- add only what you need to reproduce your issue
    null_ls.setup({
        sources = {null_ls.builtins.diagnostics.ruff}
    })
end

local function load_plugins()
    -- only add other plugins if they are necessary to reproduce the issue
    require("packer").startup({
        {
            "wbthomason/packer.nvim",
            {
                "nvimtools/none-ls.nvim",
                requires = { "nvim-lua/plenary.nvim" },
                config = null_ls_config,
            },
        },
        config = {
            package_root = package_root,
            compile_path = compile_path,
        },
    })
end

if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
    load_plugins()
    require("packer").sync()
else
    load_plugins()
    require("packer").sync()
end

Steps to Reproduce

  1. Open an empty python file with neovim using the minimal_init.lua provided:
nvim --clean -u minimal_init.lua a.py
  1. Write a syntax error like the following one:
if
  1. Exit insert mode

The crash should occur at step 3
The message can be seen using the neovim command :messages

Reproducibility Check

  • I confirm that my minimal config is based on the minimal_init.lua template and that my issue is reproducible by running nvim --clean -u minimal_init.lua and following the steps above.

Expected Behavior

No crash and display syntax error

Actual Behavior

Crash of none-ls and it does not restart until neovim is restarted.
It will keep crashing till the syntax error is fine

Debug Log

[WARN Fri Dec 22 19:08:49 2023] /tmp/nvim/site/pack/packer/start/none-ls.nvim/lua/null-ls/generators.lua:94: failed to run generator: ...r/start/none-ls.nvim/lua/null-ls/helpers/diagnostics.lua:71: index out of range

Help

No

Implementation Help

No response

Requirements

  • I have read and followed the instructions above and understand that my issue will be closed if I did not provide the required information.

I tried a few things to fix the error and figure out that, on file lua/null-ls/helpers/diagnostics.lua, at line 72, the function vim.str_byteindex is used to get something.
By printing the arguments given to this function, I saw that it could be given a string of length $n$ and an index $n+1$ (depending on the string).

By applying the following code:

local col = tonumber(entries["col"])
col = math.min(col, string.len(content_line))
local byte_index_col = vim.str_byteindex(content_line, col)

I obtain the desire behavior. Meaning that there is no crash and the syntax error is displayed correctly.

I don't know if it is the right strategy to resolve this bug or if the problem comes from somewhere else in the code but this temp fix is enough for me to get the desire behaviour.

I'd prefer not to open a PR by myself as it looks like it is a quick fix and I do not have a dev env installed to test it.

I will try to answer questions if any

Does this happen with providers other than ruff? I can't recreate this with my current set up. (I don't use ruff).

I just tried using flake8 and the issue is the same. I just changed ruff by flake8 in the minimal_init.lua

This is happening with vint as well

I'll see if I can dedicate some time to this over the next few days.

[null-ls] failed to run generator: ...im/lazy/none-ls.nvim/lua/null-ls/helpers/diagnostics.lua:71: index out of range

Facing the same issue with similar setup

[null-ls] failed to run generator: ...im/lazy/none-ls.nvim/lua/null-ls/helpers/diagnostics.lua:71: index out of range

Facing the same issue with similar setup

See my temporary fix above if you need in the meantime.

Picked #51 (comment). Please report any further crashes here if it still occurs.

I tried a few things to fix the error and figure out that, on file lua/null-ls/helpers/diagnostics.lua, at line 72, the function vim.str_byteindex is used to get something. By printing the arguments given to this function, I saw that it could be given a string of length n and an index n+1 (depending on the string).

By applying the following code:

local col = tonumber(entries["col"])
col = math.min(col, string.len(content_line))
local byte_index_col = vim.str_byteindex(content_line, col)

I obtain the desire behavior. Meaning that there is no crash and the syntax error is displayed correctly.

I don't know if it is the right strategy to resolve this bug or if the problem comes from somewhere else in the code but this temp fix is enough for me to get the desire behaviour.

I'd prefer not to open a PR by myself as it looks like it is a quick fix and I do not have a dev env installed to test it.

I will try to answer questions if any

This also fixed the issue for me. Why don’t you open a PR for this? As it still seem to be there in newer versions

I already applied that fix a while ago, so this might be a culprit elsewhere.

I confirm what @mochaaP just said, they commited the fix I suggested and, once I updated the package through :PackerSync, the crash did not occur anymore.
Easy way to test it to reproduce the steps with a clean config as described in the issue.

I did not say thank you @mochaaP at the time so thank you for fixing the crash 😄