martinothamar / Mediator

A high performance implementation of Mediator pattern in .NET using source generators.
MIT License
2.12k stars 70 forks source link

XYZ encountered an error and has been disabled + SyntaxTree is not part of the compilation #93

Closed michaeljfarr closed 1 year ago

michaeljfarr commented 1 year ago

Somehow the Mediator generator has stopped working on my machine on visual studio 2022 17.6.0 and later versions. Possibly this only happened after an update previously, I am not sure. Everything still works on Rider, so clearly it is something to do with the interaction with the Mediator codegen and visual studio. Can you advise what I can do to diagnose this further?

When I open the solution, I see a lot of "XYZ encountered an error and has been disabled" messages at the top of visual studio. These XYZ are related to various parts of the RefactoringProvider and all have the exception trace as shown below. I also see some features with the same issue and stack trace (CodeLens references .. ).

If I remove the Mediator code generator from the solution I do not see any of these issues, but obviously, I can't initialise the mediator, so nothing works in a useful way.

If I look at the IncrementalMediatorGenerator Analyzer in solution explorer, the spinning wheel continues forever, but nothing is generated.

I have tried reinstalling visual studio and rebooting and disabling resharper ... any other suggestions?

StreamJsonRpc.RemoteInvocationException: SyntaxTree is not part of the compilation (Parameter 'syntaxTree') at StreamJsonRpc.JsonRpc.d__1511.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.ValueTask1.get_Result() at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection1.<TryInvokeAsync>d__181.MoveNext() RPC server exception: System.ArgumentException: SyntaxTree is not part of the compilation (Parameter 'syntaxTree') at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSemanticModel(SyntaxTree syntaxTree, Boolean ignoreAccessibility) at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CommonGetSemanticModel(SyntaxTree syntaxTree, Boolean ignoreAccessibility) at Microsoft.CodeAnalysis.Diagnostics.SuppressMessageAttributeState.IsDiagnosticSuppressed(Diagnostic diagnostic, SuppressMessageInfo& info) at Microsoft.CodeAnalysis.Diagnostics.SuppressMessageAttributeState.ApplySourceSuppressions(Diagnostic diagnostic) at Microsoft.CodeAnalysis.GeneratorDriver.FilterDiagnostics(Compilation compilation, ImmutableArray1 generatorDiagnostics, DiagnosticBag driverDiagnostics, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.GeneratorDriver.RunGeneratorsCore(Compilation compilation, DiagnosticBag diagnosticsBag, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.GeneratorDriver.RunGenerators(Compilation compilation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.FinalizeCompilationAsync(SolutionState solution, Compilation compilationWithoutGenerators, CompilationTrackerGeneratorInfo generatorInfo, Compilation compilationWithStaleGeneratedTrees, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.BuildCompilationInfoFromScratchAsync(SolutionState solution, CompilationTrackerGeneratorInfo generatorInfo, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.BuildCompilationInfoAsync(SolutionState solution, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetOrBuildCompilationInfoAsync(SolutionState solution, Boolean lockGate, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetCompilationSlowAsync(SolutionState solution, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.GetMetadataReferenceAsync(ICompilationTracker tracker, ProjectState fromProject, ProjectReference projectReference, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.FinalizeCompilationAsync(SolutionState solution, Compilation compilationWithoutGenerators, CompilationTrackerGeneratorInfo generatorInfo, Compilation compilationWithStaleGeneratedTrees, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.BuildCompilationInfoFromScratchAsync(SolutionState solution, CompilationTrackerGeneratorInfo generatorInfo, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.BuildCompilationInfoAsync(SolutionState solution, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetOrBuildCompilationInfoAsync(SolutionState solution, Boolean lockGate, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetCompilationSlowAsync(SolutionState solution, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Razor.TagHelperResolver.GetTagHelpersAsync(Project workspaceProject, RazorProjectEngine engine, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.Razor.RemoteTagHelperProviderService.GetTagHelpersCoreAsync(RazorPinnedSolutionInfoWrapper solutionInfo, ProjectSnapshotHandle projectHandle, String factoryTypeName, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.Razor.RemoteTagHelperProviderService.GetTagHelpersDeltaCoreAsync(RazorPinnedSolutionInfoWrapper solutionInfo, ProjectSnapshotHandle projectHandle, String factoryTypeName, Int32 lastResultId, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunServiceImplAsync[T](Func2 implementation, CancellationToken cancellationToken)

panoukos41 commented 1 year ago

I am also having the exact same problem :/

martinothamar commented 1 year ago

Thanks for the report! Will have a look at this tomorrow

panoukos41 commented 1 year ago

Thanks for the fast response, I have pushed my changes in this repo at the refactor branch.

The project that uses the package is FoodOnline.Infra

It makes VS (latest & preview) to have these errors. I believe it has something to do with the generator depnendencies I will try to look into it too 😄

michaeljfarr commented 1 year ago

Possibly related to this: https://developercommunity.visualstudio.com/t/TextTransformationGenerationEnvironment/10369514

martinothamar commented 1 year ago

Sorry about the delay, digging into this now. Just to clarify, the behavior I'm seeing is that the project builds fine but VS breaks (and we can see that it breaks when getting the semantic model after having run source generators). I tested this with @panoukos41 project in the refactor branch, by adding the latest 2.1 Mediator versions and running the latest VS and dotnet previews

michaeljfarr commented 1 year ago

Hi Martin. For me, the code generator fails, so I dont get the AddMediator extension and therefore the solution doesn't build because of that.

martinothamar commented 1 year ago

I see, what is the error from the Mediator source generator? I don't see Mediator as part of the stacktrace above?

michaeljfarr commented 1 year ago

Ah - is it not happening for you? I am running Visual Studio Community 2022 17.6.2. Its probably not fixable if you can't repro easily.

I wasn't able to find find a stacktrace that included Mediator. I am going through all the errors that come up in the error bar at above the content tabs in visual studio (as shown in the other issue #96). Is there somewhere else to look for a more helpful stack trace?

martinothamar commented 1 year ago

No, I'm debugging based on the repro that @panoukos41 provided. I've made some small modifications to trigger the issue. I'm at a state now where I have included the projects into my local Mediator branch and referenced the source generators directly, and I have the VS issue, but the project builds (like dotnet build succeeds). The sourcegenerators run normally in this repro, i.e. running through the generation process in a debugger is fine (no errors or anything unexpected happens)

Here is a related issue that hasn't got much progress yet: https://developercommunity.visualstudio.com/t/Feature-is-currently-unavailable-due-to-/10344587

So, next steps - try to find some correlation between diagnostics and sourcegeneration. In particular I'm digging into this part of the stacktrace

System.ArgumentException: SyntaxTree is not part of the compilation (Parameter 'syntaxTree')
      at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSemanticModel(SyntaxTree syntaxTree, Boolean ignoreAccessibility)
      at Microsoft.CodeAnalysis.Diagnostics.SuppressMessageAttributeState.IsDiagnosticSuppressed(Diagnostic diagnostic, SuppressMessageInfo& info)
      at Microsoft.CodeAnalysis.Diagnostics.SuppressMessageAttributeState.ApplySourceSuppressions(Diagnostic diagnostic)
      at Microsoft.CodeAnalysis.GeneratorDriver.FilterDiagnostics(Compilation compilation, ImmutableArray`1 generatorDiagnostics, DiagnosticBag driverDiagnostics, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.GeneratorDriver.RunGeneratorsCore(Compilation compilation, DiagnosticBag diagnosticsBag, CancellationToken cancellationToken)

Will see if there is something in Mediator related to diagnostics that triggers this.

martinothamar commented 1 year ago

I might have found something. In my local repro the VS errors seemed to go away when I disabled diagnostics entirely. Based on the error message I got suspicious of the location parameter that is set on diagnostics. I added the diagnostics back but without location (i.e. Location.None), and I still didn't get VS errors. So the SyntaxTree is not part of the compilation (Parameter 'syntaxTree') error might be based on what location is added to the diagnostics.

Commit: https://github.com/martinothamar/Mediator/commit/f19bc02444245785fb2f968fc85434d759ffb65f Updated nuget pkg: https://www.nuget.org/packages/Mediator.SourceGenerator/2.2.0-preview.3

I just pushed this, but if could try the 2.2.0-preview.3 version when it is fully available then that would be great. If my assumption is correct, your project should also work find if Mediator produces no diagnostics. For example in @panoukos41 there were warnings about missing handlers image

The Clean Architecture sample in this repo has no warnings, and therefore never had the VS issues that surfaced with the repro, so I'm fairly certain we're on the right track here

michaeljfarr commented 1 year ago

Ya - Thanks. That fixes it for me!!

martinothamar commented 1 year ago

Great, I'm publishing the bugfix in version 2.1.2 as well: https://www.nuget.org/packages/Mediator.SourceGenerator/2.1.2 I'll revisit the "correct" location in a future refarctoring, as there are some big changes to the source generation process coming up

panoukos41 commented 1 year ago

Hey thanks for the investigation, I wanted to dig too but didnt have as much time and restarting VS doesn't help :P, I am happy the requests I left for later actually helped!!

It works as expected now, thanks again :D !!