format: dotnet format is broken on sdk 7.0.200 if `EnforceCodeStyleInBuild` is set to `true` – Could not load file or assembly 'Microsoft.CodeAnalysis'

step to reproduce

  • create new project
dotnet new console
  • add <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> to .csproj

  • add global.json

dotnet new globaljson
  • change sdk version in global.json to “7.0.103”
  • run
dotnet format

everything is ok here.

  • change sdk version in global.json to “7.0.200”
  • run
dotnet format

and now there are a lot of

Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

in output

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 36
  • Comments: 28 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Is there any Road Map for fixing this issue?

.NET 7.0.4 is installed automatically with Visual Studio 2022 and for the past 4 months bundled dotnet-format is unusable. #1782 Seems to be forgotten.

This seems to be an issue on 7.0.202 and 7.0.203 as well.

First off, apologies for the delays in getting this fixed! We have had bit of personnel churn in this space, but we have a fix checked in on the format side now and it is queued up to catch the next SDK servicing train that is currently planned for 7.0.3xx .NET SDK. I will circle back once I have a confirmation that the fix has been released. In the interim, dropping down to 7.0.2xx to use the dotnet format would be the temporary workaround.

Thanks for that! Is there any way to get automated tests for this? I think this is the second or third time dotnet format has broken completely since 6.0 for roughly the same reason.

#1825 Merged

Both this problem and dotnet/sdk#32598 seem to have been fixed in sdk 7.0.304. I re-enabled EnforceCodeStyleInBuild, removed all workarounds and downgrades which I had been using for 4 months and can once again enjoy dotnet format as is. Hope it’s not just me. Thank you!

@avidenic .NET8 also

@RikkiGibson Do you know if there are plans to assign a new maintainer to this project?

I suspect that this is caused by dotnet-format’s reference to the compiler getting out of date. #1782

@arkalyanms when will this be fixed? This has been broken for months.

I’ve been getting this using .NET 8 in a dev container where .NET was installed using dotnet-install.sh.

The project it runs into an exception while trying to format is an Incremental Source Generator.

Solution: https://github.com/LorettaDevs/Loretta (devcontainer files are contained within) Project: Loretta.Generators.SyntaxFactsGenerator Format command: dotnet format (v8.0.405501+2cb3e68c6b9a966114572fd63f2a20d2cb54a288) Output: https://gist.github.com/GGG-KILLER/81a9773f7c02a12a841edd331751d2ee

dotnet --info
$ dotnet --info
.NET SDK:
 Version:   8.0.100-preview.2.23157.25
 Commit:    54801b2435

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  22.04
 OS Platform: Linux
 RID:         ubuntu.22.04-x64
 Base Path:   /usr/local/dotnet/current/sdk/8.0.100-preview.2.23157.25/

Host:
  Version:      8.0.0-preview.2.23128.3
  Architecture: x64
  Commit:       30b879924a

.NET SDKs installed:
  6.0.407 [/usr/local/dotnet/current/sdk]
  7.0.202 [/usr/local/dotnet/current/sdk]
  8.0.100-preview.2.23157.25 [/usr/local/dotnet/current/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.15 [/usr/local/dotnet/current/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.4 [/usr/local/dotnet/current/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.2.23153.2 [/usr/local/dotnet/current/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.15 [/usr/local/dotnet/current/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.4 [/usr/local/dotnet/current/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.2.23128.3 [/usr/local/dotnet/current/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download