rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.16k stars 1.58k forks source link

Recommended setup for multiple (dependent) crates #18270

Open krobelus opened 1 day ago

krobelus commented 1 day ago

Sometimes I'm working on a Rust crate and use textDocument/definition to peek into the standard library (or other libraries). nvim-lspconfig has some fairly complex logic to use the root of the original (calling) crate for files inside library crates. The main reason for this is to avoid getting diagnostics from the stdlib. (I think there are also downsides; it looks like rust-analyzer is put into single-file mode for stdlib files, which probably makes it less useful.) Non-library crates get their own root.

Is this the recommended approach? It would be nice to simplify this somehow though I don't know how. It's odd that the config needs to know so much about Rust.

Also I'm not sure what workspace/didChangeWorkspaceFolders is used for. In my testing it doesn't seem to make a difference.

flodiebold commented 19 hours ago

The intended usage is just that you have a workspace where the code that you want to edit is, you run rust-analyzer on that workspace, and if go to definition takes you to dependencies outside that workspace that's fine, RA knows about those files too. You shouldn't try to dynamically add workspace folders for them or start a separate RA instance. I think what complicates it is that nvim doesn't have the same concept of a workspace, so I guess it tries to dynamically determine whether to start a new RA instance when you open another file. Emacs has I think a similar problem, and it handles this is AIUI by 1. having the user explicitly define the workspace folders (asking them when opening a file that's not in any known workspace), and 2. when using go to definition, it automatically reuses the LSP server of the file you're coming from instead of trying to start a new one.

krobelus commented 6 hours ago

Thanks. I think for now, I'll restrict an editor session to a single workspace (I guess whichever Rust file comes first). Multi-workspace support can be done later, it doesn't seem super important since dependant crates already work without it.

You shouldn't try to start a separate RA instance

Right. I wonder how can an LSP client tell (without knowledge of particular servers) if it can reuse the same server instance or needs to start a new one. So far I'm using the workspaceFolders capability.. which seems kinda wrong.