dotnet / format

Home for the dotnet-format command
MIT License
1.93k stars 173 forks source link

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

Open DavidZidar opened 2 years ago

DavidZidar commented 2 years ago

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 commented 2 years ago

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

CaringDev commented 2 years ago

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

DavidZidar commented 2 years ago

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

jaredthirsk commented 2 years ago

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

Franklin89 commented 2 years ago

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 commented 2 years ago

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

DavidZidar commented 2 years ago

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

alefranz commented 2 years ago

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?

Franklin89 commented 2 years ago

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

JoeRobich commented 2 years ago

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.

Franklin89 commented 2 years ago

@JoeRobich Thank you for the clarification 💯

alefranz commented 2 years ago

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

jonasmidstrup commented 2 years ago

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

iSazonov commented 1 year ago

The same in 8.0-Preview1.

michaelplavnik commented 1 year ago

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.

spaasis commented 11 months ago

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
niemyjski commented 8 months ago

Please fix this.