sdk: .NET Core SDK 6.0.200: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0'

Describe the bug

Build fails with error: ##[error]CSC(0,0): Error CS8032: An instance of analyzer Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator cannot be created from C:\hostedtoolcache\windows\dotnet\sdk\6.0.200\Sdks\Microsoft.NET.Sdk.Razor\source-generators\Microsoft.NET.Sdk.Razor.SourceGenerators.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.. when using .NET Core SDK 6.0.200 with Visual Studio 2022 on Azure DevOps Microsoft-Hosted Agent.

Error goes away after reverting to .NET Core SDK 6.0.102.

I was unable to reproduce this on my personal Windows 11 machine using Visual Studio 2022 (with latest updates) and running a build command very similar to what Azure DevOps does: "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\msbuild.exe" "C:\Users\paulmalmsten\source\repos\Terrapin\PackageRebuilder.sln" /nologo /nr:false -m /p:configuration="debug" /p:VisualStudioVersion="17.0" /p:_MSDeployUserAgent="VSTS_011b8bdf-6d56-4f87-be0d-0092136884d9_build_11835_0"

Azure DevOps image version where this was observed: https://github.com/actions/virtual-environments/releases/tag/win22%2F20220207.1

To Reproduce

I was able to reproduce this issue on a template Web App project created by Visual Studio 2022 without modification.

Steps:

  1. Follow these instructions for creating a Web App project from a template.
  2. Check the project into Git (in my example, under the WebApplication1 directory below the git repository root), push to a branch in Azure Dev Ops.
  3. Configure an Azure DevOps Pipeline which runs on windows-2022 having the following steps:
- task: NuGetToolInstaller@1
  displayName: 'Install NuGet 6.x'
  inputs:
    versionSpec: 6.x

- task: UseDotNet@2
  displayName: 'Add .NET 5.0'  
  inputs:
    packageType: 'sdk'
    version: '5.0.x'

- task: UseDotNet@2
  displayName: 'Add .NET 6.0'  
  inputs:
    packageType: 'sdk'
    version: '6.0.x'

- task: NuGetCommand@2
  displayName: 'NuGet Restore'
  inputs:
    restoreSolution: '$(Build.SourcesDirectory)\WebApplication1\WebApplication1.sln'

- task: VSBuild@1
  displayName: 'Build the solution'
  inputs:
    solution: '$(Build.SourcesDirectory)\WebApplication1\WebApplication1.sln'
    configuration: 'debug'
    msbuildArgs: '-m'
  1. Run the Azure DevOps Pipeline.

Expected Result: Compiles successfully. Actual result: Error message seen above.

Exceptions (if any)

Not applicable.

Further technical details

  • Include the output of dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.200
 Commit:    4c30de7899

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.20348
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\hostedtoolcache\windows\dotnet\sdk\6.0.200\

Host (useful for support):
  Version: 6.0.2
  Commit:  839cdfb0ec

.NET SDKs installed:
  5.0.405 [C:\hostedtoolcache\windows\dotnet\sdk]
  6.0.200 [C:\hostedtoolcache\windows\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.14 [C:\hostedtoolcache\windows\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.2 [C:\hostedtoolcache\windows\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.14 [C:\hostedtoolcache\windows\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.2 [C:\hostedtoolcache\windows\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.14 [C:\hostedtoolcache\windows\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.2 [C:\hostedtoolcache\windows\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version

Not applicable. Error happens on Azure DevOps hosted agent.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 40 (8 by maintainers)

Commits related to this issue

Most upvoted comments

This error can crop up on the CLI as well, it’s not restricted to Visual Studio environments. So I agree with the above. Issue is not yet resolved.

A workaround is to add this to your csproj:

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
    </PackageReference>
  </ItemGroup>

I have 17.1.0 and am still hitting the issue. So I don’t agree that it should be closed.

Closing as this seem to be fixed already. Thanks for confirmation, @andschwa

Hitting same issue with .NET SDK 6.0.400 and 6.0.408. This is definitely not fixed yet!

Same issue here. It happend on the build server, but not locally.

For those also affected: The workaround from pmalmsten works:

global.json: { "sdk": { "version": "6.0.100", "rollForward": "disable" } }

.yaml for the build pipelines: task: UseDotNet@2 displayName: 'Use .NET Core SDK 6.0' inputs: packageType: sdk version: 6.0.100

HIH

VS2022 v17.0.5 was having this issue and upgrading to v17.1.0 fixed it.

Thank you @El-Gor-do

The upgrade from 17.0.0 to 17.1.0 also fixed it for me. Seems to have been an issue on the part of Visual Studio (or the csc executable provided by VS), since I specified the SDK version (6.0.200) in my global.json.

I think this issue can be closed.

This issue was fixed in OmniSharp v1.38.1 (2022-02-18) , but in VSCode Extension Marketplace latest available release is 1.24.0. Potentially latest omnisharp version can be installed manually from github build, but I hope, that latest release will be available for download in Marketplace soon.

An update to my previous findings related to removing the two “Microsoft.CodeAnalysis.Compilers” and “Microsoft.Net.Compilers.Toolset” NuGet packages; This worked flawlessly in one Azure DevOps build pipeline, but we later discovered that it was indeed failing the original way in another. That is, with the symptom of not embedding the ASP.NET MVC Core views in the assembly.

That led me to look at which tools were actually used to build in those two cases. The one that worked used a manual “dotnet publish” command in a Dockerfile (with implicit “dotnet build”), using the .NET SDK version 7.0.400. To actually build, it uses its bundled MSBuild version 17.7.1.

The one that didn’t work, on the other hand, was using the “Visual Studio build” task available to Azure DevOps pipelines. It was configured to use the “Latest” version of Visual Studio, i.e. 2022, effectively getting the 17.6.6 version. Once more, to actually build, it also uses its bundled MSBuild, but this time a fairly older 17.6.3 version.

So, to conclude, the MSbuild tool actually seems to be the problem here, and you will need to use a new enough version of it. Why the latest VS would remain so far behind the latest available MSBuild is a mystery to me.

I confirmed that the dotnet CLI upgrade to SDK 6.0.202 has fixed this.

We’re also seeing this trying to build a .NET 6 Blazor app on a hosted agent using image ‘windows-2022’ (with image version ‘20220207.1’), and also using .NET SDK 6.0.200. It started happening as soon as the .NET SDK version moved to 6.0.200 (didn’t happen on SDK 6.0.102).

Even better, build msbuild and all the tools you need yourself for the absolute latest version.

A workaround is to add this to your csproj:

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
    </PackageReference>
  </ItemGroup>

Dang. I got excited. But, that didn’t work for me.

This same issue is breaking dotnet-format: https://github.com/dotnet/format/issues/1519

same here, omnisharp in vscode is broken on 6.0.200 with this exception.

I had the same problem with CS8032 warnings when building in Visual Studio 2022 17.0.6 using SDK 6.0.200. After upgrading to Visual Studio 2022 17.1.0, the CS8032 warnings disappeared.