Tsserver workspace/symbol includes symbols from imported modules?

Hi,

Attempting to setup tsserver following the instructions in the nvim-lspconfig docs.

Everything works great except that when I call vim.lsp.buf.workspace_symbol("whatever") I get symbols from my node_modules in addition to those in my own code.

I use a jsconfig.json file as suggested in the docs:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "checkJs": false
  },
  "exclude": [
    "node_modules"
  ]
}

Experimenting with adding different folders to “exclude” I do get the expected results, so I know that the jsconfig.json is being read and processed. My suspicion is then that tsserver gets symbols from all packages that I import, in this case e.g. React (and all it’s imports?).

Could this be what’s happening and if so how do I stop it? I want to get a list of all the symbols in my code, like vim.lsp.buf.document_symbol() but for all the files in my workspace (excluding node_modules).

Instead of an exclude try the includes key and add the files and folders you want tsserver to work under. Something like the following:

"includes": ["./src/**/*.ts", "./some/other/folder"]

Thanks, but using include gives me the same results: Symbols from e.g. src/**/*.js and from node_modules.

Huh that is weird, I’m trying this on my repo which has a jsconfig (poker-game-app) and I’m not getting any workspace symbols from node_modules at all.

What I did: opened src/App.jsx and performed a workspace symbol search for useStyles with :lua vim.lsp.buf.workspace_symbol('useStyles')

I’m not sure what else could be an issue, but take a look at the jsconfig there and see if there is anything missing or something different.

Thanks, that’s really helpful! Will test in your project and report back :slight_smile:

Ok, so in poker-game-app when I do :lua vim.lsp.buf.workspace_symbol('useStyles') I indeed get only symbols from the project.

But if I try :lua vim.lsp.buf.workspace_symbol('render') I get node_modules symbols, e.g:

node_modules/react-dom/cjs/react-dom.production.min.js|282 col 220| [Method] render

Grepping I find that both useStyles and render are present in node_modules, so this might support my theory that workspace symbols are being polluted (from my point of view) by imports.

Would be interesting to see if you get the same results as I do with ‘render’ as search string, if you have the time. If i use the empty string (should return all symbols), I get no results at all - my suspicion is that this is because some maximum is reached.

Hey, so I tried it and you’re right I also see node_modules when I do render. Looking further into the workspace/symbol lsp protocol it seems like it doesn’t have the ability to ignore files/folders? I’m not too sure so probably want to ask this in the typescript-language-server issues page.

But I think it’s possible to filter your results after you get them into the quickfix list, from this stack overflow post: vim - Quickfix list, how to add and remove entries - Stack Overflow - you can use cfilter to remove any thing you don’t want inside the qf list.

So you can:

  1. First add the package if you didn’t: :packadd cfilter
  2. Call :lua vim.lsp.buf.workspace_symbol('render')
  3. Then call :Cfilter! /node_modules/

Now I’m sure you could automate this in lua somehow, I tried

:lua vim.lsp.buf.workspace_symbol('render') | Cfilter! /node_modules/

but it looks like it takes the entire line as lua and give me a syntax error. But it’s up to you how you would want to filter it

Thanks for the help. I figured I should ask in typescript-language-server, but thought this seemed like such a common use case that I assumed I had configured something wrong.

In my use case filtering the quickfix list doesn’t really cut it, since I am most interested in using workspace/symbol with an empty search string to get all symbols. But this returns nothing, as I said. What I am after is for this to replace ctags, letting me quickly jump to any symbol in my code.

1 Like