Visual Studio Enterprise 2022 Preview, .NET Source Generators Integration, Usability Issues

Seth Ficke 6 Reputation points
2022-05-20T16:06:16.413+00:00

Use Case:
As a software engineer, I need to tool-generate client-server interoperation code from a message contract, so my team doesn't have to maintain boilerplate. I implement a Contracts library containing interfaces decorated with attributes, which declaratively specify an application programming interface. Then I implement source generators for my frontend and backend. I expect that when I modify my Contracts library, then the source generators should automatically generate interoperation code following my design patterns.

Solution Architecture:
project: Frontend (MAUI project)
project: Backend (Console app)
project: Contracts (class library with interface definitions)
project: Backend.Generators (source generator library)
project: Frontend.Generators (source generator library)
project: CSharpFormatting (class library with StringBuilder extension methods)

project reference: Frontend > Frontend.Generators
project reference: Frontend > Contracts
project reference: Frontend.Generators > Contracts
project reference: Frontend.Generators > CSharpFormatting
project reference: Backend > Backend .Generators
project reference: Backend > Contracts
project reference: Backend.Generators > Contracts
project reference: Backend.Generators > CSharpFormatting

Problem statements:

  1. When a source generator runs for the first time, then Visual Studio caches its output. If the source generator or the contracts library changes, then the source generator emits a different representation. Visual Studio continues to display the original representation. It is required to completely exit and restart Visual Studio to work around this issue. In practice I am restarting VS after every change when working on the contracts and generators (10+ times per hour). This is really annoying. Related issues: https://github.com/dotnet/roslyn/issues/48083
  2. Project references in source generators are respected by Visual Studio + Intellisense, but don't work at runtime. I write extension methods in CSharpFormatting, and use then in Backend.Contracts. Intellisense provides syntax highlighting. But when I run the Backend build, the source generator throws FileNotFoundException for the CSharpFormatting DLL. It is necessary to use Reflection to resolve the DLL path and then System.Reflection.Assembly.Load(...) the DLL during the generator execution as a workaround. Refactoring generators to share common code becomes painful and error-prone.

Other feedback:
Even with these issues, Source Generators are much more usable than T4 templates! Thank you for implementing the feature!!! And please keep going to improve usability such that even a junior developer can implement AOT work!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
{count} vote