dotnet / roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
MIT License
512 stars 257 forks source link

Add support for `IIncrementalGenerator` #941

Closed Flash0ver closed 2 years ago

Flash0ver commented 2 years ago

For ISourceGenerator that were introduced in 3.8, I was able to adopt the Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.[Test-Framework] package quickly, since I'm also familiar with the Analyzer/CodeFix/CodeRefactoring variants.

Now I would love to see an IncrementalGenerators variant. An alternative would be to add support for IIncrementalGenerator to the already existing SourceGenerators package, but I guess this is not preferable since the update of the dependency Microsoft.CodeAnalysis.CSharp.Workspaces to 4.0.1 would be a breaking change for existing consumers.

I built a custom testing helpers (inspired by the Source Generators Cookbook), which are continuously growing (across my source generator projects), but are missing the really powerful features such as ReferenceAssemblies and {|#0:Location|}-Syntax.

I also noticed #933, but if I understand correctly, then IIncrementalGenerator are not suppored by Microsoft.CodeAnalysis.Testing, because the generic CSharpSourceGeneratorVerifier and CSharpSourceGeneratorTest types are constrained to ISourceGenerator, new(). I also tried to apply the ISourceGenerator-types from Microsoft.CodeAnalysis.Testing, and then use CSharpSourceGeneratorTest with a custom IncrementalGenerator-to-SourceGenerator-wrapper. But this approach failed, because the methods of the instance returned by Microsoft.CodeAnalysis.GeneratorExtensions.AsSourceGenerator() throw an InvalidOperationException with message This program location is thought to be unreachable.. I have just learnt that the CSharpGeneratorDriver unwraps that type again and does not use it directly. Here's a link this failed experiment: https://github.com/Flash0ver/F0.Compatibility/blob/test/microsoft-codeanalysis-testing/src/tests/F0.Compatibility.Generator.Tests/Testing/CSharpIncrementalGeneratorVerifier.Test.cs

sharwell commented 2 years ago

Here are the two verifier classes for an incremental generator: https://github.com/tunnelvisionlabs/language-types/blob/main/test/TunnelVisionLabs.LanguageTypes.SourceGenerator.UnitTests/Verifiers/CSharpSourceGeneratorVerifier%601.cs https://github.com/tunnelvisionlabs/language-types/blob/main/test/TunnelVisionLabs.LanguageTypes.SourceGenerator.UnitTests/Verifiers/CSharpSourceGeneratorVerifier%601%2BTest.cs

Flash0ver commented 2 years ago

Great! Thank you! I'll try it out over next weekend.

Flash0ver commented 2 years ago

And I got my IIncrementalGenerator tests running.

I didn't know about the combination of

Thank you very much @sharwell for the example!

AraHaan commented 2 years ago

I think it should now be safe to use 4.0.1 on Microsoft.CodeAnalysis.CSharp.Workspaces due to .NET 6 LTS and .NET 5 being close to EOL. Besides I do not think many are using the .NET 5 SDK anymore and are instead using the .NET 6 or newer SDK and targeting .NET 5 making it able to use generators using v4.0.1 of the roslyn packages without issues because they will be using a newer compiler that uses those versions anyway.