OmniSharp / omnisharp-roslyn

OmniSharp server (HTTP, STDIO) based on Roslyn workspaces
MIT License
1.73k stars 418 forks source link

Emacs LSP with Unity3d #1692

Open jonmol opened 4 years ago

jonmol commented 4 years ago

Hi

I'm trying to get Emacs to work as my editor for Unity3d and I'm having some problems with it. C# isn't my thing so I might very well be missing something obvious.

OS: Debian bullseye Omnisharp-roslyn: 1.34.10 for Emacs and 1.34.9 for VSCode Unity version: 2019.3.0f6

I'm using the server installed by emacs, omnisharp-roslyn v1.34.10 and this is on a Debian system. The project is using Unity 2019.3.0f6 and the code is just one empty class. I have installed both Mono and MS dotnet-sdk-3.1, dotnet-runtime-3.1 and aspnetcore-runtime-3.1 packages provided by them. I seem to have two issues, one similar to #1689 and another which seems to be related to library paths.
The first one says:

Loading project: /home/me/unity/TestProject/Unity/UnityEngine.TestRunner.csproj
Failed to handle notification textDocument/documentSymbol - 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 <0b942f6825944d498ce3e334bf9d9076>:0 
  at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, OmniSharp.Extensions.Embedded.MediatR.RequestHandlerDelegate`1[TResponse] next) [0x000e1] in <12a97e97079c4b10a770c2869fa566a9>:0 
  at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1[TDescriptor].RouteRequest (TDescriptor descriptor, OmniSharp.Extensions.JsonRpc.Server.Request request, System.Threading.CancellationToken token) [0x0038a] in <709d80c63bce4fcd8b1071c79413b97b>:0 

The second one is for every generated csproj file and says:

Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=en-GB, PublicKeyToken=b03f5f7f11d50a3a'
FAILURE: Could not locate '/home/me/.emacs.d/.cache/lsp/omnisharp-roslyn/v1.34.10/omnisharp/.msbuild/Current/Bin/Microsoft.Build.resources.dll'.
Attempting to resolve 'Microsoft.Build.Tasks.Core.resources, Version=15.1.0.0, Culture=en-GB, PublicKeyToken=b03f5f7f11d50a3a'
FAILURE: Could not locate '/home/me/.emacs.d/.cache/lsp/omnisharp-roslyn/v1.34.10/omnisharp/.msbuild/Current/Bin/Microsoft.Build.Tasks.Core.resources.dll'.
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
Failed to load project file '/home/me/unity/TestProject/Unity/UnityEngine.TestRunner.csproj'.
Loading project: /home/me/unity/TestProject/Unity/UnityEngine.UI.csproj
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
Failed to load project file '/home/me/unity/TestProject/Unity/UnityEngine.UI.csproj'.
....

The first one is just confusing to me, the second one feels more like it's something I could fix with the right library paths.

I have references to 4.7.1 on two places on my disks:

/ext/opt/Unity/2019.3.0f6/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api
/usr/lib/mono/4.7.1-api
/usr/lib/mono/xbuild-frameworks/.NETFramework/v4.7.1

And the csproj files it's failing to load is pointing to Unity as far as I can see with plenty of lines such as <HintPath>/ext/opt/Unity/2019.3.0f6/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/mscorlib.dll</HintPath>.

In an act of desperation did I try installing VS Code as well, and omnisharp for it. That is also failing.

Any help is much appreciated!

omnisharp-emacs.txt omnisharp-vscode.txt

ijacquez commented 4 years ago

What worked for me is not having dotnet.

Here is a list of packages I have installed:

mono-4.0-gac
mono-gac
mono-runtime
mono-runtime-common
mono-runtime-sgen

Everything worked out of the box (for Unity3D).

jonmol commented 4 years ago

I installed the extra packages after things not working for me and getting other errors, and then looking at #1168 and its siblings. Now that I've removed aspnetcore-runtime-3.1 aspnetcore-targeting-pack-3.1 dotnet-apphost-pack-3.1 dotnet-sdk-3.1 dotnet-targeting-pack-3.1 is it back to that error:

[19:23:45] starting server on project root "~/unity/test_project/UnityTest/"
[19:23:45] Using server binary on /home/me/.emacs.d/.cache/omnisharp/server/v1.34.5/run
[19:23:46] INFORMATION: OmniSharp.Stdio.Host, Starting OmniSharp on debian 0.0 (x64)
[19:23:46] INFORMATION: OmniSharp.Services.DotNetCliService, DotNetPath set to dotnet
[19:23:46] INFORMATION: OmniSharp.MSBuild.Discovery.MSBuildLocator, Located 1 MSBuild instance(s)
            1: StandAlone 16.0 - "/home/me/.emacs.d/.cache/omnisharp/server/v1.34.5/omnisharp/.msbuild/Current/Bin"
[19:23:46] INFORMATION: OmniSharp.MSBuild.Discovery.MSBuildLocator, MSBUILD_EXE_PATH environment variable set to '/home/me/.emacs.d/.cache/omnisharp/server/v1.34.5/omnisharp/.msbuild/Current/Bin/MSBuild.dll'
[19:23:46] INFORMATION: OmniSharp.MSBuild.Discovery.MSBuildLocator, Registered MSBuild instance: StandAlone 16.0 - "/home/me/.emacs.d/.cache/omnisharp/server/v1.34.5/omnisharp/.msbuild/Current/Bin"
            MSBuildExtensionsPath = /usr/lib/mono/xbuild
            BypassFrameworkInstallChecks = true
            CscToolPath = /home/me/.emacs.d/.cache/omnisharp/server/v1.34.5/omnisharp/.msbuild/Current/Bin/Roslyn
            CscToolExe = csc.exe
            MSBuildToolsPath = /home/me/.emacs.d/.cache/omnisharp/server/v1.34.5/omnisharp/.msbuild/Current/Bin
            TargetFrameworkRootPath = /usr/lib/mono/xbuild-frameworks
[19:23:46] INFORMATION: OmniSharp.Cake.CakeProjectSystem, Detecting Cake files in '/home/me/unity/test_project/UnityTest'.
[19:23:46] INFORMATION: OmniSharp.Cake.CakeProjectSystem, Could not find any Cake files
[19:23:46] INFORMATION: OmniSharp.MSBuild.ProjectSystem, Detecting projects in '/home/me/unity/test_project/UnityTest/Unity.sln'.
[19:23:46] INFORMATION: OmniSharp.MSBuild.ProjectManager, Queue project update for '/home/me/unity/test_project/UnityTest/Assembly-CSharp.csproj'
[19:23:46] INFORMATION: OmniSharp.Script.ScriptProjectSystem, Detecting CSX files in '/home/me/unity/test_project/UnityTest'.
[19:23:46] INFORMATION: OmniSharp.MSBuild.ProjectManager, Loading project: /home/me/unity/test_project/UnityTest/Assembly-CSharp.csproj
[19:23:47] INFORMATION: OmniSharp.Script.ScriptProjectSystem, Could not find any CSX files
[19:23:47] INFORMATION: OmniSharp.WorkspaceInitializer, Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
[19:23:47] INFORMATION: OmniSharp.WorkspaceInitializer, Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
[19:23:47] INFORMATION: OmniSharp.WorkspaceInitializer, Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.EditorConfigWorkspaceOptionsProvider, Order: 200
[19:23:47] INFORMATION: OmniSharp.WorkspaceInitializer, Configuration finished.
[19:23:47] INFORMATION: OmniSharp.Stdio.Host, Omnisharp server running using Stdio at location '/home/me/unity/test_project/UnityTest' on host -1.
[19:23:47] WARNING: OmniSharp.MSBuild.ProjectLoader, Unable to create directory "/Debug/". Access to the path "/Debug/" is denied.
[19:23:47] WARNING: OmniSharp.MSBuild.ProjectLoader, Could not write state file "/Debug/Assembly-CSharp.csprojAssemblyReference.cache". Could not find a part of the path "/Debug/Assembly-CSharp.csprojAssemblyReference.cache".
[19:23:47] ERROR: OmniSharp.MSBuild.ProjectLoader, Could not write lines to file "/Debug/Assembly-CSharp.csproj.CoreCompileInputs.cache". Access to the path "/Debug" is denied.
[19:23:47] WARNING: OmniSharp.MSBuild.ProjectManager, Failed to load project file '/home/me/unity/test_project/UnityTest/Assembly-CSharp.csproj'.
[19:23:47] ERROR: OmniSharp.MSBuild.ProjectManager, Attempted to update project that is not loaded: /home/me/unity/test_project/UnityTest/Assembly-CSharp.csproj

My mono packages installed are (apart from a gazillion libmono-* packages):

mono-4.0-gac
mono-4.0-service
mono-complete
mono-csharp-shell
mono-devel
mono-gac
mono-jay
mono-mcs
mono-runtime
mono-runtime-common
mono-runtime-sgen
mono-utils
mono-xbuild
mono-xsp4
mono-xsp4-base
monodoc-base
monodoc-http
monodoc-manual
jonmol commented 4 years ago

Ok, so I removed all mono added the official mono repo for Debian and installed mono-devel. Still noting working but now it was complaining that v4.7.1 didn't exist, according to mono they do support 4.7.1. But, I did see in my files that there's 4.7.1-api, so I did:

perl -pi -e 's/v4.7.1/v4.7.1-api/g' *csproj

in the unity project root folder. That made omnisharp-mode work in Emacs with both flycheck-mode and company-mode. However, lsp-mode still fails with:

Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
Loading project: /home/me/unity/UnityTest/Unity/Assembly-CSharp.csproj
Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110
Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.EditorConfigWorkspaceOptionsProvider, Order: 200
Configuration finished.
Omnisharp server running using Lsp at location '/home/me/unity/UnityTest/Unity' on host 31368.
Finished: Routing Notification initialized in 355ms
Starting: Routing Notification textDocument/didOpen
Converting params for Notification textDocument/didOpen to OmniSharp.Extensions.LanguageServer.Protocol.Models.DidOpenTextDocumentParams
Finished: Routing Notification textDocument/didOpen in 6ms
Starting: Routing Request (2) textDocument/documentSymbol
Converting params for Request (2) textDocument/documentSymbol to OmniSharp.Extensions.LanguageServer.Protocol.Models.DocumentSymbolParams
Failed to handle notification textDocument/documentSymbol - 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 <0b942f6825944d498ce3e334bf9d9076>:0 
  at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline`2[TRequest,TResponse].Handle (TRequest request, System.Threading.CancellationToken cancellationToken, OmniSharp.Extensions.Embedded.MediatR.RequestHandlerDelegate`1[TResponse] next) [0x000e1] in <12a97e97079c4b10a770c2869fa566a9>:0 
  at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1[TDescriptor].RouteRequest (TDescriptor descriptor, OmniSharp.Extensions.JsonRpc.Server.Request request, System.Threading.CancellationToken token) [0x0038a] in <709d80c63bce4fcd8b1071c79413b97b>:0 
Finished: Routing Request (2) textDocument/documentSymbol in 11ms
Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=en-GB, PublicKeyToken=b03f5f7f11d50a3a'
FAILURE: Could not locate '/home/me/.emacs.d/.cache/lsp/omnisharp-roslyn/v1.34.10/omnisharp/.msbuild/Current/Bin/Microsoft.Build.resources.dll'.
Failed to load project file '/home/me/unity/UnityTest/Unity/Assembly-CSharp.csproj'.
Attempted to update project that is not loaded: /home/me/unity/UnityTest/Unity/Assembly-CSharp.csproj
Finding descriptor for textDocument/completion

I reckon this isn't directly related to my earlier woes but if anyone has an idea I'd appreciate it. I'd really like to use lsp-mode as it's working really well for me for both Go and Typescript.

My init looks like:

(use-package csharp-mode
  :ensure t
  :hook ((csharp-mode . lsp-deferred)
         (before-save . lsp-format-buffer)
         ))
jonmol commented 4 years ago

I was too quick, I don't get any errors but I also don't get full intellisense. company-mode only gives me the "global" imported c# suggestions such as: image The debug call looks like:

[18:42:25] --> 115 /autocomplete ((Arguments (WantDocumentationForEveryCompletionResult . t) (WantMethodHeader . t) (WantReturnType . t) (WantSnippet . t) (WantImportableTypes . :json-false) (WordToComplete . #("Colo" 0 1 (fontified t) 1 2 (fontified t) 2 3 (fontified t) 3 4 (fontified t))) (WantKind . t) (FileName . "/home/me/unity/UnityTest/Unity/Assets/Scripts/MapClick.cs") (Line . "16") (Column . "17") (Buffer . "using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
...
//
")) (Command . "/typelookup") (Seq . 117))

I don't get Color as a suggestion, nor anything else from unity , indicating that it's not getting the dependencies from the Unity generated csproj files. That is also stated in the omnisharp-log buffer:

[18:08:01] INFORMATION: OmniSharp.WorkspaceInitializer, Configuration finished.
[18:08:01] INFORMATION: OmniSharp.Stdio.Host, Omnisharp server running using Stdio at location '/home/me/unity/UnityTest/Unity' on host -1.
[18:08:01] WARNING: OmniSharp.MSBuild.ProjectManager, Failed to load project file '/home/me/unity/UnityTest/Unity/Assembly-CSharp.csproj'.
[18:08:01] ERROR: OmniSharp.MSBuild.ProjectManager, Attempted to update project that is not loaded: /home/me/unity/UnityTest/Unity/Assembly-CSharp.csproj

I also don't get any suggestions for using, like when declaring a variable DateTime (no suggestion, nor any compile errors) I don't get "you should be using System". However, when I do add "using System" am I getting suggestions on class functions on my DateTime variable.

I'm really at a loss here, C# isn't my language so any pointers or directions would be really appreciated.

razzmatazz commented 4 years ago

There is an issue with LSP text document sync, that is still in the latest version of omnisharp-roslyn that makes the client not to update the model of the document in the server and thus out-of-date autocomplete suggestions at the point.

I have a PR to fix that one particular issue (with sync) but not sure if that fixes the other things reported here:

jcs090218 commented 3 years ago

I am also encountering this issue. I am using latest version 1.37.6. Any idea with this issue? 😖

razzmatazz commented 3 years ago

I am also encountering this issue. I am using latest version 1.37.6. Any idea with this issue? 😖

I believe a lot of bugs have been fixed since this issue has been registered, e.g. with PR #2005 and others. Do you have more details on what in particular is your case of this issue?

jcs090218 commented 3 years ago

Ah, after deleting Roslyn server from .cache then everything work. I guess lsp-csharp doesn't automatically update itself?

FilipCon commented 3 years ago

I also experience similar issues in Ubuntu 20.04 with Emacs and lsp-mode or omnisharp-mode (It used to work properly last time I checked). The VSCode extension works fine though! I've installed the mono packages and removed the dotnet as stated above, but still get the same errors. I'd appreciate any help.

EDIT: I checked the VSCode extension and actually I get the following:

[fail]: OmniSharp.MSBuild.ProjectLoadListener
        Unexpected exception got thrown from project load listener: System.NullReferenceException: Object reference not set to an instance of an object
  at OmniSharp.MSBuild.ProjectLoadListener.GetSdkVersion (OmniSharp.MSBuild.Notification.ProjectLoadedEventArgs args) [0x0000b] in <52df5b302a1f4915b27902a2a98ee9f9>:0 
  at OmniSharp.MSBuild.ProjectLoadListener.ProjectLoaded (OmniSharp.MSBuild.Notification.ProjectLoadedEventArgs args) [0x0001a] in <52df5b302a1f4915b27902a2a98ee9f9>:0 
Received response for /quickinfo but could not find request.

However the completion works properly. I don't know if it related or of any help, but I thought to mention it.