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

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

Open
againstpetra opened this issue Jun 23, 2024 · 2 comments
Labels
needs: contribution Contributions are welcome

Comments

@againstpetra
Copy link

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
Copy link
Owner

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,

@mfussenegger mfussenegger added the needs: contribution Contributions are welcome label Aug 16, 2024
@patrick-hermueller
Copy link

Hi
I will jump in here because i have the same problem, trying your solution doesn't work because it seems that there is something wrong with the mason ktlint binary, i get the following error when printing the output:
.../share/nvim/mason/bin/ktlint: line 10: [: : integer expression expected
I'm quit new to the whole topic would be great if someone could help me fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: contribution Contributions are welcome
Projects
None yet
Development

No branches or pull requests

3 participants