nvim-treesitter / nvim-treesitter-textobjects

Apache License 2.0
2.19k stars 196 forks source link

C/C++ Grammer-related crash when opening file #522

Closed bicyclespokesperson closed 1 year ago

bicyclespokesperson commented 1 year ago

Describe the bug

When I open a C++ file, I get a crash with the following error message:

Error detected while processing BufReadPost Autocommands for "*":                                                                                                                                                                                    
Error executing lua callback: ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24: Error executing lua: ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:25: BufReadPost Autocommands for "*"..FileType Autocommands for "*": V
im(append):Error executing lua callback: ...im/0.9.4/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid node type at position 878 for language c                                                                                    
stack traceback:                                                                                                                                                                                                                                     
        [C]: in function '_ts_parse_query'                                                                                                                                                                                                           
        ...im/0.9.4/share/nvim/runtime/lua/vim/treesitter/query.lua:259: in function 'get_query'                                                                                                                                                     
        ...r-textobjects/lua/nvim-treesitter/textobjects/shared.lua:99: in function 'available_textobjects'                                                                                                                                          
        ...r-textobjects/lua/nvim-treesitter/textobjects/select.lua:172: in function 'attach'                                                                                                                                                        
        ...lazy/opt/nvim-treesitter/lua/nvim-treesitter/configs.lua:509: in function 'attach_module'                                                                                                                                                 
        ...lazy/opt/nvim-treesitter/lua/nvim-treesitter/configs.lua:532: in function 'reattach_module'                                                                                                                                               
        ...lazy/opt/nvim-treesitter/lua/nvim-treesitter/configs.lua:133: in function <...lazy/opt/nvim-treesitter/lua/nvim-treesitter/configs.lua:132>                                                                                               
        [C]: in function 'nvim_cmd'                                                                                                                                                                                                                  
        ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:25: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24>                                                                                                 
        [C]: in function 'nvim_buf_call'                                                                                                                                                                                                             
        ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:10>                                                                                                 
stack traceback:                                                                                                                                                                                                                                     
        [C]: in function 'nvim_cmd'                                                                                                                                                                                                                  
        ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:25: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24>                                                                                                 
        [C]: in function 'nvim_buf_call'                                                                                                                                                                                                             
        ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:10>                                                                                                 
stack traceback:                                                                                                                                                                                                                                     
        [C]: in function 'nvim_buf_call'                                                                                                                                                                                                             
        ...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:10>      

I cannot reference text objects in C/C++, though I can reference them in other languages such as Python.

It appears to be related to the C grammar file (which is imported by the C++ grammar file): If I comment out this section of the file, the crash goes away and I can reference the C++ text objects correctly (with, for example, vaf to select a function).

; (if_statement
;   alternative: (else_clause (compound_statement . "{" . (_) @_start @_end (_)? @_end . "}"
;   (#make-range! "conditional.inner" @_start @_end)))) @conditional.outer

To Reproduce

Opening any C or C++ file in vim can cause the crash for me.

I'm using the following config (copy/pasted from the README). I'm using lunarvim 1.3, (a wrapper layer around neovim 0.9), so this config is in ~/.config/lvim/config.lua

lvim.plugins = {
  -- {example/plugin}
  {
    "nvim-treesitter/nvim-treesitter-textobjects",
    after = "nvim-treesitter",
    config = function()
      require 'nvim-treesitter.configs'.setup {
        textobjects = {
          select = {
            enable = true,

            -- Automatically jump forward to textobj, similar to targets.vim
            lookahead = true,

            keymaps = {
              -- You can use the capture groups defined in textobjects.scm
              ["af"] = "@function.outer",
              ["if"] = "@function.inner",
              ["ac"] = "@class.outer",
              -- You can optionally set descriptions to the mappings (used in the desc parameter of
              -- nvim_buf_set_keymap) which plugins like which-key display
              ["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
              -- You can also use captures from other query groups like `locals.scm`
              ["as"] = { query = "@scope", query_group = "locals", desc = "Select language scope" },
            },
            -- You can choose the select mode (default is charwise 'v')
            --
            -- Can also be a function which gets passed a table with the keys
            -- * query_string: eg '@function.inner'
            -- * method: eg 'v' or 'o'
            -- and should return the mode ('v', 'V', or '<c-v>') or a table
            -- mapping query_strings to modes.
            selection_modes = {
              ['@parameter.outer'] = 'v', -- charwise
              ['@function.outer'] = 'V', -- linewise
              ['@class.outer'] = '<c-v>', -- blockwise
            },
            -- If you set this to `true` (default is `false`) then any textobject is
            -- extended to include preceding or succeeding whitespace. Succeeding
            -- whitespace has priority in order to act similarly to eg the built-in
            -- `ap`.
            --
            -- Can also be a function which gets passed a table with the keys
            -- * query_string: eg '@function.inner'
            -- * selection_mode: eg 'v'
            -- and should return true of false
            include_surrounding_whitespace = true,
          },
        },
      }
    end,
    dependencies = "nvim-treesitter/nvim-treesitter",
  },
}

Expected behavior

There should be no crash, the file should open and I should be able to reference C++ text objects.

Output of :checkhealth nvim-treesitter

============================================================================== nvim-treesitter: require("nvim-treesitter.health").check() Installation ~ - OK `tree-sitter` found 0.20.8 (9f398ca955d3329110d31ed523dac5275074948f) (parser generator, only needed for :TSInstallFromGrammar) - OK `node` found v14.18.2 (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 15.0.0 (clang-1500.0.40.1) - 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.6.0", sysname = "Darwin", version = "Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000" } ~ Parser/Features H L F I J - bash ✓ ✓ ✓ . ✓ - c ✓ ✓ ✓ ✓ ✓ - cmake ✓ . ✓ . . - comment ✓ . . . . - cpp ✓ ✓ ✓ ✓ ✓ - dockerfile ✓ . . . ✓ - gitcommit ✓ . . . ✓ - gitignore ✓ . . . . - go ✓ ✓ ✓ ✓ ✓ - javascript ✓ ✓ ✓ ✓ ✓ - json ✓ ✓ ✓ ✓ . - kdl ✓ ✓ ✓ ✓ ✓ - lua ✓ ✓ ✓ ✓ ✓ - make ✓ . ✓ . ✓ - markdown ✓ . ✓ . ✓ - markdown_inline ✓ . . . ✓ - proto ✓ . ✓ . . - python ✓ ✓ ✓ ✓ ✓ - query ✓ ✓ ✓ ✓ ✓ - regex ✓ . . . . - rust ✓ ✓ ✓ ✓ ✓ - toml ✓ ✓ ✓ ✓ ✓ - vim ✓ ✓ ✓ . ✓ - vimdoc ✓ . . . ✓ - yaml ✓ ✓ ✓ ✓ ✓ 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} ~

Output of nvim --version

λ nvim --version
NVIM v0.9.4
Build type: Release
LuaJIT 2.1.0-beta3

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/0.9.4/share/nvim"

Run :checkhealth for more info

Additional context Add any other context about the problem here.

dwcoates commented 1 year ago

Also experience this error, also confirm that commenting out those lines in textobjects.scm fixes

kiyoon commented 1 year ago

I personally don't have this problem.

@dwcoates Are you also using LunarVim?

I think what's likely that happened was that C parser has been updated, and your nvim-treesitter version is old. This used to happen when you use LunarVim because they snapshot the old version and restrict it from being updated.

@theHamsta Can you confirm this? @mortezadadgar Anything you know why this happens?

bicyclespokesperson commented 1 year ago

Thanks for the quick reply! It seems like this is indeed the case. The last commit to update the C parser was a178586, and I can confirm that e1e670a (the commit before a178586) works for me. Unfortunately I can't update nvim-treesitter without breaking some other lunarvim stuff, but I'm happy to use e1e670a for now

kiyoon commented 1 year ago

That's good to know! Until LunarVim officially update nvim-treesitter it will be good to snapshot this plugin as well.