nvim-treesitterで「query: invalid node type at...」エラーが出た

## 問題

久しぶりにNeoVim開いて`init.lua`開いたら大変なことになっていることに気がつきました

![スクリーンショット 2022-12-17 19.37.42.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/677057/f7c56814-de78-bfad-5af2-3c09e2b2e468.png)
(カラースキーム: iceberg)

この場合はtreesitterの色がついている(白文字はほとんどない)けれど、ほぼ全部白文字+真っ赤エラーの時もありました

## 環境

macOS 13.0
nvim, treesitterはbrew経由

```terminal
> nvim --version
NVIM v0.8.1
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@Ventura-arm64.local

Features: +acl +iconv +tui
See ":help feature-compile"

      システム vimrc: "$VIM/sysinit.vim"
       省略時の $VIM: "/opt/homebrew/Cellar/neovim/0.8.1/share/nvim"

Run :checkhealth for more info

> tree-sitter --version
tree-sitter 0.20.7
```

## :checkhealth nvim-treesitter

```

nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - OK: `tree-sitter` found 0.20.7 (parser generator, only needed for :TSInstallFromGrammar)
  - OK: `node` found v14.17.0 (only needed for :TSInstallFromGrammar)
  - OK: `git` executable found.
  - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
    Version: Apple clang version 14.0.0 (clang-1400.0.29.202)
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## OS Info:
{
  machine = "arm64",
  release = "22.1.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103"
}

## Parser/Features H L F I J
  - vim            x ✓ ✓ . ✓
  - help           x . . . x
  - lua            ✓ ✓ ✓ ✓ ✓
  - c              ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang}

## The following errors have been detected:
  - ERROR: vim(highlights): ...im/0.8.1/share/nvim/runtime/lua/vim/treesitter/query.lua:219: query: invalid node type at position 2765 for language vim
    vim(highlights) is concatenated from the following files:
    | [ERROR]:"/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/highlights.scm", failed to load: ...im/0.8.1/share/nvim/runtime/lua/vim/treesitter/query.lua:219: query: invalid node type at position 2765 for language vim
  - ERROR: help(highlights): ...im/0.8.1/share/nvim/runtime/lua/vim/treesitter/query.lua:219: query: invalid node type at position 100 for language help
    help(highlights) is concatenated from the following files:
    | [ERROR]:"/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/help/highlights.scm", failed to load: ...im/0.8.1/share/nvim/runtime/lua/vim/treesitter/query.lua:219: query: invalid node type at position 100 for language help
  - ERROR: help(injections): ...im/0.8.1/share/nvim/runtime/lua/vim/treesitter/query.lua:219: query: invalid node type at position 14 for language help
    help(injections) is concatenated from the following files:
    | [ERROR]:"/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/help/injections.scm", failed to load: ...im/0.8.1/share/nvim/runtime/lua/vim/treesitter/query.lua:219: query: invalid node type at position 14 for language help

```

さっきのエラーが出てますね

## 結論

エラーでググるとこんなのが出てきます

https://github.com/nvim-treesitter/nvim-treesitter/issues/3092

内容はこんな感じ

> 1. `:TSInstall! <lang>`を実行しろ
> 2. 1. で治らなければ以下のコマンドを実行し、
> ```vim
> :echo nvim_get_runtime_file('*/<lang>.so', v:true)
> :echo nvim_get_runtime_file('queries/<lang>/*.scm', v:true)
> ```
> nvim-treesitter以下でないフォルダが出てこればそれを削除しろ

やってみます
コマンドの<lang>の部分は自分がエラー出てるやつにしてください
僕はとりあえず"vim"を指定してやります

### 1. `:TSInstall! vim`

ハイライトはちょっと回復した、けどエラーは出るし起動し直したりしてるとハイライトも消える ダメそう

### 2. コマンド→ファイル削除

`:echo nvim_get_runtime_file('*/vim.so', v:true)`
```
['/opt/homebrew/Cellar/neovim/0.8.1/lib/nvim/parser/vim.so']
```

これnvim-treesitter以下じゃなさそうだな...

`:echo nvim_get_runtime_file('queries/vim/*.scm', v:true)`
```
['/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/folds.scm',
'/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/highlights.scm',
'/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/injections.scm',
'/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/locals.scm',
'/opt/homebrew/Cellar/neovim/0.8.1/share/nvim/runtime/queries/vim/highlights.scm',
'/opt/homebrew/Cellar/neovim/0.8.1/share/nvim/runtime/queries/vim/injections.scm']
```

最後の2つ、nvim-treesitter外っぽい

→`/opt/homebrew/Cellar/neovim/0.8.1/lib/nvim/parser`と`/opt/homebrew/Cellar/neovim/0.8.1/share/nvim/runtime/queries`消せば解決するのでは?

やってみる

### 削除後

`:echo nvim_get_runtime_file('*/vim.so', v:true)`
```
[]
```

`:echo nvim_get_runtime_file('queries/vim/*.scm', v:true)`
```
['/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/folds.scm',
'/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/highlights.scm',
'/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/injections.scm',
'/Users/zoi/.local/share/nvim/site/pack/jetpack/opt/nvim-treesitter/queries/vim/locals.scm']
```

うん、綺麗

`:checkhealth nvim-treesitter`
```
nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - OK: `tree-sitter` found 0.20.7 (parser generator, only needed for :TSInstallFromGrammar)
  - OK: `node` found v14.17.0 (only needed for :TSInstallFromGrammar)
  - OK: `git` executable found.
  - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
    Version: Apple clang version 14.0.0 (clang-1400.0.29.202)
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## OS Info:
{
  machine = "arm64",
  release = "22.1.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103"
}

## Parser/Features H L F I J
  - lua            ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang}

```
やった! エラー消えた!

---

というわけで、無事にエラー消えました
nvimのインストールフォルダ内にあったのも空ファイルではないんですが、おそらく古いファイルが残っていて、`:TSUpdate`でnvim-treesitter内のファイルは更新されたんだろうけど、別ファイルを参照するようになってたんじゃないかと思います。PATHの優先順みたいな感じ?
NeoVimを気分で触ってるにわかですが、やっぱり公式のGitHubのissueが一番信用できますね

![スクリーンショット 2022-12-17 19.56.12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/677057/b68a9bee-c1ca-1fe3-977d-5c56551fc0a5.png)

それではみなさん、良いNeoVimライフを!