reddit / emacs

[LSP] `ts-ls` Doesn't Recognize Test Directory

LemongrabThree · 2022-04-18 20:04
Good evening! I recently installed `lsp-mode` for the Node TypeScript project (using `ts-ls`) I'm working on at the moment, mainly just because I wanted `lsp-rename`. The problem is, it only works within `src`: If I rename a variable, then all files in `src` get updated, but not in my test directory, `tests`. A lead: If I delete `.lsp-session-v1`, then open a file in the test dir, then start LSP (and thus import the project) from that buffer, then `lsp-rename` (on a file in `src`) will also affect that one file in the test directory, but not others. `~/.emacs.d/.lsp-session-v1` contains no mention of the test file or dir in this case. It also doesn't work any more if I restart Emacs after this. I thought `lsp-workspace-folders-add` sounded good, but it doesn't appear to do anything except make it so that the rename doesn't even work across the files in `src` any more either. Tried renaming `test` to `tests`, didn't work. The fact that it searches `src` even though `.lsp-session-v1` only mentions the root directory indicates that it has some fixed folder mappings. Adding more of these would probably be the best solution. But how do I do this? I'm not very advanced with this package or Emacs in general.
TheFrenchPoulp · 2022-04-18 20:44
You probably want to set the base URL in your tsconfig.json to the root rather than src, assuming this is your issue
LemongrabThree · 2022-04-19 11:20
No, it was already set to the project root. It turns out that adding the test directory to the `include` list in the tsconfig works, but that's a problem since that file is also for other purposes. In my case, Jest now runs all test suites twice, and all tests fail even though they were fine without that `include`. I mean, my current task is just to mass rename a bunch of types and functions without changing functionality, so this is actually sorta workable for this, but not in the long term... In any case, thanks, you've helped me in understanding how this works. Do you happen to know more about how it the LSP interprets the config file or if there's any other way to set the path just for this purpose? I just looked in `lsp-javascript.el` and `lsp-mode.el` and have nowhere near enough knowledge about Lisp to make sense of the code.
TheFrenchPoulp · 2022-04-19 12:03
> Do you happen to know more about how it the LSP interprets the config file or if there's any other way to set the path just for this purpose? I'm not sure no, I haven't used `lsp-mode` in quite some time and have no issue with `eglot` with similar setups That being said, it has more to do with how the TypeScript server interprets your configuration file rather than how `lsp-mode` reads that. I'm not sure which server `ts-ls` actually is but here is the one I use daily (MacOS and Arch) with 0 path issue wrt/ tests. $ npm -g list /Users/username/.nvm/versions/node/v17.3.1/lib ├── corepack@0.10.0 ├── npm@8.7.0 └── typescript-language-server@0.9.7
covertcj · 2022-04-19 16:34
I can't really speak to the LSP with emacs specifically because I'm still new to it, but typescript and the language server don't really give you a lot of customization options outside of just updating your tsconfig. That being said, I just helped solve a similar problem at work. We recently switched up the way that our tsconfig hierarchy worked, because any editors that rely on LSPs (like VS Code) have issues if you don't include the test files in the "include" pattern. In our case, we had the strict flag set to true, but IDEs/editors wouldn't pick that up for test files, because they weren't included in the tsconfig. Our approach was basically to create 3 tsconfig files: `tsconfig.json`, `tsconfig.build.json`, and `tsconfig.test.json`. The build file `include`s the actual source code, the test file `include`s just the tests, and the root `tsconfig.json` uses [typescript project references](https://www.typescriptlang.org/docs/handbook/project-references.html) to reference the build and test configs. The LSP will always read the root `tsconfig.json` and should then be smart enough to pick up all the files with the different compilerOptions from their respective configs. Plus you can set your builds to use the `tsconfig.build.json` and jest to use the `tsconfig.test.json` so that they don't contaminate each other. I've been happy with the results so far. EDIT: minor clarification
LemongrabThree · 2022-04-20 09:12
Weird, I didn't have the language server installed globally at all. I think I initially had to give the LSP a path to `node` and then it installed its own local package. I knew I should have documented the steps I took... Well, next time I need to do mass renaming I'll figure out how to set the tsserver path.
LemongrabThree · 2022-04-20 09:17
Solved.