-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Proper support for standalone/detached files #14318
Comments
It might also make sense to adding a syntax for declaring dependencies inline via comments in detached files. It will also helps for #9343 as evcxr currently handles dependencies in jupyter notebooks via |
We can't really support that, to resolve dependencies we need cargo which in turns needs a proper cargo workspace |
We probably need to have a mirror of the project in a temp directory with a "normal" project structure at that point, which is what evcxr is doing if I understand correctly. |
rust-lang/rfcs#3424 will be interesting to us here. Unsure how we can support this but we probably would need to handle it similar to build scripts, that is we analyze the file without dep info first/invoke it initially with cargo metadata once, and afterwards keep track of the dependencies comment. If it changes re-run metadata. |
Is anyone working on this? If not, I'd love to pick it up. |
No one is working on this right now, though it's also not quite clear how to tackle this. I personally don't really understand the idea described in the comment tbh, I took a look at this at some point and the comment didn't really make sense to me in terms of how things are working in r-a right now. |
I did a little bit of experimentation and the way I found that this currently works (tried it on Neovim with lspconfig) is like: local lsp_conf = require('lspconfig')
lsp_conf.rust_analyzer.setup {
-- Other setup stuff
-- ...
-- To be able to start rust-analyzer at all a root_dir is needed,
-- I added a star match for testing purposes to match on any file
-- This could be an lspconfig/Neovim only issue
root_dir = lsp_conf.util.root_pattern("*"),
-- LSP Client needs to know the detached files ahead of time on
-- initialization and pass them to the r-a on startup
init_options = { detachedFiles = { "/tmp/file.rs" } }
} r-a Client for VSCode does something similar here. And the PR for the initial support is #8955 |
What I currently do not understand yet why would this require a change on the salsa-level(does this mean on the upstream salsa crate?). The way I understand this it would require to hide the I would guess at the time of writing |
Maybe @matklad can explain what they meant with the comment there since it was written by them |
This has to do with durabilities : https://rust-analyzer.github.io/blog/2023/07/24/durable-incrementality.html if we add an ability to open files in an ad-how way, we should make sure that opening such a file does not require to revalidate the entire graph of queries. This is in contrast to assign a new dependency via right now, each detached file is treated as a separate crate, and all crates are a part of a singleton crate graph query.s thwt means that:
To fix this, we need to go from a single crate graph query to something more fine grained, so that we can add new detached files without disturbing Durable parts of the query graph. |
Opening a detached file (in case of cargo scripts) is going to change the crate graph anyway. One thing we can do is separating the query graphs of workspaces. Currently Opening a detached file as a cargo script is as slow as opening a new cargo project, and I don't see how preventing things being invalidated will make it fast. By opening a new cargo script, we will discover a whole new cargo toml which some of its dependencies might not be even downloaded yet, I don't think any change in salsa can make that fast. That said, we may still be able to do something. We can try to detect which files are going to be opened in the detached mode (for example by looking at the shebang) and constructing the correct project workspace and crate graph from the beginning. It won't help in opening random files from random places (and I don't think we can do anything for that), but will help in projects that are multiple detached files in a folder, similar to the rustling. |
While the crate graph issues is problematic in terms of perf, I think it should be fine for us to skip that optimization for now (at least in favor of cargo script workspaces). The main issue with these detached files / cargo script workspaces are that we cannot eagerly support them, as we would need to built the full module graph for the project to figure out if a file is detached or not which is too much up front work. So at best we can support these lazily, which while not perfect is still decent I think. That is, once such a file is opened we can permanently add it to the known workspaces (I wonder if we should do the same for undiscovered workspaces). |
Small note about the issue description: Rustlings wouldn't benefit from this anymore after the release of version 6. It uses an automatically updated |
@mo8it Congratulations on releasing Rustlings 6.0! Depending on your interest/desire to maintain that Cargo.toml file, #17246 might be helpful here, as that'd make finding/loading (rust-analyzer still very much needs better standalone file support for the reasons I described in #17537.) |
rust-analyzer/crates/project-model/src/workspace.rs
Lines 86 to 96 in 9fca0a4
Fixing this would be beneficial to the rustlings project, removing the need for the rust-project.json generation step they currently employ.
The text was updated successfully, but these errors were encountered: