Просмотр исходного кода

Enable Trimming on FileProviders.Embedded (#47322)

* Enable Trimming on FileProviders.Embedded

The FileProviders.Embedded assembly wasn't marked as trimmable, and wasn't getting analyzers run on it. Enable the analyzers and fix the warnings.

Clean up items:
- Capture the result of Assembly.Location in a variable because it allocates a string every time it is called.
- Remove the TrimmingAttributes.cs file from AspNetCore.Identity because the assembly only builds for DefaultNetCoreTargetFramework.

Also fix CodeGen.proj to use Environment.NewLine, so it doesn't make unnecessary diffs on Windows machines.
Eric Erhardt 3 лет назад
Родитель
Сommit
46d1c245cf

+ 5 - 5
eng/CodeGen.proj

@@ -36,7 +36,7 @@
 -->
 <Project>
   <ItemGroup>
-    @(_ProjectReferenceProvider->'<ProjectReferenceProvider Include="%(Identity)" ProjectPath="%24(RepoRoot)%(ProjectFileRelativePath)" />', '%0A    ')
+    @(_ProjectReferenceProvider->'<ProjectReferenceProvider Include="%(Identity)" ProjectPath="%24(RepoRoot)%(ProjectFileRelativePath)" />', '$([System.Environment]::NewLine)    ')
   </ItemGroup>
 </Project>
 ]]></ProjectListContent>
@@ -63,10 +63,10 @@
 <Project>
   <ItemGroup>
     <!-- These assemblies are available as both a NuGet package and in the shared framework -->
-    @(_SharedFrameworkAndPackageRef->'<AspNetCoreAppReferenceAndPackage Include="%(Identity)" />', '%0A    ')
+    @(_SharedFrameworkAndPackageRef->'<AspNetCoreAppReferenceAndPackage Include="%(Identity)" />', '$([System.Environment]::NewLine)    ')
 
     <!-- These assemblies are only in the shared framework -->
-    @(_SharedFrameworkRef->'<AspNetCoreAppReference Include="%(Identity)" />', '%0A    ')
+    @(_SharedFrameworkRef->'<AspNetCoreAppReference Include="%(Identity)" />', '$([System.Environment]::NewLine)    ')
   </ItemGroup>
 </Project>
       ]]>
@@ -89,7 +89,7 @@
 -->
 <Project>
   <ItemGroup>
-    @(_TrimmableProject->'<TrimmableProject Include="%(Identity)" />', '%0A    ')
+    @(_TrimmableProject->'<TrimmableProject Include="%(Identity)" />', '$([System.Environment]::NewLine)    ')
   </ItemGroup>
 </Project>
       ]]>
@@ -111,7 +111,7 @@
 -->
 <Project>
   <ItemGroup>
-    @(_RequiresDelayedBuild->'<RequiresDelayedBuild Include="%24(RepoRoot)%(ProjectFileRelativePath)" />', '%0A    ')
+    @(_RequiresDelayedBuild->'<RequiresDelayedBuild Include="%24(RepoRoot)%(ProjectFileRelativePath)" />', '$([System.Environment]::NewLine)    ')
   </ItemGroup>
 </Project>
 ]]></DelayedBuildContent>

+ 1 - 0
eng/TrimmableProjects.props

@@ -95,6 +95,7 @@
     <TrimmableProject Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" />
     <TrimmableProject Include="Microsoft.AspNetCore.Components.WebAssembly" />
     <TrimmableProject Include="Microsoft.AspNetCore.Components.Web" />
+    <TrimmableProject Include="Microsoft.Extensions.FileProviders.Embedded" />
     <TrimmableProject Include="Microsoft.Extensions.Localization.Abstractions" />
     <TrimmableProject Include="Microsoft.Extensions.ObjectPool" />
     <TrimmableProject Include="Microsoft.JSInterop" />

+ 6 - 2
src/FileProviders/Embedded/src/EmbeddedFileProvider.cs

@@ -3,6 +3,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 using System.IO;
 using System.Linq;
@@ -43,6 +44,8 @@ public class EmbeddedFileProvider : IFileProvider
     /// </summary>
     /// <param name="assembly">The assembly that contains the embedded resources.</param>
     /// <param name="baseNamespace">The base namespace that contains the embedded resources.</param>
+    [UnconditionalSuppressMessage("SingleFile", "IL3000:Assembly.Location",
+        Justification = "The code handles if the Assembly.Location is empty. Workaround https://github.com/dotnet/runtime/issues/83607")]
     public EmbeddedFileProvider(Assembly assembly, string? baseNamespace)
     {
         ArgumentNullThrowHelper.ThrowIfNull(assembly);
@@ -52,11 +55,12 @@ public class EmbeddedFileProvider : IFileProvider
 
         _lastModified = DateTimeOffset.UtcNow;
 
-        if (!string.IsNullOrEmpty(_assembly.Location))
+        var assemblyLocation = assembly.Location;
+        if (!string.IsNullOrEmpty(assemblyLocation))
         {
             try
             {
-                _lastModified = File.GetLastWriteTimeUtc(_assembly.Location);
+                _lastModified = File.GetLastWriteTimeUtc(assemblyLocation);
             }
             catch (PathTooLongException)
             {

+ 6 - 2
src/FileProviders/Embedded/src/ManifestEmbeddedFileProvider.cs

@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Reflection;
 using Microsoft.AspNetCore.Shared;
@@ -120,15 +121,18 @@ public class ManifestEmbeddedFileProvider : IFileProvider
         return NullChangeToken.Singleton;
     }
 
+    [UnconditionalSuppressMessage("SingleFile", "IL3000:Assembly.Location",
+        Justification = "The code handles if the Assembly.Location is empty. Workaround https://github.com/dotnet/runtime/issues/83607")]
     private static DateTimeOffset ResolveLastModified(Assembly assembly)
     {
         var result = DateTimeOffset.UtcNow;
 
-        if (!string.IsNullOrEmpty(assembly.Location))
+        var assemblyLocation = assembly.Location;
+        if (!string.IsNullOrEmpty(assemblyLocation))
         {
             try
             {
-                result = File.GetLastWriteTimeUtc(assembly.Location);
+                result = File.GetLastWriteTimeUtc(assemblyLocation);
             }
             catch (PathTooLongException)
             {

+ 3 - 0
src/FileProviders/Embedded/src/Microsoft.Extensions.FileProviders.Embedded.csproj

@@ -12,6 +12,7 @@
     <PackageTags>files;filesystem</PackageTags>
     <NoPackageAnalysis>true</NoPackageAnalysis>
     <Nullable>enable</Nullable>
+    <IsTrimmable>true</IsTrimmable>
   </PropertyGroup>
 
   <ItemGroup>
@@ -41,5 +42,7 @@
   <ItemGroup>
     <Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
     <Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
+    <Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
+             Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />
   </ItemGroup>
 </Project>

+ 2 - 1
src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs

@@ -73,7 +73,8 @@ public class StaticWebAssetsLoader
             return null;
         }
         var assembly = Assembly.Load(environment.ApplicationName);
-        var basePath = string.IsNullOrEmpty(assembly.Location) ? AppContext.BaseDirectory : Path.GetDirectoryName(assembly.Location);
+        var assemblyLocation = assembly.Location;
+        var basePath = string.IsNullOrEmpty(assemblyLocation) ? AppContext.BaseDirectory : Path.GetDirectoryName(assemblyLocation);
         return Path.Combine(basePath!, $"{environment.ApplicationName}.staticwebassets.runtime.json");
     }
 }

+ 0 - 3
src/Identity/Core/src/Microsoft.AspNetCore.Identity.csproj

@@ -13,9 +13,6 @@
   <ItemGroup>
     <Reference Include="Microsoft.AspNetCore.Authentication.Cookies" />
     <Reference Include="Microsoft.Extensions.Identity.Core" />
-
-    <Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
-      Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />
   </ItemGroup>
 
   <ItemGroup>

+ 3 - 0
src/Tools/Tools.slnf

@@ -7,6 +7,8 @@
       "src\\Components\\Components\\src\\Microsoft.AspNetCore.Components.csproj",
       "src\\Components\\CustomElements\\src\\Microsoft.AspNetCore.Components.CustomElements.csproj",
       "src\\Components\\Forms\\src\\Microsoft.AspNetCore.Components.Forms.csproj",
+      "src\\Components\\QuickGrid\\Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter\\src\\Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter.csproj",
+      "src\\Components\\QuickGrid\\Microsoft.AspNetCore.Components.QuickGrid\\src\\Microsoft.AspNetCore.Components.QuickGrid.csproj",
       "src\\Components\\WebAssembly\\Authentication.Msal\\src\\Microsoft.Authentication.WebAssembly.Msal.csproj",
       "src\\Components\\WebAssembly\\JSInterop\\src\\Microsoft.JSInterop.WebAssembly.csproj",
       "src\\Components\\WebAssembly\\WebAssembly.Authentication\\src\\Microsoft.AspNetCore.Components.WebAssembly.Authentication.csproj",
@@ -21,6 +23,7 @@
       "src\\DataProtection\\StackExchangeRedis\\src\\Microsoft.AspNetCore.DataProtection.StackExchangeRedis.csproj",
       "src\\DefaultBuilder\\src\\Microsoft.AspNetCore.csproj",
       "src\\Extensions\\Features\\src\\Microsoft.Extensions.Features.csproj",
+      "src\\FileProviders\\Embedded\\src\\Microsoft.Extensions.FileProviders.Embedded.csproj",
       "src\\HealthChecks\\Abstractions\\src\\Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.csproj",
       "src\\HealthChecks\\HealthChecks\\src\\Microsoft.Extensions.Diagnostics.HealthChecks.csproj",
       "src\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",