Skip to content
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

Is socket support works correct? #245

Closed
destitutus opened this issue Aug 29, 2017 · 11 comments
Closed

Is socket support works correct? #245

destitutus opened this issue Aug 29, 2017 · 11 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug

Comments

@destitutus
Copy link

Language server answers nothing if --socket option used. Is implementation broken?

node -v
v6.11.2

Ways to repeat. Install Language server, for example npm install -g ocaml-language-server
Run it via command line ocaml-language-server --socket=3000
Connect via telnet to server

$ telnet
telnet> toggle crlf
Will send carriage returns as telnet <CR><LF>.
telnet> open localhost 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Content-Length: 58

{"jsonrpc":"2.0","id":2,"method":"initialize","params":{}}

It replays nothing on correct request.
If I change line https://github.com/Microsoft/vscode-languageserver-node/blob/master/server/src/main.ts#L1460-L1464

to

let server = net.createServer(socket => {
                server.close();
                output.on('data', function (d) {
                        socket.write(d)
                })
                socket.on('data', function (d) {
                        input.write(d)
                })
            }).listen(port);

it replay with correct response

$ telnet
telnet> toggle crlf
Will send carriage returns as telnet <CR><LF>.
telnet> open localhost 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Content-Length: 58

{"jsonrpc":"2.0","id":2,"method":"initialize","params":{}}
Content-Length: 152

{"jsonrpc":"2.0","id":0,"method":"window/showMessageRequest","params":{"type":2,"message":"Cannot find merlin binary at \"ocamlmerlin\".","actions":[]}}

Is it known issue?

@dbaeumer
Copy link
Member

dbaeumer commented Sep 1, 2017

@aeschli can you please answer. You originally put that in.

@aeschli
Copy link
Contributor

aeschli commented Sep 1, 2017

Sorry, I'm not really the node streams expert. So maybe this never worked?

@dbaeumer dbaeumer added the bug Issue identified by VS Code Team member as probable bug label Sep 5, 2017
@PavelSosin
Copy link

It looks like ALL LSP implementations based on this package have the same issue!

@rcjsuen
Copy link
Contributor

rcjsuen commented Sep 20, 2017

I've tried connecting to the example server in the past and also got nowhere. Please see Microsoft/vscode-languageserver-node-example#37..

@AHaberl
Copy link

AHaberl commented Sep 28, 2017

So will there be a fix for this?

@dbaeumer
Copy link
Member

dbaeumer commented Oct 2, 2017

Fixed. Published as 3.5.0-next.1

Please note that socket connections are only supported when both the client and server run on the same machine.

@dbaeumer
Copy link
Member

dbaeumer commented Oct 2, 2017

@AHaberl usually a PR helps as well :-)

@rcjsuen
Copy link
Contributor

rcjsuen commented Oct 7, 2017

@dbaeumer I don't understand your fix. The code for creating a server socket uses Net's createConnection function which is used for creating a socket that connects to the specified port. The server should be listening for connections on the specified port.

https://github.com/Microsoft/vscode-languageserver-node/blob/d40191a5a430a4362d6c19610d800eb7f7a95959/jsonrpc/src/socketSupport.ts#L39-L45

The server will not start because it tries to connect to port 3000 instead of listening on port 3000.

image

@rcjsuen
Copy link
Contributor

rcjsuen commented Oct 7, 2017

I have opened #260 with a proposed fix that corrects the original problem that was caused by the pipes reading and writing in the wrong direction.

@dbaeumer
Copy link
Member

@rcjsuen I forgot to add some documentation on how this is supposed to work:

To avoid any timing or port issues the pipe and the sockets are owned by the client processes since it is already running. The server process simply connects to the pipe / socket. In node term the VS Code process calls createServer, then starts the server process, waits until the server process has connected to the pipe / socket and then signals that the connection has been established and messages can be send back and forth.

I will update the documentation to make this clear for users that implement a server in a different programming language.

@rcjsuen
Copy link
Contributor

rcjsuen commented Oct 12, 2017

Thank you for your clarification, @dbaeumer. I see now that the client is supposed to listen for connections on the specified port and the language server should be started up to connect to the specified port.

I hope the following code will be of help to someone.

import * as net from "net"
import * as child_process from "child_process"

const server = net.createServer((socket) => {
    // close the server and don't accept anymore connections
    server.close();
    socket.on("data", (message) => {
        console.log(message.toString());
    });
    initialize(socket);
});
server.listen(3000, () => {
    // ask the language server to connect to port 3000 once the setup has completed
    child_process.spawn("node", [ "out/src/server.js", "--socket=3000" ]);
});

let messageId = 1;

function send(socket: net.Socket, method: string, params: object) {
    let message = {
        jsonrpc: "2.0",
        id: messageId++,
        method: method,
        params: params
    };
    let json = JSON.stringify(message) + "\n";
    let headers = "Content-Length: " + json.length + "\r\n\r\n";
    socket.write(headers, "ASCII");
    socket.write(json, "UTF-8");
}

function initialize(socket: net.Socket) {
    send(socket, "initialize", {
        rootPath: process.cwd(),
        processId: process.pid,
        capabilities: {
            /* ... */
            workspace: {

            }
        }
    });
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

6 participants