nix-community / nixvim

Configure Neovim with Nix! [maintainers=@GaetanLepage, @traxys, @mattsturgeon, @khaneliman]
https://nix-community.github.io/nixvim
MIT License
1.71k stars 263 forks source link

csharp-ls can't find its references to load #679

Closed loco-choco closed 11 months ago

loco-choco commented 11 months ago

Plugin affected: csharp-ls Nixpkgs channel: unstable Home-manager version: master

Description

After running neovim in a .cs file from a .net framework 4.8 project, it fails to load basic dependencies throwing unusable error warnings on the screen.

image

Here are the :messages it returns:

LSP[csharp_ls][Info] csharp-ls: initializing, csharp-ls version 0.8.0.0; cwd: "/home/locochoco/rw-mods/pd-detection/pd-detection"             
LSP[csharp_ls][Info] csharp-ls: csharp-ls is released under MIT license and is not affiliated with Microsoft Corp.; see https://github.com/raz
zmatazz/csharp-language-server                                                                                                                
LSP[csharp_ls][Info] csharp-ls: MSBuildLocator: will register ".NET Core SDK", Version=7.0.403 as default instance                            
LSP[csharp_ls][Log] csharp-ls: attempting to find and load solution based on cwd ("/home/locochoco/rw-mods/pd-detection/pd-detection")..      
LSP[csharp_ls][Log] csharp-ls: 0 solution(s) found: []                                                                                        
LSP[csharp_ls][Log] csharp-ls: no or multiple .sln files found on /home/locochoco/rw-mods/pd-detection/pd-detection                           
LSP[csharp_ls][Log] csharp-ls: looking for .csproj/fsproj files on /home/locochoco/rw-mods/pd-detection/pd-detection..                        
LSP[csharp_ls][Log] csharp-ls: loading project "/home/locochoco/rw-mods/pd-detection/pd-detection/pd-detection.csproj"..                      
LSP[csharp_ls][Log] csharp-ls: OK, 1 project files loaded                                                                                     
LSP[csharp_ls][Log] csharp-ls: msbuildWorkspace.Diagnostics: [Failure] Msbuild failed when processing the file '/home/locochoco/rw-mods/pd-det
ection/pd-detection/pd-detection.csproj' with message: /nix/store/j5rfwd16fngw915jsxgdfr7009sda5fm-dotnet-core-combined/sdk/7.0.403/Microsoft.
Common.CurrentVersion.targets: (1241, 5): The reference assemblies for .NETFramework,Version=v4.8 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                                                                                                      
LSP[csharp_ls][Log] csharp-ls: msbuildWorkspace.Diagnostics: [Warning] Unresolved metadata reference removed from project: /mscorlib.dll      
"PDDetection.cs" 86L, 3149B written   

I was able to get it to find the mscorlib dependencies by exporting export FrameworkPathOverride=/nix/store/HASH-mono-6.12.0.182/lib/mono/4.8-api before opening the file, but it kept not finding the other references, even though msbuild compiles the solution without an issue.

Config

programs.nixvim = {
  enable = true;
  colorschemes.onedark = { 
    enable = true;
  };
  plugins.lightline.enable = true;
  plugins.coq-nvim.enable = true;
  plugins.lsp = {
    enable = true;
    servers = {
      nixd.enable = true;
      bashls.enable = true;
      clangd.enable = true;
      rust-analyzer.enable = true;
      csharp-ls.enable = true;
    };
  };
  plugins.nvim-cmp = {
    enable = true;
    completion.autocomplete = [ "TextChanged" ];
  };
};
loco-choco commented 11 months ago

Update: exporting export FrameworkPathOverride=/nix/store/HASH-mono-6.12.0.182/lib/mono/4.8-api actually allowed it to find the references in some situations.

GaetanLepage commented 11 months ago

Hmm weird. I am very unfamiliar with csharp. If it happens to be caused by the plugin configuration (or fixable by nixvim) I would be glad to review/merge it.

GaetanLepage commented 11 months ago

Could you try to replicate this bug in a non-nixvim (and even non-nix) environment ? (Same config on a classically installed and configured neovim)

loco-choco commented 11 months ago

I will try on a non nixvim enviroment. It also looks like there is a newer version of the ls that isn't on nixpkgs-unstable, so will also try that.

GaetanLepage commented 11 months ago

I made a PR to update csharp-ls. I completely forgot that I was the maintainer for this !

GaetanLepage commented 11 months ago

Could you please update your flake inputs and re-try ? You should now get the latest version of csharp-ls (0.10.0).

loco-choco commented 11 months ago

Still the same issue sadly.

image

But I think I know why now, I guess it is an issue with dotnet msbuild not working well with .net framework projects in linux, cause it can't find the mono version of the assemblies.

image

Maybe if there was a way to make it look for the mono assemblies, without breaking msbuild, it would make it work, because even on this "broken" state, it can still warn if you are calling a function from a 3rd library wrong.

GaetanLepage commented 11 months ago

Ok, good to know. Anyway, it is not related to nixvim nor the csharp-ls package in nixpkgs. I will thus be closing this issue. Good luck with it !

loco-choco commented 11 months ago

I know the issue is closed, but just found a work around to use dotnet build with mono libraries, and wanted to have it here if someone finds themselves on the same situation. Based on the solution in here, you can have a file called netfx.props in your project, with the contents:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- When compiling .NET SDK 2.0 projects targeting .NET 4.x on Mono using 'dotnet build' you -->
    <!-- have to teach MSBuild where the Mono copy of the reference asssemblies is -->
    <TargetIsMono Condition="$(TargetFramework.StartsWith('net4')) and '$(OS)' == 'Unix'">true</TargetIsMono>

    <!-- Look in the standard install locations -->
    <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/Library/Frameworks/Mono.framework/Versions/Current/lib/mono')">/Library/Frameworks/Mono.framework/Versions/Current/lib/mono</BaseFrameworkPathOverrideForMono>
    <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/usr/lib/mono')">/usr/lib/mono</BaseFrameworkPathOverrideForMono>
    <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/usr/local/lib/mono')">/usr/local/lib/mono</BaseFrameworkPathOverrideForMono>

    <!-- If we found Mono reference assemblies, then use them -->
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net45'">$(BaseFrameworkPathOverrideForMono)/4.5-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net451'">$(BaseFrameworkPathOverrideForMono)/4.5.1-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net452'">$(BaseFrameworkPathOverrideForMono)/4.5.2-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net46'">$(BaseFrameworkPathOverrideForMono)/4.6-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net461'">$(BaseFrameworkPathOverrideForMono)/4.6.1-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net462'">$(BaseFrameworkPathOverrideForMono)/4.6.2-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net47'">$(BaseFrameworkPathOverrideForMono)/4.7-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net471'">$(BaseFrameworkPathOverrideForMono)/4.7.1-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND '$(TargetFramework)' == 'net48'">$(BaseFrameworkPathOverrideForMono)/4.8-api</FrameworkPathOverride>
    <EnableFrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != ''">true</EnableFrameworkPathOverride>

    <!-- Add the Facades directory.  Not sure how else to do this. Necessary at least for .NET 4.5 -->
    <AssemblySearchPaths Condition="'$(BaseFrameworkPathOverrideForMono)' != ''">$(FrameworkPathOverride)/Facades;$(AssemblySearchPaths)</AssemblySearchPaths>
  </PropertyGroup>
</Project>

And import it on your .csproj with the <Import Project="./netfx.props" /> line. It should now allow dotnet build to find the build dependencies, and thus allow csharp-ls to work.