nwolverson / purescript-language-server

MIT License
183 stars 41 forks source link

Language server crashes constantly #167

Closed carbolymer closed 2 years ago

carbolymer commented 2 years ago

I've configured my vim using LanguageClient-neovim + vimmer-ps to use purescript-language-server, however it crashes constantly with the following error:

Error: connect ECONNREFUSED ::1:15784
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 15784
}

Full error.log: LanguageClient_stderr.log purescript-language-server version: 0.16.1 from https://aur.archlinux.org/packages/purescript-language-server/ 0.15.3 also doesn't work, but the error is slightly different:

/usr/lib/node_modules/purescript-language-server/server.js:2955
                  throw util.fromLeft(step);
                  ^

Error: connect ECONNREFUSED ::1:15651
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 15651
}

Node.js v17.2.0

The language server (configured by vimmer-ps) is started using the following command:

purescript-language-server --stdio --config '{"purescript":{"buildCommand":"spago build --purs-args --json-errors","addPscPackageSources":false,"pscIdePort":null,"autoStartPscIde":true,"pursExe":"purs","addNpmPath":true,"autocompleteAddImport":true,"addSpagoSources":true}}'

Which still results in ECONNREFUSED error.

Any ideas what's missing here?

Issue in vimmer-ps repo: https://github.com/sriharshachilakapati/vimmer-ps/issues/6

carbolymer commented 2 years ago

Seems that purescript-language-server always tries to connect whenever I pass --config: This command waits:

purescript-language-server  --stdio

This command fails with an error:

purescript-language-server --config '{"purescript":{}}' --stdio
nwolverson commented 2 years ago

The ECONNREFUSED here is the connection to the purs ide server process that has been started up. It looks like a connection is being refused via the IPv6 loopback address on port 15651. Not sure why it's using that not the IPv4 loopback, we don't specify & node is making that decision (https://github.com/kritzcreek/purescript-psc-ide/blob/b9b1d0320204927cafefcf24b105ec03d0ae256b/src/PscIde.js#L10)

As an aside, we should try to get vimmer-ps/the underlying lsp client updated from using --config if possible, ideally via supporting workspace/configuration request or failing that workspace/didChangeConfiguration push on startup

carbolymer commented 2 years ago

@nwolverson regarding IPv6, https://old.reddit.com/r/ipv6/comments/qbr8jc/nodejs_finally_prefers_ipv6_addresses_over_ipv4/ it seems that it is a new default in node 17. Nonetheless, on node 16 I'm still getting ECONNREFUSED on IPv4.

Regarding not using --config, I've tried putting config directly in stdin, but it seems that it still tries to listen on the socket, despite providing --stdio argument: https://github.com/sriharshachilakapati/vimmer-ps/issues/6#issuecomment-986067342

nwolverson commented 2 years ago

--stdio refers to the communication between the lsp client and server, as per my previous comment the connection in question is from purescript-language-server to the purs ide server process (ie the compiler's IDE server process). I'm guessing this process hasn't spawned correctly, hence the connection refused - that or there is some kind of networking thing going on.

But this shouldn't kill the language server, that would be a bug.

Can you check the lsp logs provided by your client - there should be info messages logged from the language server that should give some more information.

Please ignore the comment about --config, it seems that has confused the issue. I'll open a future issue to follow that up.

DrJackilD commented 2 years ago

I want to confirm having the same problem.

Node version: v17.2.0 Npm version: 8.1.4 Neovim version: 0.6.0 LunarVim version: rolling purs version: 0.14.5 purescript-language-server version: 0.16.1

Here is the issue I opened in NeoVim repo, because I thought that's a problem with Nvim LSP client, but looks like it's not: https://github.com/neovim/neovim/issues/16581

And here is the LSP debug log: https://gist.github.com/DrJackilD/8f08767aded0d6b96c42ac7dcc11d797

Here we could see the same problems as @carbolymer mentioned:

{  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = "Retrying starting server after 500ms: Error: connect ECONNREFUSED ::1:15980\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)",    type = 3  }}
DrJackilD commented 2 years ago

I also see a few warnings regarding the codeLens/refresh, not sure is it something important or not

[WARN][2021-12-11 03:25:16] .../lua/vim/lsp.lua:744 "server_request: no handler found for"  "workspace/codeLens/refresh"

UPD: As an additional info - purs ide is still running after the purescript language server is failed

DrJackilD commented 2 years ago

I tried to start the purescript-language-server second time, but this time passes the port of already started purs ide (like --config '{"purescript: {..., "pscIdePort":15194,...}}) and it works, it started ok. Maybe it's just enough to add some progressive retries for the connection call?

nwolverson commented 2 years ago

There are already retry attempts for the initial startup connect:

https://github.com/nwolverson/purescript-language-server/blob/master/src/LanguageServer/IdePurescript/Main.purs#L244

But I see from the log this message 6 times:

[DEBUG][2021-12-11 03:16:07] .../vim/lsp/rpc.lua:451 "rpc.receive" { jsonrpc = "2.0", method = "window/logMessage", params = { message = "Retrying starting server after 500ms: Error: connect ECONNREFUSED ::1:15980\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)", type = 3 }}

After 3s the thing should be well past started.

purs ide is still running after the purescript language server is failed

Firstly thats odd as the OS should kill it. In any case, can you verify the port matches up (-p 15980 arg on on purs ide server in the above example) and that it is in fact listening on that port (eg run purs ide client -p 15980 which will make a connection to it)

DrJackilD commented 2 years ago

@nwolverson so, after I opened any *.purs file, I've got this error in NeoVim: Screenshot 2021-12-11 at 23 07 40

After that I ran ps aux | rg purs and got this:

drjackild        82668   0.0  0.1 1078073592  20792 s002  S+   11:08PM   0:00.86 /Users/drjackild/pet-projects/purescript/fp-book/node_modules/.bin/purs ide server -p 15799 --output-directory output src/**/*.purs .spago/bifunctors/v5.0.0/src/**/*.purs .spago/console/v5.0.0/src/**/*.purs .spago/const/v5.0.0/src/**/*.purs .spago/contravariant/v5.0.0/src/**/*.purs .spago/control/v5.0.0/src/**/*.purs .spago/distributive/v5.0.0/src/**/*.purs .spago/effect/v3.0.0/src/**/*.purs .spago/either/v5.0.0/src/**/*.purs .spago/exists/v5.1.0/src/**/*.purs .spago/foldable-traversable/v5.0.1/src/**/*.purs .spago/functors/v4.1.1/src/**/*.purs .spago/identity/v5.0.0/src/**/*.purs .spago/invariant/v5.0.0/src/**/*.purs .spago/lazy/v5.0.0/src/**/*.purs .spago/lists/v6.0.1/src/**/*.purs .spago/maybe/v5.0.0/src/**/*.purs .spago/newtype/v4.0.0/src/**/*.purs .spago/nonempty/v6.1.0/src/**/*.purs .spago/orders/v5.0.0/src/**/*.purs .spago/partial/v3.0.0/src/**/*.purs .spago/prelude/v5.0.1/src/**/*.purs .spago/profunctor/v5.0.0/src/**/*.purs .spago/psci-support/v5.0.0/src/**/*.purs .spago/refs/v5.0.0/src/**/*.purs .spago/safe-coerce/v1.0.0/src/**/*.purs .spago/tailrec/v5.0.1/src/**/*.purs .spago/tuples/v6.0.1/src/**/*.purs .spago/type-equality/v4.0.0/src/**/*.purs .spago/unfoldable/v5.0.0/src/**/*.purs .spago/unsafe-coerce/v5.0.0/src/**/*.purs src/**/*.purs test/**/*.purs

Next, I ran this command:

purs ide client -p 15799 

and console is just hanging, waiting for some commands, probably. When I send anything, like {} I've got immediate:

purs: <socket: 47>: Data.ByteString.hGetLine: end of file

And after that, the process is not running anymore. I could reproduce this anytime, I just need to run LspStart again from the NeoVim and all is working exactly the same (except that ports is different each time)

Hope this is help

DrJackilD commented 2 years ago

Ah, so I checked out logs again and it looks like this purs:

Found existing port from file: <any port here>

is actually the started one, but then the lsp client did not connect to it and tries to start another one, which won't be started

DrJackilD commented 2 years ago

Again, here is another logs: https://gist.github.com/DrJackilD/6bf84757c2095f12935fd6588a3974b5

And purs with port 15277 is actually started with NeoVim

DrJackilD commented 2 years ago

Alright, I found a workaround (not sure it works correctly, tho 😄)

So, I just bind the constant port in the LSP startup settings:

pscIdePort=15702

And now after I open any PS document LSP fails one time, but then after I start it a second time successfully connect to purs server. But I'm not sure it working correctly because:

  1. I didn't see any LSP suggestions in the dropdowns
  2. Import suggestions doesn't work either
  3. Go to definition and so on doesn't work either

UPD: It looks like nothing are working, but textDocument/formatting. So, I believe problem in the purs ide itself because formating should work through purty, right?

nwolverson commented 2 years ago

This message is a little misleading at the moment:

Found existing port from file:

So a port is cached in .psc-ide-port for what should be an existing running server (and that file should also be removed on exit), but unless it's followed by "Found IDE server" a test connection failed.

This message:

purs: <socket: 47>: Data.ByteString.hGetLine: end of file

does not seem right, if I connect to an lsp created port and also input {} I get this:

{"result":"Parsing the command failed with:\nError in $: key \"command\" not found\nCommand: {}","resultType":"error"}

It looks like the change on node to use IPv6 is problematic as purs ide server only listens on the IPv4 loopback. I'll make a change to explicitly use 127.0.0.1

DrJackilD commented 2 years ago

@nwolverson sounds great, thanks you! Will wait for this fix

UPD: I downgraded my node version to 14.18.2 - working fine now. So, that's could be a workaround while waiting for the fix

nwolverson commented 2 years ago

I just published 0.16.2 which should now force a IPv4 connection to purs ide server.

Can you check this resolves the issue?

carbolymer commented 2 years ago

Works perfectly. Thanks a lot! Screenshot_2021-12-21_13-52-40

DrJackilD commented 2 years ago

Works like a charm, thank you!