language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Changes in a symlinked monorepo package are not reflected in other apps

Open AnatoleLucet opened this issue 2 years ago • 11 comments

Describe the bug In a pnpm monorepo, a change made in a Svelte file in a package will not be reflected in apps until the LSP is restarted.

To Reproduce Clone: https://github.com/AnatoleLucet/svelte-lsp-monorepo-issue and install the deps with pnpm.

Both the ui package and the web app are barebone SvelteKit projects.

The ui package exports the Hello.svelte component, which has a prop named word of type string. The web app is importing the ui package and using the Hello.svelte component in src/routes/index.svelte. If I change word's type to a number, the LSP will not reflect this change in the web app. I need to restart the LSP to get the expected Type 'string' is not assignable to type 'number'..

Expected behavior Reflect the changes when another package is changed (as the typescript LSP is doing)

Screenshots

System (please complete the following information):

  • OS: Linux Manjaro 21.2.3
  • IDE: NeoVim 0.6.1 with the lsp-config plugin
  • Plugin/Package: svelte-language-server 0.14.23

Additional context I'm not sure why, but I do not see the issue in VsCode. You can check out the NeoVim Svelte lsp-config here.

AnatoleLucet avatar Mar 17 '22 09:03 AnatoleLucet

Sounds related to #1301 (or maybe even a duplicate of it). The difference to #1301 is that #1301 talks about this in the context of TS files, whereas this is about changes in Svelte files.

dummdidumm avatar Mar 17 '22 09:03 dummdidumm

Indeed it looks like a dup. Feel free to close this issue

AnatoleLucet avatar Mar 17 '22 20:03 AnatoleLucet

But why is it only happening in neovim? I can't quite think of a reason why this would differ from the LSP clients. From my debugging, It does get resolved to the real path in the server.

In VScode the problem is just the diagnostic not being updated in real-time. That's because we only update the diagnostic in other files when the file saves for better performance. But hover would still show the updated type. Not familiar with neovim. Is the file being saved to the fs in your screen recording? Also is this a split-screen of the monorepo root? Or are you opening two instances for each workspace?

jasonlyu123 avatar Mar 18 '22 01:03 jasonlyu123

Is the file being saved to the fs in your screen recording?

Yes. It's barely visible, but I do save the file (FYI, it's when I type :w in the very bottom left corner of the screen).

Also is this a split-screen of the monorepo root? Or are you opening two instances for each workspace?

This is a single instance of neovim - at the root of the monorepo - with two buffers (files) open side-by-side.

Is there anything I can do to help debug this? Maybe spinning up the server in local an adding logs that you might need?

AnatoleLucet avatar Mar 18 '22 08:03 AnatoleLucet

I am also wondering if this problem is os specific? Can you try running this script in the monorepo root and see if it's resolved to the real path?

const fs = require('fs');

console.log(fs.realpathSync.native('apps/web/node_modules/ui/src/lib/Hello.svelte'));

Also, if you're interested in debugging this. You can follow this instruction to start the server under inspect flag. And use chrome or edge to debug. if it hit this line with a symlink path then it's the same problem with 1301. It's just for some reason not resolved like what I saw in vscode. https://github.com/sveltejs/language-tools/blob/5d7f676a8b6649e5ab5eab0ccedcd4733e65450f/packages/language-server/src/plugins/typescript/SnapshotManager.ts#L108

jasonlyu123 avatar Mar 18 '22 10:03 jasonlyu123

fs.realpathSync seems to be able to resolve the correct path

How can I use the language server codebase locally to debug? I see there's a watch script in the package.json. Should I run this and install the local package globally, or is there a better way?

AnatoleLucet avatar Mar 18 '22 13:03 AnatoleLucet

We usually just use vscode to launch the extension together with the server. And use vscode to debug. But since you might want to debug with neovim. You'll have to add a --inspect flag to your server command. Launch the server and then attach the debugger to the node process. Debugger wise, a chrome browser should be the easiest. Or you could clone our repo and use our debugger config (attach) with a --inspect=6009 flag in your server command config to debug on vscode.

I have to find some time to figure out how to set up a neovim with LSP, either on windows or WSL. It probably is the second most used LSP client of svelte language server. but I don't know how to set it up to debug it.

jasonlyu123 avatar Mar 18 '22 13:03 jasonlyu123

I took a bit of time to debug. I'm not familiar with the codebase, so I'm not sure where to look at, but I did check in the SnapshotManager, and the fileName seems to be correct (/tmp/svelte-lsp-monorepo-issue/packages/ui/src/lib/Hello.svelte).

AnatoleLucet avatar Mar 21 '22 12:03 AnatoleLucet

Finally got neovim to work for me. I tried using vsplit to open these two files in split-screen. And there're two instances of the language-server process running.

圖片 圖片

From this config, it seems like neovim/lspconfig would walk through the directory to find a .git or package.json and open an LSP server process at the path. In this case, it isn't related to #1301. And the solution we discussed in that issue won't work for this case. The only solution I can think of is to open a dedicated file watcher for a single file like how the tsserver does. (The change event fires on both the real path file and symlink file)

jasonlyu123 avatar Apr 01 '22 03:04 jasonlyu123

@jasonlyu123

Finally got neovim to work for me.

Do you mind sharing your neovim config? I have almost everything working but I can't get <script lang="ts"> to do proper highlighting.

drio avatar Apr 21 '22 14:04 drio

@drio are you using treesitter?

AnatoleLucet avatar Apr 21 '22 14:04 AnatoleLucet

@jasonlyu123

Finally got neovim to work for me.

Do you mind sharing your neovim config? I have almost everything working but I can't get <script lang="ts"> to do proper highlighting.

Have you added typescript-svelte-plugin to your tsconfig.json?

https://github.com/sveltejs/language-tools/tree/master/packages/typescript-plugin#usage

thenbe avatar Dec 30 '22 12:12 thenbe

I'm in the same problem with my sveltekit project. I'm using typescript-svelte-plugin. Although typescript files (like +page.server.ts) works realtime, diagnostics of .svelte file does not updated.

https://user-images.githubusercontent.com/1560508/237040552-0a1be152-0c2b-45ab-b98c-3e927ffeae34.mov

ryoppippi avatar May 09 '23 08:05 ryoppippi

@ryoppippi This seems unrelated to this issue. This issue is neovim creating multiple instances in monorepo. If you are indeed having this problem, you can try to override the root_dir config to root_pattern(".git") or any file in the root directory.

https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#svelte

jasonlyu123 avatar May 09 '23 08:05 jasonlyu123

OK this is slightly different from Monorepo problem, but the behavior is the same. I'm doing root_pattern( {"package.json", "tsconfig.json", "tsconfig.jsonc", "node_modules"}), but it seems not working

ryoppippi avatar May 09 '23 09:05 ryoppippi

Then it's unrelated.

jasonlyu123 avatar May 09 '23 09:05 jasonlyu123