nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.68k stars 893 forks source link

Sometimes `jsx` files are not getting parsed - "No parser available for the given buffer" #4918

Closed primeapple closed 1 year ago

primeapple commented 1 year ago

Describe the bug

Sometimes when working my highlighting for jsx files does not work. It may also be on js files or even others, but there the lsp is providing semantic information, so it doesn't catch my eye.

Typing :Inspect on a token in this file does only display a "Semantic tokens" and not a "Treesitter" section.

Running :InspectTree yields the following error:

Error executing Lua callback: /usr/share/nvim/runtime/lua/vim/treesitter/playground.lua:241: No parser available for the given buffer                                                                                                           
stack traceback:                                                                                                                                                                                                                                
        [C]: in function 'assert'                                                                                                                                                                                                               
        /usr/share/nvim/runtime/lua/vim/treesitter/playground.lua:241: in function 'inspect_tree'                                                                                                                                               
        /usr/share/nvim/runtime/lua/vim/treesitter.lua:496: in function 'inspect_tree'                                                                                                                                                          
        /usr/share/nvim/runtime/plugin/nvim.lua:18: in function </usr/share/nvim/runtime/plugin/nvim.lua:9> 

To Reproduce

I don't know exactly how to reproduce. When I close Neovim and reopen it works for a while on jsx files.

It seems that once it fails once for a .jsx file it doesn't even try anymore and fails on all the other .jsx files as well.

Expected behavior

No response

Output of :checkhealth nvim-treesitter

Error executing Lua callback: /usr/share/nvim/runtime/lua/vim/treesitter/playground.lua:241: No parser available for the given buffer                                                                                                           
stack traceback:                                                                                                                                                                                                                                
        [C]: in function 'assert'                                                                                                                                                                                                               
        /usr/share/nvim/runtime/lua/vim/treesitter/playground.lua:241: in function 'inspect_tree'                                                                                                                                               
        /usr/share/nvim/runtime/lua/vim/treesitter.lua:496: in function 'inspect_tree'                                                                                                                                                          
        /usr/share/nvim/runtime/plugin/nvim.lua:18: in function </usr/share/nvim/runtime/plugin/nvim.lua:9> 

### Output of `nvim --version`

```text
NVIM v0.9.1
Build type: Release
LuaJIT 2.1.0-beta3

Additional context

My Treesitter config (using lazy.nvim):

    {
        'nvim-treesitter/nvim-treesitter',
        build = ':TSUpdate',
        version = false,
        event = { 'BufReadPost', 'BufNewFile' },
        dependencies = {
            'nvim-treesitter/nvim-treesitter-context',
            'RRethy/nvim-treesitter-textsubjects',
        },
        config = function()
            require('nvim-treesitter.configs').setup({
                ensure_installed = {
                    'bash',
                    'c',
                    'css',
                    'dockerfile',
                    'fish',
                    'html',
                    'java',
                    'javascript',
                    'json',
                    'lua',
                    'luadoc',
                    'luap',
                    'markdown',
                    'markdown_inline',
                    'nix',
                    'python',
                    'query',
                    'regex',
                    'ruby',
                    'rust',
                    'scss',
                    'svelte',
                    'tsx',
                    'typescript',
                    'vim',
                    'vimdoc',
                    'vue',
                    'yaml',
                },
                highlight = {
                    enable = true,
                    -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
                    additional_vim_regex_highlighting = false,
                },
                indent = {
                    enable = true,
                },
                -- for nvim-treesitter-textsubjects
                textsubjects = {
                    enable = true,
                    prev_selection = ',', -- (Optional) keymap to select the previous selection
                    keymaps = {
                        ['.'] = 'textsubjects-smart',
                        ['a;'] = 'textsubjects-container-outer',
                        ['i;'] = 'textsubjects-container-inner',
                    },
                },
                -- for vim-matchup
                matchup = {
                    enable = true,
                },
            })
            require('treesitter-context').setup()
        end,
    }
clason commented 1 year ago

Please try to find a reproducer without additional plugins. This report is unfortunately not very much to go on since we don't have any concrete circumstances when the issue appears.

primeapple commented 1 year ago

I'll try to disable all the treesitter related plugins. Is there any way to see enable Treesitter logging? Or maybe Parser specific logging?

TheLeoP commented 1 year ago

This happens because vim.treesitter.language.get_lang checks if there is a parser for javascriptreact and, since there isn't, it tries to register a parser for the language javascriptreact and the filetype javascriptreact. Since the parser being used by this filetype is tsx, this fails. Doing vim.treesitter.inspect_tree({lang='tsx'})) also does not work because then it does not detect the filetype and tries to register a parser for the language tsx and the filetype tsx.

But I guess this issue should be reported to the upstream Neovim repo, right?

clason commented 1 year ago

No, this is an nvim-treesitter issue; the parser should be correctly registered here.

If we can't rely on the ft->parser association javascriptreact -> javascript, we need to drop it, and users need to manually start the parser via vim.treesitter.start(buffer, language).

primeapple commented 1 year ago

I tried around with disabling all kind of Treesitter related Plugins, does not seem to work. Also it is not on the same file, it just dies randomly, sometimes directly on start.

Is there really no logging when TS failed to parse something?

This happens because vim.treesitter.language.get_lang checks if there is a parser for javascriptreact and, since there isn't, it tries to register a parser for the language javascriptreact and the filetype javascriptreact. Since the parser being used by this filetype is tsx, this fails. Doing vim.treesitter.inspect_tree({lang='tsx'})) also does not work because then it does not detect the filetype and tries to register a parser for the language tsx and the filetype tsx.

But I guess this issue should be reported to the upstream Neovim repo, right?

Not 100% sure what you mean. jsx files are parsed by the javascript parser, not by the tsx one.

primeapple commented 1 year ago

I think I found something to reproduce this.

My workflow is the following:

  1. Open Neovim
  2. Use telescope to fuzzyfind a file
  3. Edit something
  4. Use telescope to go to the next file...

While doing this, Treesitter has to parse all previews of all the files that I came across while in Telescope.

Now something special happens, when I open the preview of a Markdown file, that has a codeblock (3 backticks) followed by jsx. From this moment on, Treesitter can not parse .jsx files anymore.

If I change the jsx to js in the markdown file, I get correct highlighting of the codeblock (which I didn't get before) as well as Treesitter still works for .jsx files.

So what is the magic bullet here, now that I can reproduce it? :D

Herob527 commented 1 year ago

I had similar issue when using ts:get_node()

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

So it's a thing

Even thought I solved somewhat issue, but caused issues with snippet not properly determining if it's inside function or in global scope

I had similar issue with tsx too

schardev commented 1 year ago

Can confirm this as well. I notice this happens particularly when I edit a markdown file (especially the code blocks) and when I move to a tsx/jsx file it just fails to find the right parser, which is weird as even the basic vim.treesitter.get_parser() command fails.

This happens randomly though so there isn't a concrete repro of the issue.

towry commented 1 year ago

Check this https://github.com/neovim/neovim/issues/24531

llllvvuu commented 1 year ago

We can now close this (https://github.com/neovim/neovim/pull/25151, https://github.com/neovim/neovim/pull/25143)

lucario387 commented 1 year ago

Not yet since the patch is not available for stable users.

rendi12345678 commented 1 month ago

if u keep getting this type of error, i have the solution to auto change jsx files to js, here is the code

-- Automatically set filetype for .jsx files to javascript
vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
  pattern = "*.jsx",
  callback = function()
    vim.bo.filetype = "javascript"
  end
})