Make direct request to Rust Analyzer

I'm trying to integrate Rust Analyzer into Rust Explorer playground.

My first step is to get a valid response from Rust Analyzer directly from a terminal. So I ran

$ rust-analyzer

and then followed by an initialization request

> Content-Length: 221\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)

Can anyone point out what's wrong with my request?

Did you type a literal \r\n\r\n? You need a carriage return followed by a newline and then another carriage return and linefeed. A carriage return is not a character you can enter using your keyboard, so I don't think you can run rust-analyzer directly in a terminal. You need to write a program that interacts with it.

2 Likes

Thank you so much. You saved my day.

In theory, you can, actually: '\r' = character code 13 = control-M.

ASCII and Unicode characters 1 through 13 correspond to control-A through control-Z. Not only that, as a legacy from the actual-terminal-keyboard days, the keyboard “enter” key (or “return” on Apple hardware) corresponds to control-M.

However, this isn't quite sufficient, because unless the tty (kernel object that sits between the terminal emulator and the program accepting input) is in “raw” mode, some of them are given special meaning before they get to the program, like control-C produces a signal, and control-M ends up being converted into '\n', code 10, control-J.

(This is true of Unix-style systems; the details on Windows are different and I can't tell you what the story of typing them is.)

2 Likes