Browse Source

Add FrameworkReference to Microsoft.Aspnetcore.App for non-SharedFx projects with SharedFx-only references (#65478)

* Add FrameworkReference for non-SharedFx projects with SharedFx refs

* Better approach

* Apply suggestions from code review

Co-authored-by: Copilot <[email protected]>

* Remove Transitive ref

* fixup

* Just to be sure

* Feedback

* Fixup

* Revert "Fixup"

This reverts commit c642e6b56d98d231ad609b63ab9010ab86967dc1.

* Revert "Feedback"

This reverts commit 507615711aa4bbf736dc5bdf81921ba329a6b736.

* Fixup

* Revert "Fixup"

This reverts commit d180f8a97b1aca1cd96fe7a90ba9c51cb50dde94.

* Test

* Get error

* Fixup

---------

Co-authored-by: Copilot <[email protected]>
William Godbe 6 days ago
parent
commit
eb9e4b5300
2 changed files with 33 additions and 0 deletions
  1. 1 0
      Directory.Build.targets
  2. 32 0
      eng/targets/ResolveReferences.targets

+ 1 - 0
Directory.Build.targets

@@ -131,6 +131,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <_RemovedAspNetKnownFrameworkReference Include="@(KnownFrameworkReference->WithMetadataValue('Identity', 'Microsoft.AspNetCore.App'))" />
     <KnownFrameworkReference Condition="'$(UseAspNetCoreSharedRuntime)' != 'true'" Remove="Microsoft.AspNetCore.App" />
     <KnownFrameworkReference Remove="Microsoft.WindowsDesktop.App" />
   </ItemGroup>

+ 32 - 0
eng/targets/ResolveReferences.targets

@@ -149,6 +149,38 @@
         Text="Redundant &lt;FrameworkReference&gt;. If you have an explicit item in the project file, you might be able to remove it. Some SDKs, like Microsoft.NET.Sdk.Web, add this implicitly." />
   </Target>
 
+  <!--
+    NuGet pack reads framework references from restore assets. For affected SDK-pack projects, add a temporary
+    framework reference during project evaluation so that it flows into the restore assets used by pack / GenerateNuspec.
+  -->
+
+  <ItemGroup Condition="'$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)' AND
+                 '$(IsPackable)' == 'true' AND
+                 '$(IsAspNetCoreApp)' != 'true' AND
+                 '@(_FrameworkProjectReference)' != '' AND
+                 '@(FrameworkReference->WithMetadataValue('Identity', 'Microsoft.AspNetCore.App')->Count())' == '0'">
+    <KnownFrameworkReference Include="@(_RemovedAspNetKnownFrameworkReference)" />
+    <!-- Mark as IsTransitiveFrameworkReference to exclude the pruning data: https://github.com/dotnet/sdk/issues/53106 -->
+    <FrameworkReference Include="Microsoft.AspNetCore.App" IsTransitiveFrameworkReference="true" DoNotResolve="true" />
+  </ItemGroup>
+
+  <!-- Remove the referenced AspNetCore framework before it's resolved in ResolveFrameworkReferences.  This lets us use our own references. -->
+  <Target Name="_RemoveAspNetCoreFrameworkReference" 
+          BeforeTargets="ResolveFrameworkReferences" >
+    <ItemGroup>
+      <FrameworkReference Remove="@(FrameworkReference->WithMetadataValue('DoNotResolve', 'true'))" />
+    </ItemGroup>
+  </Target>
+
+  <!-- Remove Transtive Microsoft.Aspnetcore.App FrameworkReference, for projects referencing the affected projects above -->
+  <Target Name="_RemoveAspNetCoreTransitiveFrameworkReference"
+          BeforeTargets="AddTransitiveFrameworkReferences"
+          Condition="'$(UseAspNetCoreSharedRuntime)' != 'true'" >
+    <ItemGroup>
+      <TransitiveFrameworkReference Remove="Microsoft.AspNetCore.App" />
+    </ItemGroup>
+  </Target>
+
   <!--
     This target resolves remaining Reference items to Packages, if possible. If not, they are left as Reference
     items for the SDK to resolve. This executes on NuGet restore and during DesignTimeBuild. It should not run in