Reputation: 719
I'm trying to integrate Rust Analyzer with a browser based editor.
My first step is to run Rust Analyzer directly from a terminal and send requests via stdio.
$ rust-analyzer
> Content-Length:207\r\n\r\n{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}
But I got this error:
[ERROR rust_analyzer] Unexpected error: expected initialize request, got Err(RecvError)
expected initialize request, got Err(RecvError)
What am I missing here?
Upvotes: 3
Views: 979
Reputation: 16785
It's just because the four literal characters \
r
\
n
are not transformed into the two special characters \r
and \n
when you input them directly in the terminal.
In order to experiment by hand in the terminal, you should transform the line ending.
$ sed -u -re 's/^(.*)$/\1\r/' | rust-analyzer
Content-Length: 207
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}
... then the response is displayed here ...
Note that we press the Enter
key here; we do not try to input the \n
special character.
And on a second thought, I think that in this case (interactive terminal) you should provide Content-Length: 209
because the json content will be ended with \r\n
(two more bytes, ignored as separators).
This way, the next request can be input on the next line.
If you keep 207
, then your next request should start on the same line as the json content (right after the last }
).
Another solution would be to change the settings of the terminal.
stty -icrnl
makes the Enter
key produce the \r
(control-M) character; you then have to input control-J to produce the \n
character.
$ stty -icrnl
$ rust-analyser
Content-Length: 208^M <-- Enter + control-J
^M <-- Enter + control-J
{"jsonrpc":"2.0", ... ,"capabilities":{}}} <-- control-J
... the response is displayed here ...
Upvotes: 4