mfussenegger / nvim-lint

An asynchronous linter plugin for Neovim complementary to the built-in Language Server Protocol support.
GNU General Public License v3.0
1.93k stars 203 forks source link

ktlint: Parser failed. <...> Expected value but found invalid token at character 1 #606

Open againstpetra opened 3 months ago

againstpetra commented 3 months ago

I'm seeing this error opening an android kotlin project:

Parser failed. Error message:
~/.local/share/nvim/lazy/nvim-lint/lua/lint/linters/ktlint.lua:8: Expected value but found invalid token at character 1

running ktlint in command line works fine.

mfussenegger commented 1 month ago

Sounds like the output isn't JSON. Is it empty instead?

Could be that it needs a check like this:

diff --git a/lua/lint/linters/ktlint.lua b/lua/lint/linters/ktlint.lua
index a5d6880..7219803 100644
--- a/lua/lint/linters/ktlint.lua
+++ b/lua/lint/linters/ktlint.lua
@@ -3,16 +3,16 @@ return {
   stdin = true,
   args = { '--reporter=json', '--stdin' },
   stream = 'stderr',
   ignore_exitcode = true,
   parser = function(output)
-    local ktlint_output = vim.json.decode(output)
-    if vim.tbl_isempty(ktlint_output) then
+    if output == "" then
       return {}
     end
+    local ktlint_output = vim.json.decode(output)
     local diagnostics = {}
-    for _, error in pairs(ktlint_output[1].errors) do
+    for _, error in ipairs((ktlint_output[1] or {}).errors or {}) do
       table.insert(diagnostics, {
         lnum = error.line - 1,
         col = error.column - 1,
         end_lnum = error.line - 1,
         end_col = error.column - 1,