1

I am attempting to send JSON to Clangd on my desktop using Python. I see in the terminal window that Clangd says:

I[15:52:46.580] Warning: Missing Content-Length header, or zero-length message.
"Content-Length": 53

{
    "jsonrpc": "2.0", 
    "id": 1, 
    "method": "exit"
} 

But that is not the case. Python code:

import argparse
import sys

if __name__ == "__main__":
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--process_num", type=int, help="Clangd process number", required=True)
    parser.add_argument("-d", "--dry_run", action="store_true", help="Dry run")
    
    args = parser.parse_args()
    
    filepath = f"/proc/{args.process_num}/fd/0"
    
    my_content = """\
"Content-Length": 53

{
    "jsonrpc": "2.0", 
    "id": 1, 
    "method": "exit"
}"""
    
    if args.dry_run:
        print(my_content.encode("utf-8"))
        sys.exit(0)
    
    with open(filepath, 'wb') as f:
        f.write(my_content.encode("utf-8"))

Is there something in the formatting I am missing?

Update:

Made changes based on insight from HighCommander:

import argparse
import sys
import json

if __name__ == "__main__":
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--process_num", type=int, help="Clangd process number", required=True)
    parser.add_argument("-d", "--dry_run", action="store_true", help="Dry run")
    
    args = parser.parse_args()
    
    filepath = f"/proc/{args.process_num}/fd/0"
    
    
    my_object = {}
    my_object["jsonrpc"] = "2.0"
    my_object["id"] = 1
    my_object["method"] = "exit"
    
    my_content = json.dumps(my_object).encode()
    
    my_header = "Content-Length: {}\r\n\r\n".format(len(my_content)).encode("ascii")

    
    if args.dry_run:
        print(my_header)
        print(my_content)
        sys.exit(0)
    
    with open(filepath, 'wb') as f:
        f.write(my_header)
        f.write(my_content)

I get the missing Content-Length/zero length message print out from Clangd

4
  • 1
    You might find it helpful to look at an example of a working JSON-RPC implementation in python: github.com/HighCommander4/ls-interact/blob/master/common.py Commented Apr 27 at 1:05
  • @HighCommander4, thanks, unfortunately the issue is still at-hand. Commented Apr 27 at 1:25
  • Can you show how you are invoking clangd? Commented Apr 27 at 4:26
  • Taking another look, I see that you're writing to /proc/<pid>/fd/0 of an existing clangd process. I've never seen it done this way, I've only seen it done in a way where the clangd process is launched by the client as a subprocess. Maybe get it to work that way first, to rule out protocol format issues? Commented Apr 27 at 19:02

1 Answer 1

3

You're not supposed to quote the "Content-Length" , just send Content-Length: 53

Make sure you're using \r\n for the newlines too in the header part, as shown in the Language Server Protocol . Make sure your Content-Length is right as well, the json you posted here isn't 53 bytes,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.