dotnet / format

Home for the dotnet-format command
MIT License
1.94k stars 172 forks source link

dotnet format exception with source generator #1461

Open WeihanLi opened 2 years ago

WeihanLi commented 2 years ago

Describe the bug

When I use the dotnet format command to format code, I got some errors Could not load file or assembly, it may relate to source generator.

To Reproduce

There's a sample Github-Action CI https://github.com/OpenReservation/ReservationServer/runs/4492767423, and the project could be built locally https://github.com/OpenReservation/ReservationServer/tree/78dda500c1e93002d53e2c61a3b6591b33b490e3

Exceptions

image

Further technical details

.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22518
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  2.1.802 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  3.1.300 [C:\Program Files\dotnet\sdk]
  3.1.301 [C:\Program Files\dotnet\sdk]
  3.1.412 [C:\Program Files\dotnet\sdk]
  5.0.400 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21458.32 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.2.21505.57 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
trejjam commented 2 years ago

I have the same issue. I am sure it is bound to a source generator.

Did you find any work-around?

trejjam commented 2 years ago

I have a stack trace if it helps. (Project build works normaly)

Could not load file or assembly 'Dependency.Of.Source.Generation, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null'. 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.FormatAnalyzersCommand.FormatAnalyzersHandler.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()
WeihanLi commented 2 years ago

Did you find any work-around?

No, I did not find any work-around for now.

JoeRobich commented 2 years ago

Some general thoughts on source generator support in dotnet-format. It may not be obvious but dotnet-format does not build your project or solution. If your project has a project reference that is a source generator, then it will be necessary to dotnet build that project prior to running dotnet format on your project or solution. dotnet-format will then try and load the compiled source generator from the projects output folder. If there are any required dependencies they need to be present in the output folder.

I've done a little investigation into the ReservationServer issue and it seems to be falling over trying to load the dependencies. I wonder if it is related to https://github.com/dotnet/docs/pull/25785. I'll have to resume the investigation after the holiday break.

WeihanLi commented 2 years ago

Great thanks for your effort @JoeRobich , while I already had a dotnet build step before the dotnet format in the Github Action, and I try to add the <EnableDynamicLoading>true</EnableDynamicLoading> to my root Directory.Build.props, but seemed it did not work, thanks for your time and have a nice holiday.

ArinGhazarian commented 2 years ago

👋 @WeihanLi, I think I have a similar issue, already filed an issue but I guess it is basically the same bug.

Sebazzz commented 2 years ago

Also running into this. This started with the .NET 6 SDK - we had no issues with dotnet-format 5.x.

gabrielmaldi commented 2 years ago

This happens on projects which use Refit v6.2.16 with .NET 5 and dotnet-format 5.1.250801 (even running dotnet build beforehand).

MarkusRodler commented 2 years ago

I've also ran into this issue while trying to lint a source generator in combination with a background worker. It took some time to investigate this issue but now I have minimal repository that reproduces this issue: https://github.com/MarkusRodler/workertest/runs/8049182548?check_suite_focus=true

Fun fact: Linting works with the old version of dotnet-format as you can see in the workflow run above. I hope you can help us with this issue @JoeRobich 😄

MariuszKogut commented 1 year ago

Hm - also having this issue in combination with https://github.com/ChilliCream/graphql-platform/tree/main/src/StrawberryShake CodeGenerators. Please fix!

sharwell commented 1 year ago

Referencing the following project: https://github.com/OpenReservation/ReservationServer/tree/e964038039c5dae27c5bc524eb9c018dfd4b1b60

I would not expect this solution to work in its current form. At first glance, errors include:

  1. OpenReservation.Business references a source generator contained in OpenReservation.Database: https://github.com/OpenReservation/ReservationServer/blob/e964038039c5dae27c5bc524eb9c018dfd4b1b60/OpenReservation.Business/OpenReservation.Business.csproj#L13

    However, this source generator project contains dependencies which are not provided to the consuming project.

  2. OpenReservation.Database contains a source generator, but does not target netstandard2.0. Source generators are required to target netstandard2.0 (nothing earlier or later).

WeihanLi commented 1 year ago

Referencing the following project: https://github.com/OpenReservation/ReservationServer/tree/e964038039c5dae27c5bc524eb9c018dfd4b1b60

I would not expect this solution to work in its current form. At first glance, errors include:

  1. OpenReservation.Business references a source generator contained in OpenReservation.Database: https://github.com/OpenReservation/ReservationServer/blob/e964038039c5dae27c5bc524eb9c018dfd4b1b60/OpenReservation.Business/OpenReservation.Business.csproj#L13 However, this source generator project contains dependencies which are not provided to the consuming project.
  2. OpenReservation.Database contains a source generator, but does not target netstandard2.0. Source generators are required to target netstandard2.0 (nothing earlier or later).

Actually, the source generator does work for me, I changed back to T4 to makes the dotnet format work in https://github.com/OpenReservation/ReservationServer/commit/7e7f5d7bcb6c7e849a7c24db5eab9cec12a045ad, the commit for SourceGenerator should be https://github.com/OpenReservation/ReservationServer/tree/78dda500c1e93002d53e2c61a3b6591b33b490e3