format icon indicating copy to clipboard operation
format copied to clipboard

dotnet format does not respect global.json and breaks when preview versions of .NET is installed

Open DavidZidar opened this issue 3 years ago • 20 comments
trafficstars

I am unable to use dotnet format in my projects because it throws the following error:

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

This seems to be because it is trying to load assemblies from .NET 6.0.200 preview even though I have the following global.json in my project:

{
  "sdk": {
    "version": "6.0.100",
    "allowPrerelease": false,
    "rollForward": "latestFeature"
  }
}

The following log output illustrates the problem:

> dotnet format -v diag --verify-no-changes .\project.sln

  The dotnet runtime version is '6.0.1'.
  The dotnet CLI version is '6.0.101'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\'.
  Formatting code files in workspace 'C:\code\projects\myproject\myproject.sln'.
  Loading workspace.
  Project MyProject is using configuration from 'C:\code\projects\.editorconfig'.
  Project MyProject is using configuration from 'C:\code\projects\myproject\.editorconfig'.
  Project MyProject is using configuration from 'C:\code\projects\myproject\src\MyProject\obj\Debug\net6.0\MyProject.GeneratedMSBuildEditorConfig.editorconfig'.
  Project MyProject is using configuration from 'C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\Sdks\Microsoft.NET.Sdk\analyzers\build\config\analysislevel_6_all.editorconfig'.
# ...truncated...
  Complete in 2975ms.
  Determining formattable files.
  Complete in 542ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 33 analyzers on MyProject.
# ...truncated...
  Complete in 2577ms.
  Analysis complete in 2578ms.
Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.Assembly.GetTypes()
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFinderHelpers.<>c.<LoadAnalyzersAndFixers>b__0_0(Assembly assembly)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.OfTypeIterator[TResult](IEnumerable source)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
   at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1 items)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFinderHelpers.LoadAnalyzersAndFixers(IEnumerable`1 assemblies)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerReferenceInformationProvider.GetAnalyzersAndFixers(Project project)
   at System.Collections.Immutable.ImmutableDictionary.<>c__DisplayClass9_0`3.<ToImmutableDictionary>b__0(TSource element)   at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 items, MutationInput origin, KeyCollisionBehavior collisionBehavior)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs, Boolean avoidToHashMap)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs)
   at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 keyComparer, IEqualityComparer`1 valueComparer)
   at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerReferenceInformationProvider.GetAnalyzersAndFixers(Solution solution, FormatOptions formatOptions, ILogger logger)
   at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFormatter.FormatAsync(Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.RunCodeFormattersAsync(Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
   at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext()--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

DavidZidar avatar Jan 22 '22 16:01 DavidZidar

I had to work around this issue by completely uninstalling Visual Studio 2022 preview.

DavidZidar avatar Jan 22 '22 17:01 DavidZidar

It seems this is intended: #1292 last bullet point. It's painful for me too, but then I don't know the reasons behind 🤷

CaringDev avatar Jan 31 '22 18:01 CaringDev

@CaringDev Oh damn, thanks for that link. That's just broken by design then. :(

DavidZidar avatar Jan 31 '22 20:01 DavidZidar

Workaround: In C:\Program Files\dotnet\sdk, temporarily rename 6.0.200 to x6.0.200 while I don't need it.

jaredthirsk avatar Feb 03 '22 23:02 jaredthirsk

This also has an impact on the GitHub Actions runner. They have been updated to the lastest SDK an now the dotnet format analyzers command is broken.

Franklin89 avatar Feb 16 '22 07:02 Franklin89

Any news on this? This is pretty annoying to work with.

Franklin89 avatar Jun 20 '22 11:06 Franklin89

Agreed, I had to uninstall VS Preview again the other day because of this.

DavidZidar avatar Jun 20 '22 11:06 DavidZidar

Any plan to fix this or introduce some sort of workaround to allow to use dotnet format when you have a .NET preview version installed?

alefranz avatar Jun 30 '22 10:06 alefranz

@JoeRobich can someone at least look at this, or give some feedback please.

Franklin89 avatar Jun 30 '22 10:06 Franklin89

The correct dotnet-format is being run based on global.json, but the issue here is that we are taking the highest versioned MSBuild (and thus SDK) returned by MSBuildLocator. This is a behavior that was quite useful when we shipped only as a dotnet tool but is undesirable now that we are in the SDK. This is something I can look at in the July development sprint.

JoeRobich avatar Jun 30 '22 17:06 JoeRobich

@JoeRobich Thank you for the clarification 💯

Franklin89 avatar Jul 01 '22 04:07 Franklin89

A possible workaround is to install it as global tool

dotnet tool install -g dotnet-format --version "7.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json

(not sure if there is a different feed where 6.* is available)

and then running it as dotnet-format

alefranz avatar Jul 01 '22 13:07 alefranz

Any news on this one? Can't find any PR for this.

jonasmidstrup avatar Aug 17 '22 13:08 jonasmidstrup

The same in 8.0-Preview1.

iSazonov avatar Mar 07 '23 14:03 iSazonov

It is not only the issue of preview version. Any SDK with only patch level difference is affected. E.g. 6.0.122 and 6.0.401.

michaelplavnik avatar Sep 27 '23 20:09 michaelplavnik

This breaks our pull request build pipelines since the formatting in dotnet 8 seems to be a bit different from dotnet 6 which our global.json defines as the sdk. Any updates on an actual fix?

A possible workaround is to add this to your pipeline before the formatting:

  - task: UseDotNet@2
    inputs:
      packageType: 'sdk'
      useGlobalJson: true

spaasis avatar Nov 21 '23 08:11 spaasis

Please fix this.

niemyjski avatar Feb 02 '24 19:02 niemyjski