Skip to content

Resolving Analyzer Assembly References Newer Compiler Version Error ​

Problem Statement ​

When working with .NET projects in Visual Studio (particularly .NET 6 projects after installing newer SDKs), you may encounter a compiler version mismatch error like:

CS9057 The analyzer assembly '...\Microsoft.NET.Sdk.Razor.SourceGenerators.dll' 
references version '4.7.0.0' of the compiler, which is newer than the 
currently running version '4.6.0.0'.

This occurs because:

  1. A newer .NET SDK (e.g., .NET 8/9 preview) is installed alongside your project's target SDK (.NET 6/7)
  2. The compiler automatically selects the latest SDK, causing conflicts
  3. Analyzer assemblies from the newer SDK require a newer compiler version than currently loaded

Common triggers:

  • Installing a .NET preview SDK alongside stable versions
  • Updating Visual Studio without matching SDK updates
  • Having multiple SDK versions installed without explicit version pinning

Choose the method that best fits your development environment:

1. Pin SDK Version with global.json (Most Reliable) ​

Create a global.json file in your solution root to specify the exact SDK version your project should use.

bash
# Navigate to solution root
dotnet new globaljson --sdk-version 6.0.400  # Use your required SDK version

Example file content:

json
{
  "sdk": {
    "version": "6.0.426",  // Match your installed SDK version
    "rollForward": "latestMinor"
  }
}
Verifying SDK versions
Run dotnet --list-sdks to see installed versions. Choose the highest stable version matching your project's target framework.

2. Update Visual Studio and SDKs ​

  1. Update Visual Studio: Help → Check for Updates
  2. Install latest SDK for target framework:
  3. Restart your computer
  4. Clean project: Delete bin and obj folders

3. Uninstall Conflicting SDKs ​

For projects that don't require newer SDKs:

  1. Open Windows "Add or remove programs"
  2. Uninstall preview or conflicting SDK versions
  3. Reinstall required stable SDK version
  4. Restart Visual Studio

4. Special Case: Build Pipelines (Azure DevOps) ​

Adjust pipeline configuration:

yaml
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '6.0.400'  # Match project SDK

- task: DotNetCoreCLI@2  # Instead of VSBuild@1
  inputs:
    command: 'build'
    projects: '**/*.csproj'

5. Advanced: MSBuild Property Workaround ​

Add this to your .csproj file as a temporary fix:

xml
<PropertyGroup>
  <!-- Add this line -->
  <BuildWithNetFrameworkHostedCompiler>true</BuildWithNetFrameworkHostedCompiler>
  
  <TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
Caution
Use compiler workarounds only as temporary solutions. They bypass version safety checks and may introduce instability.

Troubleshooting Guide ​

SymptomSolution
Error persists after global.jsonVerify file location (solution root)
Multiple SDKs in CI/CD pipelinesExplicitly specify SDK in pipeline tasks
JetBrains Rider usersConfigure SDK in Settings → Build → Toolset
.NET 8 specific issuesSee known issues documentation

Prevention Best Practices ​

  1. Always use global.json in projects with multiple SDK installations
  2. Maintain SDK version parity between development machines and CI/CD environments
  3. Separate preview SDKs using virtual machines or containers
  4. Regularly clean artifacts:
    powershell
    Get-ChildItem -Directory -Recurse -Include bin, obj | Remove-Item -Recurse -Force
  5. Update all NuGet packages regularly: dotnet outdated -u
Pro Tip
Install SDK version managers like dotnet-install scripts for better version control across projects.

Additional Resources ​

By implementing these solutions, you'll resolve version conflicts while maintaining a stable, version-consistent development environment across machines and pipelines.