question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

18reactions
AndrewBabbitt97commented, Mar 4, 2022

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.

9reactions
Piedonecommented, Mar 11, 2022

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>
Read more comments on GitHub >

github_iconTop Results From Across the Web

"csc.exe" exited with code -2146232797 ("CompilerServer: ...
Core.dll" does not exist. 1> Using "Csc" task from assembly "C:\Program Files\VS2022P\MSBuild\Current\Bin\Roslyn\Microsoft.Build.Tasks.
Read more >
C# 11 and . NET 7 - Modern Cross-Platform Development ...
NET SDK 6.0.200 and later, which was released in February 2022, you can set the language version to preview to start exploring C#...
Read more >
Apps and Services With . NET 7
Dynamically loading assemblies and executing methods • 233 ... Windows App SDK (formerly WinUI 3 and Project Reunion) apps, 2021.
Read more >
Net | PDF | Xml | Microsoft Visual Studio
Collect detailed assembly loading information. Tutorials ... NET SDK, you don't need to install the ... NET Core installer like Visual Studio does,...
Read more >
Apps and Services with .NET 7. Build practical projects ...
Service technologies: How to build and secure services with ASP.NET Core Web API Minimal APIs, OData, GraphQL, gRPC, SignalR, and Azure Functions. 4....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found