joaotavora / eglot

A client for Language Server Protocol servers
GNU General Public License v3.0
2.22k stars 203 forks source link

Again the problem of Working with OmniSharp: [jsonrpc] Server exited with status 9 #1060

Closed blldw closed 1 year ago

blldw commented 1 year ago

For the working problem of eglot package with OmniSharp server, I followed the following threads, and neither could solve my problem: https://github.com/joaotavora/eglot/issues/573 https://github.com/joaotavora/eglot/issues/241

My OS is Ubuntu 20.04 and Emacs version is 29.0.50.

Following these steps for a Minimum Reproducible Example: (1) Install dotnet-sdk-6.0: sudo apt install dotnet-sdk-6.0. (2) Download latest OmniSharp version from this link:
https://github.com/OmniSharp/omnisharp-roslyn/releases/download/v1.39.1/omnisharp-linux-x64-net6.0.zip and extract it to /opt/omnisharp-roslyn, then add this path to PATH environment variable. (3) Create a C# project:

  mkdir ~/tmp &&  cd ~/tmp
  dotnet new console -o Hello

(4) Start Emacs with an empty ~/.emacs.d directory. (5) run these commands:

  M-x package-refresh-contents
  M-x package-install RET csharp-mode RET
  M-x package-install RET eglot RET

(6) Rerun Emacs and open ~/tmp/Hello/Program.cs, then typeM-x eglot, Emacs will prompt in the minibuffer,

  [eglot] Sorry, couldn't guess for `csharp-mode'!
  Enter program to execute (or <host>:<port>):

and type OmniSharp -stdio -lsp, now the problem occurs.

In the Messages buffer, notable text was:

[eglot] Waiting in background for server `EGLOT (Hello/csharp-mode)'
[jsonrpc] Server exited with status 9
Error running timer: (error "[eglot] Timed out")

While in the eglot events buffer, text was:

[client-request] (id:1) Fri Sep 23 22:39:22 2022:
(:jsonrpc "2.0" :id 1 :method "initialize" :params
      (:processId 10031 :rootPath "/home/jason/tmp/csharp/Hello/" :rootUri "file:///home/jason/tmp/csharp/Hello" :initializationOptions #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data
                                                                              ())
              :capabilities
              (:workspace
               (:applyEdit t :executeCommand
                   (:dynamicRegistration :json-false)
                   :workspaceEdit
                   (:documentChanges :json-false)
                   :didChangeWatchedFiles
                   (:dynamicRegistration t)
                   :symbol
                   (:dynamicRegistration :json-false)
                   :configuration t)
               :textDocument
               (:synchronization
            (:dynamicRegistration :json-false :willSave t :willSaveWaitUntil t :didSave t)
            :completion
            (:dynamicRegistration :json-false :completionItem
                          (:snippetSupport :json-false)
                          :contextSupport t)
            :hover
            (:dynamicRegistration :json-false :contentFormat
                          ["plaintext"])
            :signatureHelp
            (:dynamicRegistration :json-false :signatureInformation
                          (:parameterInformation
                           (:labelOffsetSupport t)
                           :activeParameterSupport t))
            :references
            (:dynamicRegistration :json-false)
            :definition
            (:dynamicRegistration :json-false :linkSupport t)
            :declaration
            (:dynamicRegistration :json-false :linkSupport t)
            :implementation
            (:dynamicRegistration :json-false :linkSupport t)
            :typeDefinition
            (:dynamicRegistration :json-false :linkSupport t)
            :documentSymbol
            (:dynamicRegistration :json-false :hierarchicalDocumentSymbolSupport t :symbolKind
                          (:valueSet
                           [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26]))
            :documentHighlight
            (:dynamicRegistration :json-false)
            :codeAction
            (:dynamicRegistration :json-false :codeActionLiteralSupport
                          (:codeActionKind
                           (:valueSet
                        ["quickfix" "refactor" "refactor.extract" "refactor.inline" "refactor.rewrite" "source" "source.organizeImports"]))
                          :isPreferredSupport t)
            :formatting
            (:dynamicRegistration :json-false)
            :rangeFormatting
            (:dynamicRegistration :json-false)
            :rename
            (:dynamicRegistration :json-false)
            :publishDiagnostics
            (:relatedInformation :json-false :codeDescriptionSupport :json-false))
               :experimental #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data
                           ()))))
[internal] Fri Sep 23 22:39:52 2022:
(:message "Connection state changed" :change "已杀死\n")

----------b---y---e---b---y---e----------
[stderr] 
[stderr] 
[stderr] nil
[stderr] nil
[stderr] Process EGLOT (Hello/csharp-mode) stderr finished

However, if I just run OmniSharp -lsp (without -stdio option), the eglot event buffer output more text compared with OmniSharp -stdio -lsp:

//... previous text
               :experimental #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data
                           ()))))
[server-notification] Fri Sep 23 22:48:23 2022:
(:jsonrpc "2.0" :method "window/logMessage" :params
      (:type 3 :message "OmniSharp.Services.DotNetCliService: Checking the 'DOTNET_ROOT' environment variable to find a .NET SDK | "))
[server-notification] Fri Sep 23 22:48:23 2022:
(:jsonrpc "2.0" :method "window/logMessage" :params
      (:type 3 :message "OmniSharp.Services.DotNetCliService: Checking the 'DOTNET_ROOT' environment variable to find a .NET SDK | "))
[server-notification] Fri Sep 23 22:48:23 2022:
(:jsonrpc "2.0" :method "window/logMessage" :params
      (:type 3 :message "OmniSharp.Services.DotNetCliService: Using the 'dotnet' on the PATH. | "))
[server-notification] Fri Sep 23 22:48:23 2022:
(:jsonrpc "2.0" :method "window/logMessage" :params
      (:type 3 :message "OmniSharp.Services.DotNetCliService: Using the 'dotnet' on the PATH. | "))
[server-notification] Fri Sep 23 22:48:23 2022:
(:jsonrpc "2.0" :method "window/logMessage" :params
      (:type 3 :message "OmniSharp.Services.DotNetCliService: DotNetPath set to dotnet | "))
[server-notification] Fri Sep 23 22:48:23 2022:
(:jsonrpc "2.0" :method "window/logMessage" :params
      (:type 3 :message "OmniSharp.Services.DotNetCliService: DotNetPath set to dotnet | "))
[internal] Fri Sep 23 22:48:52 2022:
(:message "Connection state changed" :change "已杀死\n")
// ...

If I run OmniSharp -stdio -lsp from the ~/tmp/Hello directory, the server seems to work fine.

I am not sure if the problem is caused by the OS environment, by Emacs or eglot, and thanks for any tips.

joaotavora commented 1 year ago

A while ago I was working with OmniSharp and eglot and I found that one reliable way to get this server is to install VsCode (ugh :vomiting_face: ) and use this incantation when Eglot prompts you:

dotnet $HOME/.vscode/extensions/ms-dotnettools.csharp-1.25.0-linux-x64/.omnisharp/1.39.0-net6.0/OmniSharp.dll -lsp

I've since uninstalled VsCode, so I can't test with a recent version. Probably replace $HOME with your home dir.

Hope this helps.

I am not sure if the problem is caused by the OS environment, by Emacs or eglot, and thanks for any tips.

I'm pretty sure it's not Eglot, but the incantation and the versino of the Omnisharp server. Using the version hidden away in VsCode seems like a good idea.

blldw commented 1 year ago

I installed VsCode and C# extension with OmniSharp support, and the incantation doesn't work either, I found: (1) OmniSharp shipped with C# extension is just the one I used, I compared their hash value with md5sum and got the same result: e7d914ed518c01c3eb6c4007b08633e0. (2) OmniSharp worked in VsCode very well. So, does VsCode load OmniSharp in some other way?

joaotavora commented 1 year ago

Peek 2022-09-24 10-10

I found I I still have a omnisharp-roslyn program lying around. It seems to work fine. The problem is likely with your configuration.

Here I am trying this out in a simple project. I'm using the latest Eglot and Emacs but it'll probably work with Emacs 28 and an older Eglot. Just make sure you start with with no fancy configuration, and then re-add some of this configuration once you have it working.

joaotavora commented 1 year ago

Also the reason I'm passing -f package-initialize to the Emacs incantation is to activate local ELPA packages. Needed packages here are csharp-mode and markdown-mode (the last one is more like optional)

joaotavora commented 1 year ago

So, does VsCode load OmniSharp in some other way?

I had this question and I answered it with ps -fax

blldw commented 1 year ago

Yes, I followed your way and it works now, thanks for your answering.

runeanielsen commented 1 year ago

@blldw I've had much better experience working in C# using this language server https://github.com/razzmatazz/csharp-language-server. It's much faster and haven't experienced any issues with it since I started using it, where with Omnisharp I had issues daily.

It can be installed with running dotnet tool install --global csharp-ls.

Here is the emacs-lisp. :)

(add-to-list 'eglot-server-programs
                 '(csharp-mode . ("csharp-ls")))