Stream: t-compiler/rust-analyzer

Topic: Improving the experience on Sublime Text


Kon / GolDDranks (May 08 2020 at 14:42, on Zulip):

Hi, I've been using rust-analyzer for few weeks now on Sublime Text with the LSP plugin. I noticed some cases where the plugin wouldn't work and even some cases where it would crash. I decided to try and debug the problems. First of all, my problem manifests when editing a single file, stand-alone (no external deps except for stdlib) .rs file with no associated Cargo.toml. What is the expected behaviour of Rust-Analyzer in this case?

Laurențiu (May 08 2020 at 15:02, on Zulip):

Files outside of a project/workspace are not supported, so it's expected to not start.

Kon / GolDDranks (May 08 2020 at 15:05, on Zulip):

I see, thanks!

Kon / GolDDranks (May 08 2020 at 15:08, on Zulip):

Btw. are project/workspace-less files not supported at the LSP level, or is it a current limitation of Rust-analyzer?

Kon / GolDDranks (May 08 2020 at 15:53, on Zulip):

So, after trying all kinds of things, I found out that I can trigger a crash in cases where there exists a project structure, with Cargo.toml and all, but I open "just" the source file itself with subl src/main.rs. Now that I can reliably reproduce it, I'll try and debug it.

Kon / GolDDranks (May 08 2020 at 15:57, on Zulip):

I don't know if it's a bug in Sublime's LSP or in Rust-analyzer, but I'll try and fix it nevertheless.

Kon / GolDDranks (May 08 2020 at 16:43, on Zulip):

Allright, I managed to get my hands to the LSP <-> Rust-Analyzer log so I can try to make sense of it. However, I thought that I could, for interactive debugging, also start rust-analyzer from command line and copy-paste messages to it, but I'm having another problem here

bjorn3 (May 08 2020 at 16:46, on Zulip):

Pyry Kontio said:

Btw. are project/workspace-less files not supported at the LSP level, or is it a current limitation of Rust-analyzer?

limitation of rust-analyzer

Kon / GolDDranks (May 08 2020 at 16:46, on Zulip):

Trying to paste the original initialize message, but it manages to paste only 1024 characters before freezing (ctrl-C helps to kill it). Sounds like a buffering thing?

bjorn3 (May 08 2020 at 16:47, on Zulip):

that may be a terminal issue

bjorn3 (May 08 2020 at 16:47, on Zulip):

try putting everything you want to send in a file and then do cat file | rust-analyzer

Kon / GolDDranks (May 08 2020 at 19:14, on Zulip):

Hmm, I'm getting a ProtocolError, and I realized that LSP's "payload log" doesn't log the communication verbatim but indeed, just the "payloads" so I can't easily copy-paste the messages for reference

Kon / GolDDranks (May 08 2020 at 19:15, on Zulip):

So, I need either to re-generate the surrounding headers + JSON-RPC-messages by hand or do something more clever.

Kon / GolDDranks (May 08 2020 at 19:17, on Zulip):

I tried to search Rust-analyzer repo for tests that would have sample files for the protocol but I couldn't find any. Is the closest one you're doing to integration testing, these ones? https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/rust-analyzer/tests/heavy_tests/main.rs

Kon / GolDDranks (May 08 2020 at 19:17, on Zulip):

No tests that send raw data verbatim to the rust-analyzer binary?

Laurențiu (May 08 2020 at 19:17, on Zulip):

Pyry Kontio said:

So, after trying all kinds of things, I found out that I can trigger a crash in cases where there exists a project structure, with Cargo.toml and all, but I open "just" the source file itself with subl src/main.rs. Now that I can reliably reproduce it, I'll try and debug it.

I don't think that works

Laurențiu (May 08 2020 at 19:17, on Zulip):

It didn't work for me in Code when I tried it earlier

Kon / GolDDranks (May 08 2020 at 19:18, on Zulip):

Works in the sense that you don't think Rust-analyzer works or my way of debugging doesn't work?

Kon / GolDDranks (May 08 2020 at 19:19, on Zulip):

I thought that surely this isn't a desirable outcome so I'd figure should debug it スクリーンショット-2020-05-09-4.19.04.png

Laurențiu (May 08 2020 at 19:20, on Zulip):

image.png

Kon / GolDDranks (May 08 2020 at 19:20, on Zulip):

Ah, in that sense! Yes, I'd appreciate an error message like that.

Kon / GolDDranks (May 08 2020 at 19:20, on Zulip):

Instead of a crash.

Kon / GolDDranks (May 08 2020 at 19:21, on Zulip):

So, let's fix that!

Laurențiu (May 08 2020 at 19:21, on Zulip):

Got it :-)

Laurențiu (May 08 2020 at 19:22, on Zulip):

Maybe Sublime doesn't show the error? I mean, it does crash.

Kon / GolDDranks (May 08 2020 at 19:29, on Zulip):

Looking at the log, it might be upon LSP: Rust-analyzer does send this: "window/showMessage: {'message': 'rust-analyzer failed to discover workspace, no Cargo.toml found, dirs searched: /Applications/Sublime Text.app/Contents/MacOS', 'type': 1}"

Kon / GolDDranks (May 08 2020 at 19:30, on Zulip):

Then it sends this: "$/progress: {'token': 'rustAnalyzer/startup', 'value': {'message': 'rust-analyzer loaded, 1 packages', 'kind': 'end'}}", but the LSP plugin doesn't like that: "LSP: unknown $/progress token: rustAnalyzer/startup"

Kon / GolDDranks (May 08 2020 at 19:33, on Zulip):

After that, once I change something, the plugin sends a textDocument/didChange message, and RA panicks with "Error: LspError { code: -32900, message: "Rust file outside current workspace is not supported yet." }"

Kon / GolDDranks (May 08 2020 at 19:35, on Zulip):

Haven't delved into the protocol yet, so I haven't got much of a clue what is to be expected and what is wrong. I'm gonna sleep now but continuing tomorrow! If somebody has any words of wisdom, I'm all ears!

Laurențiu (May 09 2020 at 06:33, on Zulip):

Progress reporting is a newer LSP feature: https://microsoft.github.io/language-server-protocol/specification#progress

Laurențiu (May 09 2020 at 07:34, on Zulip):

It's likely that Sublime doesn't support it yet. That's fine, since there's support in the protocol for exposing client capabilities, but rust-analyzer doesn't respect all of them yet. I filed https://github.com/rust-analyzer/rust-analyzer/issues/4384 for this specific case.

Kon / GolDDranks (May 09 2020 at 17:32, on Zulip):

So, I looked into the communication between the LSP plugin and RA. Actually, the LSP plugin does list the workDoneProgress capability, so it supports capabilities. The error message "LSP: unknown $/progress token: rustAnalyzer/startup" seems to about that RA hasn't earlier registered a token named "rustAnalyzer/startup" to track progress of.

Kon / GolDDranks (May 09 2020 at 17:34, on Zulip):

But that is not the reason why it crashes, the LSP plugin reports about a crash just because RA panics:
Error: LspError { code: -32900, message: "Rust file outside current workspace is not supported yet." }
thread '<unnamed>' panicked at 'called Result::unwrap() on an Err value: "SendError(..)"', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/lsp-server-0.3.1/src/stdio.rs:29:13

Kon / GolDDranks (May 09 2020 at 17:36, on Zulip):

It sends a "window/showMessage" notification about the unsupported case before, but maybe it should send an error response instead of a succesful init message when the init message passes "workspaceFolders":null to it

Kon / GolDDranks (May 09 2020 at 17:39, on Zulip):

Ah, when opening it with proper workspace, it sends the window/workDoneProgress/create message.

Kon / GolDDranks (May 09 2020 at 17:40, on Zulip):

I wonder how big a project would it be to implement a support for workspace/projectless, stand-alone files

Kon / GolDDranks (May 09 2020 at 17:40, on Zulip):

This usecase interests me because I do it often these days

Kon / GolDDranks (May 09 2020 at 19:09, on Zulip):

Correction: supports capabilities → supports the proggress capability

Kon / GolDDranks (May 10 2020 at 05:02, on Zulip):

So, I read through the developer documentation. The core system seems very well thought of! It seems that basically all the work that needs to be done is concentrated in https://github.com/rust-analyzer/rust-analyzer/tree/master/crates/rust-analyzer

Kon / GolDDranks (May 10 2020 at 05:03, on Zulip):

I'm going to look through that codebase now

Laurențiu (May 10 2020 at 07:26, on Zulip):

See also https://github.com/rust-analyzer/rust-analyzer/issues/3715

Kon / GolDDranks (May 10 2020 at 09:10, on Zulip):

So... there is a comment FIXME: support dynamic workspace loading. in main_loop.rs. So of the LSP connection is established and no WS roots are given at beginning, there is no support currently for adding them later? I guess that's a major prerequisite for opening single files, or even files that belong to a cargo project but the project directory being unknown to the editor.

Kon / GolDDranks (May 10 2020 at 09:14, on Zulip):

The first point in time RA could infer the workspace, if not provided by the editor at start, is on textDocument/didOpen request

Kon / GolDDranks (May 10 2020 at 09:37, on Zulip):

Hmm, I have to be careful not to mix up LSP and RA concepts too much. Please tell me if my understanding is correct here: LSP used to have a concept rootPath / rootUri, and a later addition workspaceFolders to support multi-folder workspaces. However, the textDocument/didOpen requests are absolute URLs and may be not strictly contained to workspaces? Is LSP expected to "add" a workspace for opened documents or are there workspaceless documents?

Kon / GolDDranks (May 10 2020 at 09:41, on Zulip):

Then, RA has its VFS, and source roots, as described here: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/guide.md#source-roots-aka-filesystems-are-horrible. Is this part still up to date, there were a bit of warning that this guide might be outdated. I guess that we need to support dynamically adding source roots, right? Is that currently supported?

Kon / GolDDranks (May 10 2020 at 09:43, on Zulip):

I'll try and read the source how textDocument/didOpen is currently handled!

Florian Diebold (May 10 2020 at 10:28, on Zulip):

yeah, I think these kinds of things are mostly blocked on dynamic workspace configuration, which in turn is blocked on improving the VFS

Kon / GolDDranks (May 10 2020 at 11:07, on Zulip):

All right. Btw. I think that the name of this thread is not very accurate if it comes to improving dynamic workspace configuration. Seems that if that gets better, my problems with Sublime automatically get better too. I'm opening a new thread.

Last update: Jul 24 2021 at 20:00UTC