-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feature: Support standalone Rust files #8955
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this!
But let's add the following as a comment on DetachedFiles
, as a sort of extended TODO:
The primary limitation of this approach is that the set of detached files needs to be fixed at the beginning. That's not the end user experience we should strive for. Ideally, you should be able to just open a random detached file in existing cargo projects, and get the basic features working. That needs some changes on the salsa-level though. In particular, we should split the unified CrateGraph
(which currently has maximal durability) into proper crate graph, and a set of ad hoc roots (with minimal durability). Then, we need to hide the graph behind the queries such that most queries look only at the proper crate graph, and fall back to ad hoc roots only if there's no results. After this, we should be able to tweak the logic in reload.rs
to add newly opened files, which don't belong to any existing crates, to the set of the detached files.
if (rustDocuments.length > 0) { | ||
ctx = await Ctx.create(config, context, serverPath, { kind: 'Detached Files', files: rustDocuments }); | ||
} else { | ||
throw new Error("no rust files are opened"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, didn't realize we still have this vscode.workspace.workspaceFolders?.[0]
logic on the client side. It's wrong and should be removed (unrelated to this PR of course)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Time to break the client again, I guess.
What should be the alternative?
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As usual, just don't do it at all :-)
We use workspace folder for two things:
- to set cwd for the server
- in task provider
The serever is not relying on cwd (we are careful with using AbsPaths everywhere), so, presumably, that logic can be removed.
The tasks should be fixed to collect the task from all workspace folders, and not just from the first one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to set cwd for the server
I feel that could be connected with the debug.ts logic (it's a 3rd place where we use vscode.workspace.workspaceFolders
), but I'll give it a try and check how it goes.
Not doing anything in this PR, just in case.
Co-authored-by: Aleksey Kladov <[email protected]>
Agree, that's why I added multiple files into the Meanwhile, the FIXME is added. |
bors r+ |
8995: fix: Create tasks for all workspaces r=matklad a=SomeoneToIgnore Follow-up of #8955 (comment) Before: <img width="593" alt="image" src="https://user-images.githubusercontent.com/2690773/119575267-712b5300-bdbf-11eb-833c-f688f7a7dd0f.png"> After: <img width="643" alt="image" src="https://user-images.githubusercontent.com/2690773/119575273-74264380-bdbf-11eb-8283-a78bbcb7346e.png"> This is the first time I've used multiple workspaces feature in VSCode, but so far looks like * opening detached files works * run and debug lens work * Rust Analyzer: Run action works * run task works and now shows tasks for all workspaces * there are no platform-specific changes involved Co-authored-by: Kirill Bulatov <[email protected]>
Thank you. Thank you so much. I am on the verge of tears. |
Currently the r-a server requires |
How can I, as a rust analyzer user, fix this? I use RA vscode |
I don't think there's anything to fix in my quote, this is the way it works in vscode currently: if you use a server that's new enough, it's able to accept and process this parameter; if you use vscode plugin that's new enough, it's able to send this parameter when a detached file is opened with the If you have any issues with opening a detached file via If you want to support arbitrary detached files being dynamically added to an already open vscode instance, that's not supported currently and will require plenty of changes along the way (protocol extensions to let the server dynamically know about the new files, server side refactoring for better dynamic project update and the feature implementation itself) and won't happen in the near future unless somebody works on that. |
Thank you, the dynamic opening is likely my issue, good to know the solution. |
* Some language servers *cough*rust-analyzer*cough* need an empty/custom workspaceFolders for certain usecases. For example, rust-analyzer needs an empty workspaceFolders table for standalone file support (See rust-lang/rust-analyzer#8955). * This can also be useful for other languages that need to commonly open a certain directory (like flutter or lua), which would help prevent spinning up a new language server altogether. * In case no workspaceFolders are passed, we fallback to what we had before.
…15132) Some language servers *cough*rust-analyzer*cough* need an empty/custom workspaceFolders for certain usecases. For example, rust-analyzer needs an empty workspaceFolders table for standalone file support (See rust-lang/rust-analyzer#8955). This can also be useful for other languages that need to commonly open a certain directory (like flutter or lua), which would help prevent spinning up a new language server altogether. In case no workspaceFolders are passed, we fallback to what we had before.
I have standalone cargo-less files working in coc-rust-analyzer, added dynamically (got just a single r-a instance running for multiple, file-path unrelated, rust files opened after the first one: coc-rust-analyzer has once added support for this newer So I assume r-a works just fine for standalone files when receiving EDIT: See newer test bellow, this one is actually invalid. |
I've experimented with similar thing in VSCode: it can react on any file that's open in the current editor and try to resolve that opened file's path against current workspace, either returning the relative(?) path for workspace files or If I'm not mistaken, you describe the same functionality. But rust-analyzer already has a proper crate metadata for such files, and adding those as standalone files would be weird at best, if not breaking. More complexity adds the fact that VSCode API operates FS pathes whereas current cargo graph (same as the majority things in rust-analyzer) is pretty distant from any FS abstractions: to support a "is this detached file actually from some crate in the tree" check, you'll have to change the server codebase. |
…eovim#15132) Some language servers *cough*rust-analyzer*cough* need an empty/custom workspaceFolders for certain usecases. For example, rust-analyzer needs an empty workspaceFolders table for standalone file support (See rust-lang/rust-analyzer#8955). This can also be useful for other languages that need to commonly open a certain directory (like flutter or lua), which would help prevent spinning up a new language server altogether. In case no workspaceFolders are passed, we fallback to what we had before.
I want to amend the information in the test above, it's invalid, and repeat the test with valid information, I think this is potentially pretty relevant to r-a's support of standalone cargo-less files. r-a seems to actually fit almost the same case as
If you apply this test with (If for some reason the above isn't clear enough, feel free to mail me if you wish a link with a very detailed description of what this is about, which is what made me revisit r-a's case. It may make it clearer where r-a lies in this problem scope) cc @fannheyward |
…eovim#15132) Some language servers *cough*rust-analyzer*cough* need an empty/custom workspaceFolders for certain usecases. For example, rust-analyzer needs an empty workspaceFolders table for standalone file support (See rust-lang/rust-analyzer#8955). This can also be useful for other languages that need to commonly open a certain directory (like flutter or lua), which would help prevent spinning up a new language server altogether. In case no workspaceFolders are passed, we fallback to what we had before.
is there a guide on how to enable all this is vs-code ? no one seems to talk about it on how to actually use it.. |
There's nothing but the ability to open a standalone file with code, as Due to impl limitations described above, all consequent standalone file opens in the same editor won't work. |
Note for neovim users there: you probably should use rust-tools.nvim, as lspconfig won't support this language-specific feature. |
Closes #6388
Caveats:
I've decided to support multiple detached files in the code (anticipating the scratch files), but I found no way to open multiple files in VSCode at once: running
code *.rs
makes the plugin to register in thevscode.workspace.textDocuments
only the first file, while code actually displays all files later.Apparently what happens is the same as when you have VSCode open at some workplace already and then run
code some_other_file.rs
: it gets opened in the same workspace of the same VSCode with no server to support it.If there's a way to override it, I'd appreciate the pointer.
No way to toggle inlay hints, since the setting is updated for the workspace (which does not exist for a single file opened)
In theory, we can detect
rustc
, run it on a file and run the resulting binary, but not sure if worth doing it at this stage.Otherwise imports, hints, completion and other features work.