question

SethFicke-4721 avatar image
0 Votes"
SethFicke-4721 asked JackJJun-MSFT commented

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

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!

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SethFicke-4721, Welcome to Microsoft Q&A, based on your description, your problem sounds like a feedback about visual studio. I recommend that you could ask the question in Developer Community, which may provide better professional support.

Thanks for your understanding.



0 Votes 0 ·

0 Answers