nvimtools / none-ls.nvim

null-ls.nvim reloaded / Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.
The Unlicense
2.43k stars 72 forks source link

Crash upon making diagnostics with column index out of range #51

Closed XanX3601 closed 8 months ago

XanX3601 commented 8 months ago

FAQ

Issues

Neovim Version

0.9.4

Dev 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

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

XanX3601 commented 8 months ago

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

jakenvac commented 8 months ago

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

XanX3601 commented 8 months ago

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

bartels commented 8 months ago

This is happening with vint as well

jakenvac commented 8 months ago

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

mrlkn commented 8 months ago

[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

XanX3601 commented 8 months ago

[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.

mochaaP commented 8 months ago

Picked https://github.com/nvimtools/none-ls.nvim/issues/51#issuecomment-1867956063. Please report any further crashes here if it still occurs.

moha-abdi commented 7 months ago

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

mochaaP commented 7 months ago

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

XanX3601 commented 7 months ago

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 😄