dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version #65624

Open benjamin-asdf opened 1 year ago

benjamin-asdf commented 1 year ago

Version Used:

4.4.0

Steps to Reproduce:

  1. var instance = MSBuildLocator.QueryVisualStudioInstances().First();
    MSBuildLocator.RegisterInstance(instance);

    this registers /usr/share/dotnet/sdk/7.0.100/

dotnet --list-sdks
3.1.424 [/usr/share/dotnet/sdk]
6.0.403 [/usr/share/dotnet/sdk]
7.0.100 [/usr/share/dotnet/sdk]

The essential thing for me is chsarp 9 language features.

  1. open solution, GetCompilationFromSolution

TestProj.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

TestProject.sln

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject.csproj", "{A0F68BB9-30D4-432C-87E2-F307EF0D3439}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Release|Any CPU = Release|Any CPU
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {A0F68BB9-30D4-432C-87E2-F307EF0D3439}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {A0F68BB9-30D4-432C-87E2-F307EF0D3439}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {A0F68BB9-30D4-432C-87E2-F307EF0D3439}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {A0F68BB9-30D4-432C-87E2-F307EF0D3439}.Release|Any CPU.Build.0 = Release|Any CPU
    EndGlobalSection
EndGlobal

Program.cs

Console.WriteLine("Hello, World!");

Expected Behavior:

Better error that tells me for example

  1. what sdk was needed
  2. what sdk it had
  3. what language version it wanted to compile with
  4. a link to an overview of language features, sdk versions, roslyn versions...

Maybe this could say "consider installing sdk XXX and make sure you are registering the instance"

Actual Behavior:

There is a hard to understand low level error

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

It already took me shit tons of sweat to understand this has something to do with MSBuildLocator.

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

svick commented 1 year ago

Could you provide all the code required to reproduce this? Probably the best option is to create a GitHub repo or attach a zip file here.

benjamin-asdf commented 1 year ago

Turns out this is fixed with these versions:

    <PackageReference Include="Microsoft.Build" Version="17.4.0" ExcludeAssets="runtime" />
    <PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
    <PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.4.0" />

it was an older combination of versions that made the error.

        <MicrosoftBuildLocatorVersion>1.2.6</MicrosoftBuildLocatorVersion>
        <MicrosoftCodeAnalysisVersion>3.6.0</MicrosoftCodeAnalysisVersion>
        <MicrosoftBuildVersion>16.5.0</MicrosoftBuildVersion>