Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug Failure. Did not expect ImportDeclaration to have an Identifier in its trivia #4271

Closed
samuliraty opened this issue Apr 15, 2024 · 5 comments

Comments

@samuliraty
Copy link

In neovim i get bombarded with:

tsserver: 1: <semantic> TypeScript Server Error (5.3.3)
Debug Failure. Did not expect ImportDeclaration to have an Identifier in its trivia
Error: Debug Failure. Did not expect ImportDeclaration to have an Identifier in its trivia
    at addSyntheticNodes (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:143992:15)
    at processNode (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:143966:5)
    at visitNode2 (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:30227:18)
    at forEachChildInImportDeclaration (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:30533:59)
    at NodeObject.forEachChild (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:143949:12)
    at createChildren (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:143977:8)
    at NodeObject.getChildren (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:143928:48)
    at getTokenAtPositionWorker (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:132140:32)
    at getTouchingToken (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:132113:10)
    at getTouchingPropertyName (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:132110:10)
    at getReferencesAtLocation (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:163581:31)
    at getReferencesInContainer (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:163574:7)
    at getReferencesInContainerOrFiles (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:163157:7)
    at getReferencedSymbolsForSymbol (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:163150:7)
    at Object.getReferencedSymbolsForNode (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:162897:24)
    at Object.getReferenceEntriesForNode (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:162541:30)
    at getSemanticDocumentHighlights (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:135411:59)
    at Object.getDocumentHighlights (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:135399:12)
    at getDocumentHighlights (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:145195:31)
    at C:\x\y\z\z.foo\bar\node_modules\@volar\typescript\lib\node\decorateLanguageService.js:204:120
    at process (C:\x\y\z\z.foo\bar\node_modules\@volar\typescript\lib\node\decorateLanguageService.js:609:28)
    at linkedCodeFeatureWorker (C:\x\y\z\z.foo\bar\node_modules\@volar\typescript\lib\node\decorateLanguageService.js:596:21)
    at languageService.getDocumentHighlights (C:\x\y\z\z.foo\bar\node_modules\@volar\typescript\lib\node\decorateLanguageService.js:204:28)
    at IpcIOSession.getDocumentHighlights (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:184178:61)
    at documentHighlights (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:183149:43)
    at c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:185375:69
    at IpcIOSession.executeWithRequestId (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:185367:14)
    at IpcIOSession.executeCommand (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:185375:29)
    at IpcIOSession.onMessage (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:185417:51)
    at process.<anonymous> (c:\x\y\z\z.foo\bar\node_modules\typescript\lib\tsserver.js:186999:14)
    at process.emit (node:events:514:28)
    at emit (node:internal/child_process:937:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)

Project Dependencies:

...
    "dependencies": {
        "@headlessui/vue": "^1.7.19",
        "pinia": "^2.1.7",
        "validator": "^13.11.0",
        "vue": "^3.3.11",
        "vue-i18n": "^9.9.1",
        "vue-router": "^4.2.5"
    },
    "devDependencies": {
        "@total-typescript/ts-reset": "^0.5.1",
        "@tsconfig/node18": "^18.2.2",
        "@types/node": "^18.19.3",
        "@types/validator": "^13.11.9",
        "@vitejs/plugin-vue": "^4.5.2",
        "@vue/tsconfig": "^0.5.0",
        "@vue/typescript-plugin": "^2.0.13",
        "autoprefixer": "^10.4.17",
        "npm-run-all2": "^6.1.1",
        "postcss": "^8.4.33",
        "prettier": "^3.2.4",
        "tailwindcss": "^3.4.1",
        "typescript": "~5.3.0",
        "vite": "^5.0.10",
        "vue-tsc": "^1.8.25"
    }
...

neovim config (init.lua)

...
        tsserver = {
          init_options = {
            plugins = {
              {
                name = '@vue/typescript-plugin',
                location = '...',
                languages = { 'vue' },
              },
            },
          },
...

Originally posted by @samuliraty in #4246 (comment)

@samuliraty
Copy link
Author

Closing as removing the following from my config ended the issue:

local client = vim.lsp.get_client_by_id(event.data.client_id)
if client and client.server_capabilities.documentHighlightProvider then
  vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
    buffer = event.buf,
    callback = vim.lsp.buf.document_highlight,
  })

  vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
    buffer = event.buf,
    callback = vim.lsp.buf.clear_references,
  })
end

@gin-18
Copy link

gin-18 commented Apr 19, 2024

Excuse me, I have a problem. It works fine without diagnostics, but if diagnostics is present in the code, I still get this error when I called vim.lsp.buf.references. I don't know how to solve this problem.

Here is my lsp setup:

  {
    'neovim/nvim-lspconfig',
    event = 'VeryLazy',
    dependencies = {
      { 'folke/neodev.nvim', opts = {} },
      { 'j-hui/fidget.nvim', opts = {} },
    },
    config = function()
      local lspconfig = require('lspconfig')
      local icons = require('plugins.config.icons').diagnostic_icons

      -- diagnostic
      vim.diagnostic.config({
        virtual_text = false,
        float = { border = 'rounded' },
      })

      -- set signs
      local signs = {
        Error = icons.error,
        Warn = icons.warn,
        Hint = icons.hint,
        Info = icons.info,
      }
      for type, icon in pairs(signs) do
        local hl = 'DiagnosticSign' .. type
        vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
      end

      vim.api.nvim_create_autocmd('LspAttach', {
        group = vim.api.nvim_create_augroup('UserLspConfig', {}),
        callback = function(ev)
          vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

          local opts = { buffer = ev.buf }
          vim.keymap.set('n', '<space>gk', vim.lsp.buf.hover, opts)
          vim.keymap.set('n', '<space>gd', vim.lsp.buf.definition, opts)
          vim.keymap.set('n', '<space>gr', vim.lsp.buf.references, opts)
          vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
          vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
        end,
      })

      -- border for float win
      require('lspconfig.ui.windows').default_options.border = 'rounded'
      local handlers = {
        ['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, { border = 'rounded' }),
        ['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = 'rounded' }),
      }

      -- float diagnostic under cursor
      vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
        group = vim.api.nvim_create_augroup('float_diagnostic', { clear = true }),
        callback = function()
          vim.diagnostic.open_float(nil, { focus = false })
        end,
      })

      -- autocompletion
      local capabilities = require('cmp_nvim_lsp').default_capabilities(vim.lsp.protocol.make_client_capabilities())

      -- on attch
      local on_attach = function(client, bufnr)
        -- float diagnostic under cursor
        vim.api.nvim_create_autocmd('CursorHold', {
          buffer = bufnr,
          callback = function()
            local opts = {
              focusable = false,
              close_events = { 'BufLeave', 'CursorMoved', 'InsertEnter', 'FocusLost' },
              border = 'rounded',
              source = 'always',
              prefix = ' ',
              scope = 'cursor',
            }
            vim.diagnostic.open_float(nil, opts)
          end,
        })

        -- highlight symbol under cursor
        if client.server_capabilities.documentHighlightProvider then
          vim.api.nvim_create_augroup('lsp_document_highlight', {
            clear = false,
          })
          vim.api.nvim_clear_autocmds({
            buffer = bufnr,
            group = 'lsp_document_highlight',
          })
          vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
            group = 'lsp_document_highlight',
            buffer = bufnr,
            callback = function()
              if vim.bo.filetype ~= 'vue' then
                vim.lsp.buf.document_highlight()
              end
            end,
          })
          vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
            group = 'lsp_document_highlight',
            buffer = bufnr,
            callback = function()
              if vim.bo.filetype ~= 'vue' then
                vim.lsp.buf.clear_references()
              end
            end,
          })
        end
      end

      local servers = {
        'clangd',
        'vimls',
        'html',
        'cssls',
        'jsonls',
        'tailwindcss',
      }
      for _, lsp in ipairs(servers) do
        lspconfig[lsp].setup({
          on_attach = on_attach,
          capabilities = capabilities,
        })
      end

      -- lua
      lspconfig.lua_ls.setup({
        on_attach = on_attach,
        handlers = handlers,
        capabilities = capabilities,
        settings = {
          Lua = {
            diagnostics = {
              globals = { 'vim' },
            },
          },
        },
      })

      -- vue
      local getServerPath = function(package_name, server_path)
        local mason_registry = require('mason-registry')
        return mason_registry.get_package(package_name):get_install_path() .. server_path
      end

      local vue_language_server_path = getServerPath('vue-language-server', '/node_modules/@vue/language-server')
      local typescript_language_server_path =
        getServerPath('typescript-language-server', '/node_modules/typescript/lib')

      lspconfig.tsserver.setup({
        on_attach = on_attach,
        capabilities = capabilities,
        filetypes = { 'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue' },
        init_options = {
          plugins = {
            {
              name = '@vue/typescript-plugin',
              location = vue_language_server_path,
              languages = { 'vue' },
            },
          },
        },
      })
      lspconfig.volar.setup({
        on_attach = on_attach,
        capabilities = capabilities,
        init_options = {
          typescript = {
            tsdk = typescript_language_server_path,
          },
        },
      })
    end,
  },

@beselig
Copy link

beselig commented Apr 24, 2024

This issue does not seem resolved, since removing this code is not a fix to the underlying problem. (I am having that same code snippet which @samuliraty removed to remove the error)

The specific code causing the error is this:

 vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
    buffer = event.buf,
    callback = vim.lsp.buf.document_highlight,
  })

It's just responsible to highlight the word underneath the cursor and all its occurences inside the document. That works even when the error occurs, btw. So I don't see a reason why this code should be removed. The TS/vue integration needs to be fixed I suppose.

The complete snippet again for:

          local client = vim.lsp.get_client_by_id(event.data.client_id)
          if client and client.server_capabilities.documentHighlightProvider then
             vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
              buffer = event.buf,
              callback = vim.lsp.buf.document_highlight,
            })

            vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
              buffer = event.buf,
              callback = vim.lsp.buf.clear_references,
            })
          end

@samuliraty
Copy link
Author

Agreed, did not and do not have time to investigate this further, but what I did does not fix the issue, only got rid of the worst symptoms for me.

I will reopen this issue since you are still experiencing it.

@samuliraty samuliraty reopened this Apr 24, 2024
@johnsoncodehk
Copy link
Member

This should fixed by volarjs/volar.js@f0aad5d, please update @vue/typescript-plugin to 2.0.14 or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants