hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.45k stars 370 forks source link

Parsing error: Error 08:05:18 notify.error ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/snippet.lua:411: snippet parsing failed. #1877

Open christianhturner opened 2 months ago

christianhturner commented 2 months ago

FAQ

Announcement

Minimal reproducible full config

return {
    {
        "hrsh7th/nvim-cmp",
        dependencies = {
            "hrsh7th/cmp-cmdline",
            "hrsh7th/cmp-nvim-lsp-document-symbol",
            "hrsh7th/cmp-nvim-lsp-signature-help",
        },
        opts = {
          vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true })
          local cmp = require("cmp")
          local defaults = require("cmp.config.default")()
            sources = {
                { name = "nvim_lsp" },
                { name = "buffer" },
                { name = "path" },
                { name = "nvim_lsp_signature_help" },
            },
            completion = {
                completeopt = "menu,menuone,noinsert",
            },
        },
        config = function(_, opts)
            local cmp = require("cmp")
            cmp.mapping.preset.insert({
                ["<C-u>"] = cmp.mapping.scroll_docs(-4),
                ["<C-d>"] = cmp.mapping.scroll_docs(4),
                ["<c-y>"] = cmp.mapping.complete({ select = false }),
                ["<c-Y>"] = cmp.mapping.confirm({
                    behavior = cmp.ConfirmBehavior.Replace,
                    select = false,
                }),
            })
            cmp.setup(opts)
            -- `/` cmdline setup.
            cmp.setup.cmdline("/", {
                mapping = cmp.mapping.preset.cmdline(),
                sources = cmp.config.sources({
                    { name = "nvim_lsp_document_symbol" },
                }, {
                    { name = "buffer" },
                }),
            })
            -- `:` cmdline setup.
            cmp.setup.cmdline(":", {
                mapping = cmp.mapping.preset.cmdline(),
                sources = cmp.config.sources({
                    { name = "path" },
                }, {
                    {
                        name = "cmdline",
                        option = {
                            ignore_cmds = { "Man", "!" },
                        },
                    },
                }),
            })
        end,
config = function(_, opts)
    for _, source in ipairs(opts.sources) do
      source.group_index = source.group_index or 1
    end
    local cmp = require("cmp")
    local Kind = cmp.lsp.CompletionItemKind
    cmp.setup(opts)
    cmp.event:on("confirm_done", function(event)
      if not vim.tbl_contains(opts.auto_brackets or {}, vim.bo.filetype) then
        return
      end
      local entry = event.entry
      local item = entry:get_completion_item()
      if vim.tbl_contains({ Kind.Function, Kind.Method }, item.kind) then
        local keys = vim.api.nvim_replace_termcodes("()<left>", false, false, true)
        vim.api.nvim_feedkeys(keys, "i", true)
      end
    end)
  end,
    },

}

Description

I'm receiving the following parsing error: Error 08:05:18 notify.error ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/snippet.lua:411: snippet parsing failed. I've identified that the issue began after the last known working commit for me, "b356f2c". Starting at this commit I begin receiving the error being documented. I went into the code base of nvim-cmp, and printed the input being parsed, and while erroring the input is always $a, followed by the parsing error.

I set debugging within the code base to true, and was able to catch this additional logging on the issue:

08:37:17 msg_show >>> InsertEnter
08:37:17 msg_show ctx: `export async function registerHooks() {`
08:37:17 msg_show unchanged
08:37:17 msg_show >>> CursorMovedI
08:37:21 msg_show >>> CursorMovedI
08:37:21 msg_show >>> TextChangedI
08:37:21 msg_show ctx: `   `
08:37:21 msg_show changed
08:37:21 msg_show path^Irequest^I1^I{
08:37:21 msg_show   triggerKind = 1
08:37:21 msg_show }
08:37:21 msg_show nvim_lsp_signature_help^Irequest^I4^I{
08:37:21 msg_show   triggerCharacter = " ",
08:37:21 msg_show   triggerKind = 2
08:37:21 msg_show }
08:37:23 msg_show >>> CursorMovedI
08:37:23 msg_show >>> TextChangedI
08:37:23 msg_show ctx: `   d`
08:37:23 msg_show changed
08:37:23 msg_show nvim_lsp:tsserver^Irequest^I4^I{
08:37:23 msg_show   triggerKind = 1
08:37:23 msg_show }
08:37:23 msg_show buffer^Irequest^I4^I{
08:37:23 msg_show   triggerKind = 1
08:37:23 msg_show }
08:37:23 msg_show buffer^Iretrieve^I166
08:37:23 msg_show nvim_lsp:tsserver^Iretrieve^I2051
08:37:23 msg_show parse:407 logging message registerHooks
08:37:23 msg_show parse:407 logging message _c
08:37:23 msg_show parse:407 logging message _f
08:37:23 msg_show parse:407 logging message _h
08:37:23 msg_show parse:407 logging message _l
08:37:23 msg_show parse:407 logging message _p
08:37:23 msg_show parse:407 logging message _s
08:37:23 msg_show parse:407 logging message $a
08:37:23 msg_show Traceback for parsing input
08:37:23 msg_show stack traceback:
08:37:23 msg_show ^I...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/snippet.lua:410: in function 'parse'
08:37:23 msg_show ^I...turnrq/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:121: in function 'callback'
08:37:23 msg_show ^I.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/cache.lua:38: in function 'get_word'
08:37:23 msg_show ^I...turnrq/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:377: in function 'callback'
08:37:23 msg_show ^I.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/cache.lua:38: in function 'match'
08:37:23 msg_show ^I...urnrq/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:116: in function 'get_entries'
08:37:23 msg_show ^I...cturnrq/.local/share/nvim/lazy/nvim-cmp/lua/cmp/view.lua:75: in function 'open'
08:37:23 msg_show ^I...cturnrq/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:336: in function <...cturnrq/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:309>
   Error  08:37:23 notify.error ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/snippet.lua:411: snippet parsing failed.

This issue is not happening in every project, or even every project with this LSP. I'm having issues with a javascript project. More context in the section below related to context.

Steps to reproduce

If you wish to see the issue within the project that it's occurring within you can feel free to clone the repo I'm working within with:

git clone https://github.com/chturner94/Knowledge-Recalled-PF2e-FVTT

or

gh repo clone chturner94/Knowledge-Recalled-PF2e-FVTT

This assumes you are within a project that could cause this issue, more context below if you want to attempt to create a troublesome project.

  1. Place cursor anywhere within the project.
  2. InsertEnter and begin typing.
  3. With each key stroke, you'll receive an error, as stated above, that parsing has failed. i.e. You want to type const, press c; error notification, press o; error notification, press n; error notification, press s; error notification, press t error notification, press space; error notification. It will stop once you complete the symbol name and append it with a space. You can even back space back into the symbol and completion will begin working. You can type a variable name, followed by =, but after this you'll begin receiving completion errors again.

Expected behavior

When typing you should receive completion context without receiving parsing errors.

Actual behavior

When typing through the project, about 80% of time I'm receiving parsing errors when completion is attempting to occur. I'm not receiving any errors from the LSP. Given this project is the only project I'm having issues with, I thought that maybe there was any issue with the LSP and the data being received by the LSP. It's possible that nvim-cmp can't handle the input being received from the LSP, but what's being output by the LSP in itself doesn't appear to be problematic and most likely it should be something that nvim-cmp should be able to handle.

Additionally, this issue does not occur with commit b356f2c, or any before this commit. I found a commit further back to see if the issue occurred then (a commit from December I believe), and it did not. I then tried every commit from December to present until i encounter the first commit where the problem resumed.

Additional context

I provided the codebase where I'm experiencing this issue, but I'll provide context to the project in the event that someone wishes to try and create a project that the issue occurs within. When using the most current version of nvim-cmp I typically do not have issues with javascript, typescript, or golang. This particular project I admit is more bespoke and is an edge case. It's a module for a tabletop roleplaying platform and I'm using a library for it which allows svelte to transpile to work within the system. It's a javascript project. Here are a few things that I'm doing that I'm not doing in projects that don't have this issues.

  1. Being a plugin, I have created softlinks inside of a directory called external/ found in the project root which link to the application source code to provide type completion and context to global variables within the application.
  2. Uses Vite as the build system. I have another project using vite without issues.
  3. Moderately complex ESLINT configuration (other projects with ESLINT have less complex configs)
  4. Using jsconfig.json file to reference the code found in external/
  5. Svelte files are found in my project. I have another svelte project without issues though.
  6. Complex JSDOC usage.

My suspicion is that JSDOC or typescript typing inside of javascript project might be causing the issue. I've tried suppressing the JSDOC (removing them; and the issue resumes) not sure if there is caching or anything like that though.

DerTimonius commented 2 months ago

Can confirm this bug. Encountered it in a large TypeScript/React Problem and only had the issue in .tsx files. Normal .ts files don't appear to cause this error.

Shougo commented 2 months ago

Hm... It seems the snippet is broken. It is truncated?

Note: https://github.com/hrsh7th/nvim-cmp/commit/b356f2c80cb6c5bae2a65d7f9c82dd5c3fdd6038

I think it truncate.

christianhturner commented 2 months ago

Hm... It seems the snippet is broken. It is truncated?

Note: b356f2c

I think it truncate.

So this is the last commit that is working for me, apologies if I wasn't clear regarding that in the original post, a very truncated version of my actual config (I ommited the commit in the top level minimal config, since it works when I specify this commit:

        "hrsh7th/nvim-cmp",
        commit = "b356f2c",
        dependencies = {
            "hrsh7th/cmp-cmdline",
            "hrsh7th/cmp-nvim-lsp-document-symbol",
            "hrsh7th/cmp-nvim-lsp-signature-help",
        },
.
.
.

This is the commit that seems to have broke the plugin for me: b7b2c12

Looking at this commit it appears that utils/snippet.lua is a brand new implementation and file. That is where I'm getting the error as well, specifically at Line 409.

ziriuz84 commented 2 months ago

Can confirm this bug. Encountered it in a large TypeScript/React Problem and only had the issue in .tsx files. Normal .ts files don't appear to cause this error.

I have the same problem also in normal .ts files

shiro commented 2 months ago

So this is the last commit that is working for me, apologies if I wasn't clear regarding that in the original post, a very truncated version of my actual config (I ommited the commit in the top level minimal config, since it works when I specify this commit:

        commit = "b356f2c",

Can confirm this commit is working, thanks for bisecting!

ziriuz84 commented 1 month ago

I tried last commit, but this issue isn't solved, last working is b356f2c

christianhturner commented 1 month ago

After doing some more investigating on this issue, I found a resolution for my project. At this point I'm not sure if it's nvim-cmp issue or an issue with the LSP itself. I'm working on a project where I'm building a plugin for an electron application. In order to get completion I have a folder called external where I'm creating soft links to the project code that is exporting the global variables. Whenever I remove that folder from my project, I can use the most up to date nvim-cmp. I also noted that the reliability (even when using the older version of nvim-cmp) are more reliable and faster when I remove this directory. I"m not sure if this is a size issue, since others mentioned this issu when working on large projects.

I don't want to close the issue unless it can't be resolved and it has something to do with our projects, I don't think this is likely since it did previously work. Seems like there may need to be some sort of exception that the new implementation is able to handle that it currently isn't. I'd be more than happy to gather more information if that would be helpful, but I'm not entirely sure where I should start. I'll leave this open for now, as the maintainer can always close it.

Not sure if anyone else has gathered any more information since their original issues.

xeho91 commented 1 month ago

I've run into similar issue in a project using Svelte.

While trying to type in insert mode on the *.svelte fules, I get a spam of the following error:

Error executing vim.schedule lua callback: ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/snippet.lua:409: snippet parsing failed.
stack traceback:
    [C]: in function 'error'
    ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/snippet.lua:409: in function 'parse'
    ...xeho91/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:130: in function 'callback'
    .../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/cache.lua:38: in function 'get_word'
    ...xeho91/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:81: in function 'callback'
    .../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/cache.lua:38: in function 'get_offset'
    ...eho91/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:353: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

It might be related to this issue: https://github.com/hrsh7th/nvim-cmp/issues/1858

Workaround

I pinned nvim-cmp version to commit: https://github.com/hrsh7th/nvim-cmp/commit/b356f2c80cb6c5bae2a65d7f9c82dd5c3fdd6038

Source: https://github.com/hrsh7th/nvim-cmp/issues/1858#issuecomment-2014660238

To anyone using LazyVim, there's the quick snippet that may help you continue to work:

return {
    {
        -- https://github.com/hrsh7th/nvim-cmp
        "hrsh7th/nvim-cmp",
        commit = "b356f2c",
        pin = true,
    },
}
ziriuz84 commented 1 month ago

To anyone using LazyVim, there's the quick snippet that may help you continue to work:

return {
  {
      -- https://github.com/hrsh7th/nvim-cmp
      "hrsh7th/nvim-cmp",
      commit = "b356f2c",
      pin = true,
  },
}

Since I'm using LazyVim distro, if I write a simple lua file in my plugins directory does it overwrite all nvim-cmp configuration or does it simply update it?

mikededo commented 1 month ago

Tried what @xeho91 said, yet LazyVim is updating to the latests version all the time... Can't find what's wrong in the config 🤔

xeho91 commented 1 month ago

To anyone using LazyVim, there's the quick snippet that may help you continue to work:

return {
    {
        -- https://github.com/hrsh7th/nvim-cmp
        "hrsh7th/nvim-cmp",
        commit = "b356f2c",
        pin = true,
    },
}

Since I'm using LazyVim distro, if I write a simple lua file in my plugins directory does it overwrite all nvim-cmp configuration or does it simply update it?

I can't give an honest answer, because I don't override the default config for nvim-cmp set by LazyVim.

mikededo commented 1 month ago

Tried what @xeho91 said, yet LazyVim is updating to the latests version all the time... Can't find what's wrong in the config 🤔

Don't mind me, made it work finally.

Lompad commented 1 month ago

@mikededo How exactly did you make it work? I'm having the same issue and have tried pretty much everything.

suamai commented 1 month ago

@Lompad I was having the same issue, and reinstalling everything with the pinned commit version config from the get go solved it for me.

My uneducated guess is that lazy-lock or something on the shared folder were conflicting with the pin commit and preventing it from taking effect.

arjenbloemsma commented 1 month ago

Same for me. After pinning nvim-cmp to that specific commit as described above, I removed nvim-cmp via the Lazy menu (leader L) and from there I removed the nvim-cmp package (go to the nvim-cmp line and hit 'x'). After that close and re-open nvim and the error is gone.

Thanks for the workaround guys. Now let's hope the real solution will come soon.

DerTimonius commented 2 weeks ago

I think this has been fixed with one of the latest commits. Just removed the pin to the suggested commit and I did not get this error in the places where I got them last time

Shougo commented 2 weeks ago

So it should be closed?

LamprosPitsillos commented 1 week ago

I don't think so , error is still present on latest cmp sadly