jose-elias-alvarez / null-ls.nvim

Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.
Other
3.64k stars 791 forks source link

Eslint produces `eslint: failed to decode json: Expected value but found invalid token at character 1` in some tsx projects #991

Closed benediktms closed 2 years ago

benediktms commented 2 years ago

FAQ

Issues

Neovim Version

0.7.2

Operating System

Manjaro Linux

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.cmd([[set runtimepath=$VIMRUNTIME]])

local temp_dir
if on_windows then
  temp_dir = vim.loop.os_getenv("TEMP")
else
  temp_dir = "/tmp"
end

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 async_formatting = function(bufnr)
  bufnr = bufnr or vim.api.nvim_get_current_buf()

  vim.lsp.buf_request(
    bufnr,
    "textDocument/formatting",
    vim.lsp.util.make_formatting_params({}),
    function(err, res, ctx)
      if err then
        local err_msg = type(err) == "string" and err or err.message
        -- you can modify the log message / level (or ignore it completely)
        vim.notify("formatting: " .. err_msg, vim.log.levels.WARN)
        return
      end

      -- don't apply results if buffer is unloaded or has been modified
      if not vim.api.nvim_buf_is_loaded(bufnr) or vim.api.nvim_buf_get_option(bufnr, "modified") then
        return
      end

      if res then
        local client = vim.lsp.get_client_by_id(ctx.client_id)
        vim.lsp.util.apply_text_edits(res, bufnr, client and client.offset_encoding or "utf-16")
        vim.api.nvim_buf_call(bufnr, function()
          vim.cmd("silent noautocmd update")
        end)
      end
    end
  )
end

local null_ls_config = function()
  local null_ls = require("null-ls")
  local formatting = null_ls.builtins.formatting
  local diagnostics = null_ls.builtins.diagnostics
  local code_actions = null_ls.builtins.code_actions
  local augroup = vim.api.nvim_create_augroup("LspFormatting", {})

  -- add only what you need to reproduce your issue
  null_ls.setup({
    sources = {
      formatting.prettier.with({
        extra_filetypes = { "toml" },
      }),
      code_actions.eslint,
      diagnostics.eslint,

      formatting.stylua,
      diagnostics.luacheck,
      diagnostics.luacheck,

      formatting.rustfmt,
    },
    debug = true,
    on_attach = function(client, bufnr)
      if client.supports_method("textDocument/formatting") then
        vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
        vim.api.nvim_create_autocmd("BufWritePost", {
          group = augroup,
          buffer = bufnr,
          callback = function()
            async_formatting(bufnr)
          end,
        })
      end
    end,
  })
end

local function load_plugins()
  -- only add other plugins if they are necessary to reproduce the issue
  require("packer").startup({
    {
      "wbthomason/packer.nvim",
      {
        "jose-elias-alvarez/null-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

Expected behavior

No error should appear (tested this in VSCode and it seems to work there)

Actual behavior

See the error appear on the first line

Debug log

export default ChannelsSelect;

[TRACE Thu 28 Jul 2022 09:31:37 BST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:121: received LSP request for method shutdown
[TRACE Thu 28 Jul 2022 09:31:37 BST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method exit
[WARN  Tue 02 Aug 2022 11:18:39 BST] .../packer/start/null-ls.nvim/lua/null-ls/builtins/init.lua:17: failed to load builtin stylua for method diagnostics; please check your config
[TRACE Fri 05 Aug 2022 16:37:46 BST] ...te/pack/packer/start/null-ls.nvim/lua/null-ls/client.lua:106: starting null-ls client
[TRACE Fri 05 Aug 2022 16:37:46 BST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:121: received LSP request for method initialize
[DEBUG Fri 05 Aug 2022 16:37:46 BST] ...te/pack/packer/start/null-ls.nvim/lua/null-ls/client.lua:161: unable to notify client for method textDocument/didOpen (client not active): {
  textDocument = {
    uri = "file:///home/benedikt/code/upp/packages/frontend/src/components/QualityControlChecks/QualityControlChecks.tsx"
  }
}
[TRACE Fri 05 Aug 2022 16:37:46 BST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method initialized
[TRACE Fri 05 Aug 2022 16:37:46 BST] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method textDocument/didOpen
[TRACE Fri 05 Aug 2022 16:37:46 BST] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG Fri 05 Aug 2022 16:37:46 BST] ...rt/null-ls.nvim/lua/null-ls/helpers/command_resolver.lua:35: attempting to find local executable node_modules/.bin/eslint
[TRACE Fri 05 Aug 2022 16:37:46 BST] ...rt/null-ls.nvim/lua/null-ls/helpers/command_resolver.lua:39: resolved dynamic command for [node_modules/.bin/eslint] with cwd=/home/benedikt/code/upp/packages/frontend
[DEBUG Fri 05 Aug 2022 16:37:46 BST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:286: Using dynamic command for [eslint], got: "/home/benedikt/code/upp/packages/frontend/node_modules/.bin/eslint"
[DEBUG Fri 05 Aug 2022 16:37:46 BST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:346: spawning command "/home/benedikt/code/upp/packages/frontend/node_modules/.bin/eslint" at /home/benedikt/code/upp/packages/frontend with args { "-f", "json", "--stdin", "--stdin-filename", "/home/benedikt/code/upp/packages/frontend/src/components/QualityControlChecks/QualityControlChecks.tsx" }
[DEBUG Fri 05 Aug 2022 16:37:51 BST] ...site/pack/packer/start/null-ls.nvim/lua/null-ls/loop.lua:154: command [/home/benedikt/code/upp/packages/frontend/node_modules/.bin/eslint] timed out after 5000 ms
[TRACE Fri 05 Aug 2022 16:37:51 BST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:217: error output: nil
[TRACE Fri 05 Aug 2022 16:37:51 BST] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:218: output: =============

WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: >=3.2.1 <3.7.0

YOUR TYPESCRIPT VERSION: 3.7.2

Please only submit bug reports when using the officially supported version.

=============

[TRACE Fri 05 Aug 2022 16:37:51 BST] ...ck/packer/start/null-ls.nvim/lua/null-ls/diagnostics.lua:169: received diagnostics from source 3
[TRACE Fri 05 Aug 2022 16:37:51 BST] ...ck/packer/start/null-ls.nvim/lua/null-ls/diagnostics.lua:170: { {
    col = 0,
    end_col = 0,
    end_lnum = 1,
    lnum = 0,
    message = "failed to decode json: Expected value but found invalid token at character 1",
    severity = 1,
    source = "eslint"
  } }

Help

Yes, but I don't know how to start. I would need guidance

Implementation help

I'm happy to try and resolve this issue if I can but I'm not very comfortable with Lua and it's ecosystem (though I've used stylua, luacheck, and can use luarocks), and I'm unsure what parts of the code base are responsible for what. I've not worked on vim/nvim plugins before

Follow up from https://github.com/jose-elias-alvarez/null-ls.nvim/issues/443#issuecomment-1206518889

Requirements

jose-elias-alvarez commented 2 years ago

From your log, the issue is pretty clear: the typescript-estree error message, which is not JSON, is causing the JSON parser to throw. If you can put together a repo that I can use to reproduce this, I can try to see if there's a solution, but the fastest way to solve this may be to resolve the error message by either downgrading your TypeScript version or upgrading whatever dependency is using typescript-estree (which I understand may not be realistic in a commercial project). You could also try using the ESLint language server, which will provide a better experience than null-ls in this case.

benediktms commented 2 years ago

Thanks for your help, that makes sense (actually it didn't occur to me that the warning was being parsed but I guess it makes sense). I looked into this and I think this can probably/hopefully be solved by updating our dependencies. I can try to produce a repo to reproduce this but I think it might not really be worth either of our time 😬

jose-elias-alvarez commented 2 years ago

Sure, feel free to open another issue if you're still running into this after upgrading.