-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
When we first added source generators/analyzers to our shared frameworks in .NET 6.0 they were meant to be always on and used like "add-on" features. New API that was explicitly addressed through some user gesture - like applying an attribute.
Now in .NET 8.0 more generators are coming online that intend to apply some behavior to existing code with no source changes. The user's source itself does not opt-in to using the generator, but the generator will change the user's code in some way.
One example of this is: dotnet/runtime#44493
This is meant to automatically replace usage of API that is not linker safe with source generated code that is linker safe.
There may be more in the future if we come up with additional generators that are meant to change the user's code in some meaningful way.
In light of these, we should think about how we want to represent these generators and activate them / disable them.
Today the conventions for generators / analyzers in both nuget packages and the ref-packs are "always on". There is not a great convention for disabling generators passed to the compiler. See dotnet/roslyn#55518
One case where this is happening today is
sdk/src/WebSdk/Web/Sdk/Sdk.props
Lines 64 to 75 in a76a0cc
<!-- The RequestDelegateGenerator is bundled into the Microsoft.AspNetCore.App ref pack.--> | |
<!-- We want this generator to be disabled by default so we remove it from the list of--> | |
<!-- analyzers here. Since this depends on the analyzer having already been added to the--> | |
<!-- `Analyzer` item group by previous tasks in the build, we must wrap this work in a--> | |
<!-- target that executes right before the compilation loop.--> | |
<Target Name="RemoveRequestDelegateGenerator" BeforeTargets="CoreCompile"> | |
<ItemGroup> | |
<Analyzer | |
Remove="@(Analyzer->HasMetadata('FileName')->WithMetadataValue('FileName', 'Microsoft.AspNetCore.Http.Generators'))" | |
Condition="'$(Language)'=='C#' AND '$(EnableRequestDelegateGenerator)' != 'true'"/> | |
</ItemGroup> | |
</Target> |
Where ASP.NET doesn't want the generator enabled by default and runs a target to remove it. This works, but requires targets be added to the SDK for each generator, along with separate handling in the nuget package (in cases of OOB generators).
Describe the solution you'd like
Some consistent way to represent and interact with optional generators. Ideally it shouldn't involve running targets.
Potential solutions:
- A way to represent disabled generators in the ref packs and packages and SDK support for "enabling" them.
- A way to represent disabled by default generators in the actual generator API, then compiler support for "enabling" them.
- Some other type of gesture in the source for enabling/disabling. EG: assembly level attribute / additional data to compiler / etc.
etc
cc @eerhardt @chsienki @dsplaisted @davidfowl @captainsafia @layomia @terrajobst @stephentoub