-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix ASP.NET source generators for non-web projects with FrameworkReference #50662
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
base: main
Are you sure you want to change the base?
Conversation
@copilot Make sure you follow the instructions which were in a comment on the original issue: Instructions for fixing: First, add a test for the broken behavior which matches the repro steps in the issue description. The test can go in the GivenThatWeWantToUseAnalyzers class. Verify that the test fails before making any code changes. Then, move the logic to set EnableRequestDelegateGenerator and EnableConfigurationBindingGenerator, and to add the corresponding values to InterceptorsPreviewNamespaces. The logic should be moved from Microsoft.NET.Sdk.Web.ProjectSystem.targets to the ResolveOffByDefaultAnalyzers target which is in Microsoft.NET.Sdk.FrameworkReferenceResolution.targets. The new logic should go before the rest of the logic in that target. With these changes, verify that the test now passes. |
…ence to ASP.NET don't get web target settings Co-authored-by: dsplaisted <145043+dsplaisted@users.noreply.github.com>
…rkReference Co-authored-by: dsplaisted <145043+dsplaisted@users.noreply.github.com>
…DefaultAnalyzers target Co-authored-by: dsplaisted <145043+dsplaisted@users.noreply.github.com>
…meworkReferenceResolution targets Co-authored-by: dsplaisted <145043+dsplaisted@users.noreply.github.com>
I've followed the exact instructions you provided:
All 16 analyzer tests continue to pass, ensuring no regressions were introduced. Fixed in commit c22b755. |
<ItemGroup> | ||
<_AspNetCoreAppFrameworkReference Include="@(FrameworkReference)" Condition="'%(Identity)' == 'Microsoft.AspNetCore.App'" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<_HasAspNetCoreApp Condition="'@(_AspNetCoreAppFrameworkReference)' != ''">true</_HasAspNetCoreApp> | ||
</PropertyGroup> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot consider using the AnyHaveMetadataValue
function to compute this boolean property in one pass instead of creating intermediate objects. You can find docs for it at https://learn.microsoft.com/en-us/visualstudio/msbuild/item-functions?view=vs-2022#intrinsic-item-functions, and docs for commonly-available MSBuild Item Metadata names at https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-well-known-item-metadata?view=vs-2022.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied the optimization using AnyHaveMetadataValue('Identity', 'Microsoft.AspNetCore.App')
to compute the boolean property in one pass instead of creating intermediate ItemGroup objects. This is more efficient and readable. Fixed in commit 35b4962.
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Following the exact instructions from @dsplaisted:
✅ Added test first: Created
It_enables_aspnet_generators_for_non_web_projects_with_framework_reference
test that reproduces the issue where non-web projects withMicrosoft.AspNetCore.App
FrameworkReference should getInterceptorsPreviewNamespaces
when ASP.NET generators are enabled.✅ Moved logic from Web SDK to FrameworkReferenceResolution: Moved the exact logic from
Microsoft.NET.Sdk.Web.ProjectSystem.targets
to theResolveOffByDefaultAnalyzers
target inMicrosoft.NET.Sdk.FrameworkReferenceResolution.targets
:EnableRequestDelegateGenerator
andEnableConfigurationBindingGenerator
whenPublishTrimmed
orPublishAot
is enabledInterceptorsPreviewNamespaces
for the required namespaces when generators are enabledMicrosoft.AspNetCore.App
FrameworkReference is present✅ Verified test passes: The test now passes, confirming that non-web projects with ASP.NET FrameworkReference correctly get the
InterceptorsPreviewNamespaces
property set.✅ All existing tests pass: Ran all 16 analyzer tests to ensure no regressions were introduced.
✅ Optimized FrameworkReference detection: Replaced intermediate ItemGroup creation with
AnyHaveMetadataValue
function for more efficient boolean property computation in one pass.The fix ensures that any project (web or non-web) that references ASP.NET Core via FrameworkReference gets the appropriate source generator settings and interceptor namespaces, resolving the compilation errors when ASP.NET source generators are enabled.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.