Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IIncrementalGenerator #941

Closed
Flash0ver opened this issue Dec 18, 2021 · 4 comments · May be fixed by #952
Closed

Add support for IIncrementalGenerator #941

Flash0ver opened this issue Dec 18, 2021 · 4 comments · May be fixed by #952
Labels
Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing

Comments

@Flash0ver
Copy link

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 sharwell added Area-Source Generators SDK support for source generators Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing and removed Area-Source Generators SDK support for source generators labels Feb 2, 2022
@Flash0ver
Copy link
Author

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

@Flash0ver
Copy link
Author

And I got my IIncrementalGenerator tests running.

I didn't know about the combination of

  • EmptySourceGeneratorProvider for our CSharpSourceGeneratorTest derivation
  • overriding GetSourceGenerators() so that we can AsSourceGenerator() the IIncrementalGenerator under test

Thank you very much @sharwell for the example!

@AraHaan
Copy link
Member

AraHaan commented Apr 11, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants