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

Emacs + ls-mode fail on C# file in Linux Ubuntu 20.04 #2188

Closed
snowgoon88 opened this issue Sep 16, 2020 · 19 comments
Closed

Emacs + ls-mode fail on C# file in Linux Ubuntu 20.04 #2188

snowgoon88 opened this issue Sep 16, 2020 · 19 comments

Comments

@snowgoon88
Copy link

Describe the bug
Using emacs + lsp-mode with Linux (Ubuntu 20.04) on C# files, it seems I can connect to the omnisharp server but then nothing work and I have "LSP :: Error from the Language Server: Internal Error - System.NullReferenceException".
Seems related to issue #1125.

To Reproduce
Emacs with lsp-mode package.
Open .cs file
M-x lsp

Expected behavior
Code analysis and flycheck errors (as working for my c++ files)

Which Language Server did you use
omnisharp-roslyn as downloaded by emacs in
~/.emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1
(I tried to git clone the omnisharp-roselyn and built but failed because of gitVersion)

OS
Linux Ubuntu 20.04
=> mono --version Mono JIT compiler version 6.8.0.105 (Debian 6.8.0.105+dfsg-2 Wed Feb 26 23:23:50 UTC 2020) Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com TLS: __thread SIGSEGV: altstack Notifications: epoll Architecture: amd64 Disabled: none Misc: softdebug Interpreter: yes LLVM: supported, not enabled. Suspend: hybrid GC: sgen (concurrent by default)

Error callstack
lsp-workspace-log in https://gist.github.com/snowgoon88/6e0f1a0cf3eadff11c64076c8dd4fa31#file-lsp-workspace-log
messages.log in https://gist.github.com/snowgoon88/6e0f1a0cf3eadff11c64076c8dd4fa31#file-messages-log

lsp-session

[-] /home/alain/Projets/Unity/ImPacman
 -[-] csharp:429402
    |-[-] Buffers
    |  -[+] MoveFromAbove.cs
    -[+] Capabilities
[X] /home/alain/Projets/ChuchuGL

The Assembly-CSharp.csproj and ImPacman.sln (generated with Unity) are in /home/alain/Projets/Unity/ImPacman

It might be a difficulty to read/analyse the .csproj or .sln. But I'm not expert enough in C# or emacs to tell. I can share these files if needed.

@yyoncho
Copy link
Member

yyoncho commented Sep 16, 2020

It might be a difficulty to read/analyse the .csproj or .sln. But I'm not expert enough in C# or emacs to tell. I can share these files if needed.

Can you provide that project or another one that can be used to reproduce the issue? Most likely it is a server-side issue.

I am not 100% sure if the server supports that kind of project. @razzmatazz might know better.

@snowgoon88
Copy link
Author

I think you are right.
I tried to "talk" to the server using
./run -s ~/Projets/Unity/ImPacman -v
and I have messages saying that some files in the .Net framework could not be found and, even if omnisharp-roselyn v1.37.1 should be compatible up to .NET v4.7.2 I also have
The reference assemblies for .NETFramework,Version=**v4.7.1** were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks

Here is the output of ./run -s ~/Projets/Unity/ImPacman -v
https://gist.github.com/snowgoon88/6e0f1a0cf3eadff11c64076c8dd4fa31#file-omnisharp-log

The .csproj and .sln are
https://gist.github.com/snowgoon88/6e0f1a0cf3eadff11c64076c8dd4fa31#file-assembly-csharp-csproj
https://gist.github.com/snowgoon88/6e0f1a0cf3eadff11c64076c8dd4fa31#file-impacman-sln

@yyoncho
Copy link
Member

yyoncho commented Sep 16, 2020

CC @sebasmonia as well.

@sebasmonia
Copy link

hi @snowgoon88! Haven't used Unity projects with Omnisharp but from your logs it would seem that something more fundamental is missing.

Have you installed the C# SDK?

And one thing worth trying is, following @yyoncho 's suggestion, create a new project dotnet net console in any directory, and see if you get omnisharp-roslyn running with it. That can help us narrow down the problem.

@snowgoon88
Copy link
Author

snowgoon88 commented Sep 16, 2020

Hmm, as I said, I'm not skilled in the C# environment. I'm on Linux, using Unity and tried to use Emacs as an editor.
I haven't installed the dotnet of Microsoft, but I'll do it. It might help but I'm not sure as omnisharp-roselyn is looking for the missing files in ~/.emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1
Oh, by the way, the missing files are (at least)

Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll
Microsoft.CodeAnalysis.VisualBasic.Features.dll
Microsoft.CodeAnalysis.Workspaces.resources.dll
Microsoft.CodeAnalysis.CSharp.Workspaces.resources.dll
Microsoft.CodeAnalysis.CSharp.Features.resources.dll
Microsoft.CodeAnalysis.Features.resources.dll
Microsoft.Build.resources.dll
Microsoft.Build.resources.dll
Microsoft.Build.Tasks.Core.resources.dll`

They seem to come from

.NET Compiler Platform ("Roslyn") support for analyzing Visual Basic projects and solutions.
 More details at https://aka.ms/roslyn-packages

Ok; installing .Net from Micro$oft...

@snowgoon88
Copy link
Author

I've created a simple Test program using dotnet new console

I have the same kind of errors in Messages, but other logs and error are a bit different. Besides, flycheck gives me many errors, some are a bit dubious.
Everything is here https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f

console.log the result of running omnisharp-roselyb with un -s /home/alain/tmp/TestCSharp -v > console.log
https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f#file-console-log

lsp.log the buffer lsp-log (long file)
https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f#file-lsp-log

messages.log the Messages buffer
https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f#file-messages-log

Program.cs the simple c# code
https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f#file-program-cs

TestCSharp.csproj no surprise
https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f#file-testcsharp-csproj

flycheck.errors the errors displayed by flycheck
https://gist.github.com/snowgoon88/440a944f20b50b52f0e89c17eba9930f#file-flycheck-errors

@sebasmonia
Copy link

I am not sure what is missing in your environment, but something is.
Did you follow the install instructions from https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#install-the-sdk ?

@snowgoon88
Copy link
Author

Yes, I did follow the instruction. I have one good news : I restarted my linux session, and now it works smoothly for the basic console project. Nice.
(Sorry about that, I'm not used to restart session after installing things. I did that because I tried to build omnisharp-roselyn from the git clone and, as explained in OmniSharp/omnisharp-roslyn#1920, I installed dotnet-gitversion and it explicitely asks to restart the session).

Still, the bad news (for me) is that lsp-mode still fails on .cs file inside a Unity project. Mostly because of .csproj file as in the basic console project, the log says :

{"Event":"log","Body":{"LogLevel":"INFORMATION","Name":"OmniSharp.MSBuild.ProjectManager","Message":"Successfully loaded project file '/home/alain/tmp/TestCSharp/TestCSharp.csproj'."},"Seq":85,"Type":"event"}

{"Event":"MsBuildProjectDiagnostics","Body":{"FileName":"/home/alain/tmp/TestCSharp/TestCSharp.csproj","Warnings":[],"Errors":[]},"Seq":86,"Type":"event"}
{"Event":"log","Body":{"LogLevel":"INFORMATION","Name":"OmniSharp.MSBuild.ProjectManager","Message":"Adding project '/home/alain/tmp/TestCSharp/TestCSharp.csproj'"},"Seq":87,"Type":"event"}
{"Event":"log","Body":{"LogLevel":"INFORMATION","Name":"OmniSharp.MSBuild.ProjectManager","Message":"Update project: TestCSharp"},"Seq":88,"Type":"event"}

whereas with the files coming from Unity, I have :

{"Event":"log","Body":{"LogLevel":"ERROR","Name":"OmniSharp.MSBuild.ProjectLoader","Message":"The reference assemblies for .NETFramework,Version=v4.7.1 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks"},"Seq":86,"Type":"event"}
{"Event":"log","Body":{"LogLevel":"WARNING","Name":"OmniSharp.MSBuild.ProjectManager","Message":"Failed to load project file '/home/alain/Projets/Unity/ImPacman/Assembly-CSharp.csproj'."},"Seq":87,"Type":"event"}


{"Event":"MsBuildProjectDiagnostics","Body":{"FileName":"/home/alain/Projets/Unity/ImPacman/Assembly-CSharp.csproj","Warnings":[],"Errors":[{"LogLevel":"Error","FileName":"/home/alain/.emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1/omnisharp/.msbuild/Current/Bin/Microsoft.Common.CurrentVersion.targets","Text":"The reference assemblies for .NETFramework,Version=v4.7.1 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks","StartLine":1178,"StartColumn":5,"EndLine":0,"EndColumn":0}]},"Seq":88,"Type":"event"}
{"Event":"log","Body":{"LogLevel":"ERROR","Name":"OmniSharp.MSBuild.ProjectManager","Message":"Attempted to update project that is not loaded: /home/alain/Projets/Unity/ImPacman/Assembly-CSharp.csproj"},"Seq":89,"Type":"event"}

It seems strange to me because omnisharp comes with its own mono (and thus SDK I think), supposed to deal with .NET version 4.7.2

and then, in *Message* buffer after M-x lsp RET on one of my file...

LSP :: Error from the Language Server: Internal Error - System.NullReferenceException: Object reference not set to an instance of an object
  at OmniSharp.LanguageServerProtocol.Handlers.OmniSharpDocumentSymbolHandler.Handle (OmniSharp.Extensions.LanguageServer.Protocol.Models.DocumentSymbolParams request, System.Threading.CancellationToken token) [0x00096] in <57f6177d3f2b4286a7d315a96dc1d7b3>:0 
  at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x0006d] in <1238dd34c3f54a5080acfb1001dda78e>:0 
  at MediatR.Pipeline.RequestPreProcessorBehavior`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x0013e] in <856ae299ee314304bc3affd17d8d5868>:0 
  at MediatR.Pipeline.RequestPostProcessorBehavior`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x0007e] in <856ae299ee314304bc3affd17d8d5868>:0 
  at MediatR.Pipeline.RequestExceptionProcessorBehavior`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x00084] in <856ae299ee314304bc3affd17d8d5868>:0 
  at MediatR.Pipeline.RequestExceptionProcessorBehavior`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x00276] in <856ae299ee314304bc3affd17d8d5868>:0 
  at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x00084] in <856ae299ee314304bc3affd17d8d5868>:0 
  at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, MediatR.RequestHandlerDelegate`1[TResponse] next) [0x001e7] in <856ae299ee314304bc3affd17d8d5868>:0 
  at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1[TDescriptor].<RouteRequest>g__InnerRoute|5_0 (Microsoft.Extensions.DependencyInjection.IServiceScopeFactory serviceScopeFactory, OmniSharp.Extensions.JsonRpc.Server.Request request, TDescriptor descriptor, System.Object params, System.Threading.CancellationToken token, Microsoft.Extensions.Logging.ILogger logger) [0x000f2] in <fba9e575570149efaee3484292339424>:0 
  at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1[TDescriptor].RouteRequest (OmniSharp.Extensions.JsonRpc.IRequestDescriptor`1[TDescriptor] descriptors, OmniSharp.Extensions.JsonRpc.Server.Request request, System.Threading.CancellationToken token) [0x005f3] in <fba9e575570149efaee3484292339424>:0 
  at OmniSharp.Extensions.JsonRpc.InputHandler+<>c__DisplayClass38_0.<RouteRequest>b__5 (System.Threading.CancellationToken ct) [0x000e8] in <fba9e575570149efaee3484292339424>:0  (Internal Error)

and so on...

@sebasmonia
Copy link

Yes it does seems strange. Usually I would say it referencing 4.7.2 is a problem with Windows dependencies (the version numbers on .NET are up to 4.7 for "old .NET" and up to 3 for "dotnet core".
So of course there is no 4.7 in Linux.
But then how does Unity work, what does it reference etc. 🤔

PS: soon we will have a single .NET, version 5, all unified. But until then the 4.7 log entries are suspicious.

@snowgoon88
Copy link
Author

Ok, I was not aware of the existence of 2 different .Net ... ok...

So, it seems the issue comes from omnisharp. But, as I said, it comes with Mono (compilation of C# on Linux) that is supposed to be compatible with .Net 4.7.2.

I guess I will have to wait to use lsp on Unity. Too bad.

@snowgoon88
Copy link
Author

I have it working on my ubuntu 20.04.
I had a global mono installed (installed ./deb packages mono-complete), version 6.8.0.105 but the run script used by lsp-mode in .emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1 does not know how to use it. This run script always uses the .emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1bin/mono, which does not know how to handle Unity .csproj files.

So I've modified the run script as below and even if I still have some errors while the connection with server is initialized, sometimes it works. In fact; my second try was the good one : lsp is ok, can find definitions, suggest proper actions and completions. (although, a bit slow but Unity is Huge)

(to be complete, I mention the solution suggested in OmniSharp/omnisharp-roslyn#1948 (comment), but I haven't tested it)

My run file

#!/usr/bin/env bash

debug=""  #set to anything to debug

base_dir="$(cd "$(dirname "$0")" && pwd -P)"
bin_dir=${base_dir}/bin
etc_dir=${base_dir}/etc
omnisharp_dir=${base_dir}/omnisharp

# test if valid mono command
# if version too low, or no mono installed
mono_cmd=`command -v mono`
mono_ver=`${mono_cmd} --version | head -n1 | cut -d " " -f5`
if [ $debug ]; then
    echo "CMD:"${mono_cmd}
    echo "VER:"${mono_ver}
fi

requiredver="5.0.0"
if [ "$(printf '%s\n' "$requiredver" "$mono_ver" | sort -V | head -n1)" = "$requiredver" ]; then
    if [ ${debug} ]; then  
        echo "Installed mono version Greater than or equal to ${requiredver}"
    fi
    config_file=/etc/mono/config
else
    if [ ${debug} ]; then  
        echo "installed mono version Less than ${requiredver}"
    fi
    mono_cmd=${bin_dir}/mono
    chmod 755 "${mono_cmd}"

    config_file=${etc_dir}/config
fi

omnisharp_cmd=${omnisharp_dir}/OmniSharp.exe
no_omnisharp=false

if [ "$1" = "--no-omnisharp" ]; then
    shift
    no_omnisharp=true
fi

export MONO_CFG_DIR=${etc_dir}
export MONO_ENV_OPTIONS="--assembly-loader=strict --config ${config_file}"

if [ "$no_omnisharp" = true ]; then
    if [ ${debug} ]; then
        echo "${mono_cmd}" "$@"
    else
        "${mono_cmd}" "$@"
    fi
else
    if [ ${debug} ]; then
        echo "${mono_cmd}" "${omnisharp_cmd}" "$@"
    else
        "${mono_cmd}" "${omnisharp_cmd}" "$@"
    fi
fi

@snowgoon88
Copy link
Author

But I have to restart the server several times (like 3 or 4) for it to finally work. Not very user friendly...

@yyoncho
Copy link
Member

yyoncho commented Sep 18, 2020

But I have to restart the server several times (like 3 or 4) for it to finally work. Not very user friendly...

Did you try to wait for some time?

I am closing this issue since it seems there is nothing to do on emacs side.

@yyoncho yyoncho closed this as completed Sep 18, 2020
@yyoncho
Copy link
Member

yyoncho commented Sep 18, 2020

I think that there was an issue related to startup. Where if you do some operation right after the server has started it breaks. In this case you have to close the buffer and open it again.

@snowgoon88
Copy link
Author

snowgoon88 commented Sep 18, 2020

OK, I'll check. But waiting before doing things does not solves the problem : I've waited 15min. Nothing. Restarted the server, and everything was ok, nearly instantly.

BTW, on the server side (omnisharp) they have closed the issue, saying it was the client that was responsible of "finding" the good mono...

@yyoncho
Copy link
Member

yyoncho commented Sep 18, 2020

BTW, on the server side (omnisharp) they have closed the issue, saying it was the client that was responsible of "finding" the good mono...

You may either rename this bug or open another one.

@sebasmonia
Copy link

sebasmonia commented Sep 18, 2020

Not sure I agree with the assessment that it's on the client to find mono. Regardless, glad you got it working :)

EDIT: just read the issue, they said it's up to the user to install global mono, I understood they were asking lsp-mode to locate it. I kind of agree with their point. They provided a minimum to get it working but if you need extra dependencies is up to you to set them up.

@razzmatazz
Copy link
Collaborator

razzmatazz commented Sep 20, 2020

There is not much I can add here, except for trying with another omnisharp client (like vscode) and then noting if there is a difference in behaviour, and if not (i.e. if it works the same in emacs and VS code -- which uses legacy/custom protocol) -- report the issue to omnisharp-roslyn server project, which I see has been done here.

Other than that, I have added a PR #2196 which handles 'o#/error' notifications from the server and displays them in *Warnings* buffer so the user knows things are wrong and hopefully does not have to run the server from console to diagnose the issue..

@R0flcopt3r
Copy link

I have it working on my ubuntu 20.04.
I had a global mono installed (installed ./deb packages mono-complete), version 6.8.0.105 but the run script used by lsp-mode in .emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1 does not know how to use it. This run script always uses the .emacs.d/.cache/lsp/omnisharp-roslyn/v1.37.1bin/mono, which does not know how to handle Unity .csproj files.

So I've modified the run script as below and even if I still have some errors while the connection with server is initialized, sometimes it works. In fact; my second try was the good one : lsp is ok, can find definitions, suggest proper actions and completions. (although, a bit slow but Unity is Huge)

(to be complete, I mention the solution suggested in OmniSharp/omnisharp-roslyn#1948 (comment), but I haven't tested it)

My run file

#!/usr/bin/env bash
[...]

I am not able to replicate this. Sadly my setup randomly broke today, and I've been trying a couple various solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants