Skip to content

Error opening Astro file in Neovim when tsconfig.json has a trailing commaΒ #13

@ivanalejandro0

Description

@ivanalejandro0

Issue initially reported on neovim/neovim#32422

Problem

Neovim shows an error and fails to syntax highlight on Astro files when a trailing comma is left in the tsconfig.json file.

Steps to reproduce

$ nvim --clean example.astro
Error detected while processing BufReadPost Autocommands for "*":
Error executing lua callback: ...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetype.lua:35: Error executing lua: ...ro-fix/nvim-linux-x86
_64/share/nvim/runtime/filetype.lua:36: BufReadPost Autocommands for "*"..FileType Autocommands for "*"..function <SNR>1_LoadFTPlugin[20]..
script /home/user/nvim-astro-fix/nvim-linux-x86_64/share/nvim/runtime/ftplugin/astro.vim[163]..function <SNR>21_CollectPathsFrom
Config, line 11: Vim(let):E474: Trailing comma: }
stack traceback:
        [C]: in function 'nvim_cmd'
        ...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetype.lua:36: in function <...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetyp
e.lua:35>
        [C]: in function 'nvim_buf_call'
        ...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetype.lua:35: in function <...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetyp
e.lua:10>
stack traceback:
        [C]: in function 'nvim_buf_call'
        ...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetype.lua:35: in function <...ro-fix/nvim-linux-x86_64/share/nvim/runtime/filetyp
e.lua:10>
Press ENTER or type command to continue
$ nvim --version
NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info

Opening Neovim and then opening the file results in no syntax highlight.
Reloading the file with :e shows the error on screen.

Files to reproduce the issue:

example.astro:

---
---
<p>This is an example</p>

tsconfig.json:

{
  "extends": "astro/tsconfigs/strict",
}

About the problem

The trailing comma shouldn't be there, but I had more options below the "extends" and I commented them out, leaving the trailing comma by mistake.

We could argue that the trailing comma shouldn't be there, but the tsconfig.json file does support trailing commas and comments (I went through typescript docs and github issues but I couldn't find a reference for the file format).

Expected behavior

In my opinion, the UI shouldn't break like this but I don't know how would it be the proper way of handling this.

As a workaround, since the problem appears when a trailing comma is left on the tsconfig.json, just removing that trailing comma makes the error go away.

A possible fix

The problem seems to come from an unhandled error on runtime/ftplugin/astro.vim (Neovim's path), or in this repo https://github.com/wuelnerdotexe/vim-astro/blob/9b4674ecfe1dd84b5fb9b4de1653975de6e8e2e1/autoload/astro.vim .

I wrapped the problematic code with a try/catch and that made the error go away.
I don't know what would be the correct behavior on Neovim when this problem happens.

54,60c54,66
<     let paths_from_config = config_json
<                 \ ->readfile()
<                 \ ->filter({ _, val -> val =~ '^\s*[\[\]{}"0-9]' })
<                 \ ->join()
<                 \ ->json_decode()
<                 \ ->get('compilerOptions', {})
<                 \ ->get('paths', {})
---
>     try
>         let paths_from_config = config_json
>                     \ ->readfile()
>                     \ ->filter({ _, val -> val =~ '^\s*[\[\]{}"0-9]' })
>                     \ ->join()
>                     \ ->json_decode()
>                     \ ->get('compilerOptions', {})
>                     \ ->get('paths', {})
>     catch
>         " not actually handling the error, what should we do here? inform the
>         " user somehow about a broken json config?
>         return
>     endtry

The original implementation gets compilerOptions and paths from the tsconfig.json file and uses them, but with this approach those options will be ignored if present.
I think that a better approach would be to read those options regardless of issues with the file format.

Does this affect Vim?

No, I just downloaded the AppImage for Vim v9.1.1106 and I couldn't reproduce the issue.
https://github.com/vim/vim-appimage/releases/tag/v9.1.1106

From the discussion on #9 it seems that the json decoder on Vim is more tolerant to trailing commas than Neovim's json decoder.

Extra broken tsconfig.json file

If you add two trailing commas to a line or make some other change that makes the file not conform with what's expected will break both Vim and Neovim.

How to proceed?

I'm not sure what would be the best UI/UX on this case.

I don't think that there should be errors while opening an Astro file or breaking the syntax highlighting because of some other file in the repository. The user may not make the connection between a problem in the tsconfig.json file and the Astro file being edited.
But on the other hand, if the contents of the tsconfig.json file are being used for properly help the user while editing an Astro file, it may be misleading to show different results because of a problem while parsing a the json file.
Also, there's no guarantee that Vim/Neovim will parse the tsconfig.json file in the same way as tsc, so I guess there's only so much that can be done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions