OmniSharp / omnisharp-emacs

Troll coworkers - use Emacs at work for csharp!
GNU General Public License v3.0
514 stars 94 forks source link

Omnisharp doesn't detect errors #459

Closed gl4eqen closed 4 years ago

gl4eqen commented 5 years ago

I'm using Omnisharp within csharp layer of Spacemacs. It works pretty well apart from error detection. It doesn't see anything more sophisticated than missing coma: lacking namespace import, typo in method name etc. Neither omnisharp-solution-errors nor flycheck show anything useful.

This line seems worrying from omnisharp log: [13:13:58] WARNING: OmniSharp.MSBuild.ProjectManager, Failed to load project file '/home/knbr34/dotnet-projects/SampleProj/SampleProj.csproj'. Is my configuration wrong? Omnisharp version: 20181023.505 from melpa Manual build by dotnet build behaves correctly.

Possibly helpful files: *omnisharp-log* with omnisharp-debug set to 't'

[13:13:56] Starting OmniSharpServer using project folder/solution file: /home/knbr34/dotnet-projects/dotnet-projects.sln
[13:13:56] Using server binary on /home/knbr34/.emacs.d/.cache/omnisharp/server/v1.32.6/run
[13:13:56] --> 1 /codecheck ((Arguments (FileName . "/home/knbr34/dotnet-projects/SampleProj/Program.cs") (Line . "1") (Column . "1") (Buffer . "namespace SampleProj
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(\"Hello world!\");
        }
    }
}
")) (Command . "/codecheck") (Seq . 1))
[13:13:57] INFORMATION: OmniSharp.Stdio.Host, Starting OmniSharp on arch 0.0 (x64)
[13:13:57] INFORMATION: OmniSharp.Services.DotNetCliService, DotNetPath set to dotnet
[13:13:57] INFORMATION: OmniSharp.MSBuild.Discovery.MSBuildLocator, Located 1 MSBuild instance(s)
            1: StandAlone 15.0 - "/home/knbr34/.emacs.d/.cache/omnisharp/server/v1.32.6/omnisharp/msbuild/15.0/Bin"
[13:13:57] INFORMATION: OmniSharp.MSBuild.Discovery.MSBuildLocator, MSBUILD_EXE_PATH environment variable set to '/home/knbr34/.emacs.d/.cache/omnisharp/server/v1.32.6/omnisharp/msbuild/15.0/Bin/MSBuild.dll'
[13:13:57] INFORMATION: OmniSharp.MSBuild.Discovery.MSBuildLocator, Registered MSBuild instance: StandAlone 15.0 - "/home/knbr34/.emacs.d/.cache/omnisharp/server/v1.32.6/omnisharp/msbuild/15.0/Bin"
            MSBuildExtensionsPath = /usr/lib/mono/xbuild
            BypassFrameworkInstallChecks = true
            CscToolPath = /home/knbr34/.emacs.d/.cache/omnisharp/server/v1.32.6/omnisharp/msbuild/15.0/Bin/Roslyn
            CscToolExe = csc.exe
            MSBuildToolsPath = /home/knbr34/.emacs.d/.cache/omnisharp/server/v1.32.6/omnisharp/msbuild/15.0/Bin
            TargetFrameworkRootPath = /usr/lib/mono/xbuild-frameworks
[13:13:58] INFORMATION: OmniSharp.Cake.CakeProjectSystem, Detecting Cake files in '/home/knbr34/dotnet-projects'.
[13:13:58] INFORMATION: OmniSharp.Cake.CakeProjectSystem, Could not find any Cake files
[13:13:58] INFORMATION: OmniSharp.WorkspaceInitializer, Project system 'OmniSharp.DotNet.DotNetProjectSystem' is disabled in the configuration.
[13:13:58] INFORMATION: OmniSharp.MSBuild.ProjectSystem, Detecting projects in '/home/knbr34/dotnet-projects/dotnet-projects.sln'.
[13:13:58] INFORMATION: OmniSharp.MSBuild.ProjectManager, Queue project update for '/home/knbr34/dotnet-projects/SampleProj/SampleProj.csproj'
[13:13:58] INFORMATION: OmniSharp.Script.ScriptProjectSystem, Detecting CSX files in '/home/knbr34/dotnet-projects'.
[13:13:58] INFORMATION: OmniSharp.Script.ScriptProjectSystem, Could not find any CSX files
[13:13:58] INFORMATION: OmniSharp.WorkspaceInitializer, Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpWorkspaceOptionsProvider
[13:13:58] INFORMATION: OmniSharp.WorkspaceInitializer, Configuration finished.
[13:13:58] INFORMATION: OmniSharp.Stdio.Host, Omnisharp server running using Stdio at location '/home/knbr34/dotnet-projects' on host -1.
[13:13:58] The server has started
[13:13:58] INFORMATION: OmniSharp.MSBuild.ProjectManager, Loading project: /home/knbr34/dotnet-projects/SampleProj/SampleProj.csproj
[13:13:58] WARNING: OmniSharp.MSBuild.ProjectManager, Failed to load project file '/home/knbr34/dotnet-projects/SampleProj/SampleProj.csproj'.
[13:13:58] ERROR: OmniSharp.MSBuild.ProjectManager, Attemped to update project that is not loaded: /home/knbr34/dotnet-projects/SampleProj/SampleProj.csproj
[13:14:09] --> 2 /codecheck ((Arguments (FileName)) (Command . "/codecheck") (Seq . 2))

SampleProj.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

</Project>

dotnet-projects.sln (I wondered if this happens only with solutionless projects so I created solution but it doesn't change a thing)


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleProj", "SampleProj\SampleProj.csproj", "{CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Debug|x64 = Debug|x64
        Debug|x86 = Debug|x86
        Release|Any CPU = Release|Any CPU
        Release|x64 = Release|x64
        Release|x86 = Release|x86
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Debug|x64.ActiveCfg = Debug|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Debug|x64.Build.0 = Debug|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Debug|x86.ActiveCfg = Debug|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Debug|x86.Build.0 = Debug|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Release|Any CPU.Build.0 = Release|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Release|x64.ActiveCfg = Release|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Release|x64.Build.0 = Release|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Release|x86.ActiveCfg = Release|Any CPU
        {CEDBB06B-5E53-4DB1-9B10-5D423CA3A261}.Release|x86.Build.0 = Release|Any CPU
    EndGlobalSection
EndGlobal

Program.cs (visible in first log but whatever)

namespace SampleProj
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello world!");
        }
    }
}
razzmatazz commented 5 years ago

Hi. Sorry for writing so late, but hopefully my response still might be helpful to you.

IMHO this appears to be an issue with omnisharp server (or your environment). You are right, that the problematic line in the output is:

[13:13:58] WARNING: OmniSharp.MSBuild.ProjectManager, Failed to load project file '/home/knbr34/dotnet-projects/SampleProj/SampleProj.csproj'.

Sadly, it does not provide any diagnostic info..

My suggestions/questions are:

  1. have you tried loading the project in VSCode or Atom editor (with omnisharp plugin) – it uses the same server as omnisharp-emacs and may provide more diagnostics to figure out what is wrong;
  2. try to change omnisharp-expected-server-version variable from "1.32.6" to any from the https://github.com/OmniSharp/omnisharp-roslyn/releases page; maybe there is a fix for you issue
  3. if that does not help you might want to ask folks on the https://github.com/OmniSharp/omnisharp-roslyn project.
unhammer commented 5 years ago

I'm getting this without spacemacs, same "Failed to load project file" warning without any more info than that, and only syntactic errors shown. Emacs 26.1 with omnisharp-20181206.21 from melpa, omnisharp server is ~/.emacs.d/.cache/omnisharp/server/v1.32.6/run.

I do get errors for other projects (e..g there's a tests project where I get "variable assigned but never used" warnings).

TatriX commented 5 years ago

I managed to fix this by doing:

(let ((dotnet-version (string-trim (shell-command-to-string "dotnet --version"))))
    (setenv "MSBuildSDKsPath" (format "/opt/dotnet/sdk/%s/Sdks" dotnet-version)))

(Adjust path depending on your distro)

unhammer commented 5 years ago

setting MSBuildSDKsPath (to /usr/share/dotnet/sdk/2.2.101/Sdks in my case) didn't help me :(

I tried adding "--loglevel" "debug" in omnisharp--do-server-start and saw:


[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=nn-NO, PublicKeyToken=b03f5f7f11d50a3a'
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, FAILURE: Could not locate '/home/unhammer/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Build.resources.dll'.
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=nn, PublicKeyToken=b03f5f7f11d50a3a'
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, FAILURE: Could not locate '/home/unhammer/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Build.resources.dll'.
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=no, PublicKeyToken=b03f5f7f11d50a3a'
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, FAILURE: Could not locate '/home/unhammer/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Build.resources.dll'.
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=nn-NO, PublicKeyToken=b03f5f7f11d50a3a'
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, FAILURE: Could not locate '/home/unhammer/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Build.resources.dll'.
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=nn, PublicKeyToken=b03f5f7f11d50a3a'
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, FAILURE: Could not locate '/home/unhammer/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Build.resources.dll'.
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, Attempting to resolve 'Microsoft.Build.resources, Version=15.1.0.0, Culture=no, PublicKeyToken=b03f5f7f11d50a3a'
[12:35:49] DEBUG: OmniSharp.MSBuild.Discovery.MSBuildLocator, FAILURE: Could not locate '/home/unhammer/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Build.resources.dll'.
[12:35:49] WARNING: OmniSharp.MSBuild.ProjectManager, Failed to load project file '/home/unhammer/src/Systor.M3WebServices/WSProject/WSProject.csproj'.

There's no such file in .emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/, although it exists in e.g. /usr/share/dotnet/sdk/2.2.101/en/Microsoft.Build.resources.dll. If I $ cp /usr/share/dotnet/sdk/2.2.101/en/Microsoft.Build.resources.dll ~/.emacs.d/.cache/omnisharp/server/v1.32.8/omnisharp/msbuild/15.0/Bin/ it still doesn't work (though it says SUCCESS where it above said FAILURE, I still get Failed to load project file).

TatriX commented 5 years ago

You should try setting (setq omnisharp-debug t) and then look into OmniServer buffer to see server's output.

unhammer commented 5 years ago

I at least see that .emacs.d/.cache/omnisharp/server/v1.32.8/lib/mono/xbuild/Microsoft/VisualStudio/v15.0/WebApplications/Microsoft.WebApplication.targets\" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

This file exists in /usr/lib/mono/xbuild/Microsoft/VisualStudio/v15.0/WebApplications/Microsoft.WebApplication.targets. When I do /usr/bin/msbuild from the command line, it builds fine and seems to use that file.

gl4eqen commented 5 years ago

(let ((dotnet-version (string-trim (shell-command-to-string "dotnet --version")))) (setenv "MSBuildSDKsPath" (format "/opt/dotnet/sdk/%s/Sdks" dotnet-version)))

by @TatriX

This solution PLUS installing msbuild-stable from AUR on Arch Linux helped. I hate those bugs which are a result of incompatibilities between versions. I hope this get fixed somehow in further releases.

I don't really get why dotnet-core eventually depends on Mono. They are completely separate projects. It seems they share the same builder - MSBuild/XBuild ??, this seems to be so complicated, I'd love someone to explain me all those dotnet core / .NET internals :(

Debug outputs in order of applied fixes: Before setting env var MSBuildSDKsPath

After setting env var MSBuildSDKsPath

After installing msbuild-stable

PS. I did

2. try to change omnisharp-expected-server-version variable from "1.32.6" to any from the https://github.com/OmniSharp/omnisharp-roslyn/releases page; maybe there is a fix for you issue

just to be sure I have newest version of omnisharp server running.

unhammer commented 5 years ago

I'm on Ubuntu, so there's no msbuild-stable package, but I do have mono-complete from https://www.mono-project.com/download/stable/ which includes msbuild version 1:16.0+xamarinxplat.2018.09.26.17.53-0xamarin3+ubuntu1804b1.

But it seems your error from missing msbuild-stable was

{"Event":"Error","Body":{"Text":"Microsoft.Build.Exceptions.InvalidProjectFileException: 
The imported project \"/usr/lib/mono/xbuild/15.0/Microsoft.Common.props\" was not found. 
Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.  
/opt/dotnet/sdk/2.2.100/Sdks/Microsoft.NET.Sdk/Sdk/Sdk.props

while without the MSBuildSDKsPath env var it was:

{"Event":"Error","Body":{"Text":"Microsoft.Build.Exceptions.InvalidProjectFileException: 
The SDK 'Microsoft.NET.Sdk' specified could not be found.
unhammer commented 5 years ago

A workaround for my

emacs.d/.cache/omnisharp/server/v1.32.8/lib/mono/xbuild/Microsoft/VisualStudio/v15.0/WebApplications/Microsoft.WebApplication.targets\" was not found. 
Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

was to

cd ~/.emacs.d/.cache/omnisharp/server/v1.32.8/lib/mono/
ln -s /usr/lib/mono/xbuild

– but shouldn't omnisharp have found that path when /usr/bin/msbuild finds it? Or does omnisharp use a different msbuild from the one installed by mono?

gl4eqen commented 5 years ago

@unhammer Ok, now I get it. You CAN'T have mono installed without msbuild-stable when using dotnet-core. Apparently omnisharp searches for toolchain and finds mono (if it is installed) before dotnet-core. If you have dotnet-core only, only setting environment variable is needed.

msbuild-stable adds files to mono directories which (accidentally?) match dotnet-core toolchain. This is why workaround works.