Skip to content

Commit 46d1c24

Browse files
authored
Enable Trimming on FileProviders.Embedded (dotnet#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.
1 parent 414e3eb commit 46d1c24

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

eng/CodeGen.proj

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
-->
3737
<Project>
3838
<ItemGroup>
39-
@(_ProjectReferenceProvider->'<ProjectReferenceProvider Include="%(Identity)" ProjectPath="%24(RepoRoot)%(ProjectFileRelativePath)" />', '%0A ')
39+
@(_ProjectReferenceProvider->'<ProjectReferenceProvider Include="%(Identity)" ProjectPath="%24(RepoRoot)%(ProjectFileRelativePath)" />', '$([System.Environment]::NewLine) ')
4040
</ItemGroup>
4141
</Project>
4242
]]></ProjectListContent>
@@ -63,10 +63,10 @@
6363
<Project>
6464
<ItemGroup>
6565
<!-- These assemblies are available as both a NuGet package and in the shared framework -->
66-
@(_SharedFrameworkAndPackageRef->'<AspNetCoreAppReferenceAndPackage Include="%(Identity)" />', '%0A ')
66+
@(_SharedFrameworkAndPackageRef->'<AspNetCoreAppReferenceAndPackage Include="%(Identity)" />', '$([System.Environment]::NewLine) ')
6767
6868
<!-- These assemblies are only in the shared framework -->
69-
@(_SharedFrameworkRef->'<AspNetCoreAppReference Include="%(Identity)" />', '%0A ')
69+
@(_SharedFrameworkRef->'<AspNetCoreAppReference Include="%(Identity)" />', '$([System.Environment]::NewLine) ')
7070
</ItemGroup>
7171
</Project>
7272
]]>
@@ -89,7 +89,7 @@
8989
-->
9090
<Project>
9191
<ItemGroup>
92-
@(_TrimmableProject->'<TrimmableProject Include="%(Identity)" />', '%0A ')
92+
@(_TrimmableProject->'<TrimmableProject Include="%(Identity)" />', '$([System.Environment]::NewLine) ')
9393
</ItemGroup>
9494
</Project>
9595
]]>
@@ -111,7 +111,7 @@
111111
-->
112112
<Project>
113113
<ItemGroup>
114-
@(_RequiresDelayedBuild->'<RequiresDelayedBuild Include="%24(RepoRoot)%(ProjectFileRelativePath)" />', '%0A ')
114+
@(_RequiresDelayedBuild->'<RequiresDelayedBuild Include="%24(RepoRoot)%(ProjectFileRelativePath)" />', '$([System.Environment]::NewLine) ')
115115
</ItemGroup>
116116
</Project>
117117
]]></DelayedBuildContent>

eng/TrimmableProjects.props

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<TrimmableProject Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" />
9696
<TrimmableProject Include="Microsoft.AspNetCore.Components.WebAssembly" />
9797
<TrimmableProject Include="Microsoft.AspNetCore.Components.Web" />
98+
<TrimmableProject Include="Microsoft.Extensions.FileProviders.Embedded" />
9899
<TrimmableProject Include="Microsoft.Extensions.Localization.Abstractions" />
99100
<TrimmableProject Include="Microsoft.Extensions.ObjectPool" />
100101
<TrimmableProject Include="Microsoft.JSInterop" />

src/FileProviders/Embedded/src/EmbeddedFileProvider.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.IO;
89
using System.Linq;
@@ -43,6 +44,8 @@ public EmbeddedFileProvider(Assembly assembly)
4344
/// </summary>
4445
/// <param name="assembly">The assembly that contains the embedded resources.</param>
4546
/// <param name="baseNamespace">The base namespace that contains the embedded resources.</param>
47+
[UnconditionalSuppressMessage("SingleFile", "IL3000:Assembly.Location",
48+
Justification = "The code handles if the Assembly.Location is empty. Workaround https://github.com/dotnet/runtime/issues/83607")]
4649
public EmbeddedFileProvider(Assembly assembly, string? baseNamespace)
4750
{
4851
ArgumentNullThrowHelper.ThrowIfNull(assembly);
@@ -52,11 +55,12 @@ public EmbeddedFileProvider(Assembly assembly, string? baseNamespace)
5255

5356
_lastModified = DateTimeOffset.UtcNow;
5457

55-
if (!string.IsNullOrEmpty(_assembly.Location))
58+
var assemblyLocation = assembly.Location;
59+
if (!string.IsNullOrEmpty(assemblyLocation))
5660
{
5761
try
5862
{
59-
_lastModified = File.GetLastWriteTimeUtc(_assembly.Location);
63+
_lastModified = File.GetLastWriteTimeUtc(assemblyLocation);
6064
}
6165
catch (PathTooLongException)
6266
{

src/FileProviders/Embedded/src/ManifestEmbeddedFileProvider.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO;
67
using System.Reflection;
78
using Microsoft.AspNetCore.Shared;
@@ -120,15 +121,18 @@ public IChangeToken Watch(string filter)
120121
return NullChangeToken.Singleton;
121122
}
122123

124+
[UnconditionalSuppressMessage("SingleFile", "IL3000:Assembly.Location",
125+
Justification = "The code handles if the Assembly.Location is empty. Workaround https://github.com/dotnet/runtime/issues/83607")]
123126
private static DateTimeOffset ResolveLastModified(Assembly assembly)
124127
{
125128
var result = DateTimeOffset.UtcNow;
126129

127-
if (!string.IsNullOrEmpty(assembly.Location))
130+
var assemblyLocation = assembly.Location;
131+
if (!string.IsNullOrEmpty(assemblyLocation))
128132
{
129133
try
130134
{
131-
result = File.GetLastWriteTimeUtc(assembly.Location);
135+
result = File.GetLastWriteTimeUtc(assemblyLocation);
132136
}
133137
catch (PathTooLongException)
134138
{

src/FileProviders/Embedded/src/Microsoft.Extensions.FileProviders.Embedded.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageTags>files;filesystem</PackageTags>
1313
<NoPackageAnalysis>true</NoPackageAnalysis>
1414
<Nullable>enable</Nullable>
15+
<IsTrimmable>true</IsTrimmable>
1516
</PropertyGroup>
1617

1718
<ItemGroup>
@@ -41,5 +42,7 @@
4142
<ItemGroup>
4243
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
4344
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
45+
<Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
46+
Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />
4447
</ItemGroup>
4548
</Project>

src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ internal static void UseStaticWebAssetsCore(IWebHostEnvironment environment, Str
7373
return null;
7474
}
7575
var assembly = Assembly.Load(environment.ApplicationName);
76-
var basePath = string.IsNullOrEmpty(assembly.Location) ? AppContext.BaseDirectory : Path.GetDirectoryName(assembly.Location);
76+
var assemblyLocation = assembly.Location;
77+
var basePath = string.IsNullOrEmpty(assemblyLocation) ? AppContext.BaseDirectory : Path.GetDirectoryName(assemblyLocation);
7778
return Path.Combine(basePath!, $"{environment.ApplicationName}.staticwebassets.runtime.json");
7879
}
7980
}

src/Identity/Core/src/Microsoft.AspNetCore.Identity.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
<ItemGroup>
1414
<Reference Include="Microsoft.AspNetCore.Authentication.Cookies" />
1515
<Reference Include="Microsoft.Extensions.Identity.Core" />
16-
17-
<Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
18-
Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />
1916
</ItemGroup>
2017

2118
<ItemGroup>

src/Tools/Tools.slnf

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"src\\Components\\Components\\src\\Microsoft.AspNetCore.Components.csproj",
88
"src\\Components\\CustomElements\\src\\Microsoft.AspNetCore.Components.CustomElements.csproj",
99
"src\\Components\\Forms\\src\\Microsoft.AspNetCore.Components.Forms.csproj",
10+
"src\\Components\\QuickGrid\\Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter\\src\\Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter.csproj",
11+
"src\\Components\\QuickGrid\\Microsoft.AspNetCore.Components.QuickGrid\\src\\Microsoft.AspNetCore.Components.QuickGrid.csproj",
1012
"src\\Components\\WebAssembly\\Authentication.Msal\\src\\Microsoft.Authentication.WebAssembly.Msal.csproj",
1113
"src\\Components\\WebAssembly\\JSInterop\\src\\Microsoft.JSInterop.WebAssembly.csproj",
1214
"src\\Components\\WebAssembly\\WebAssembly.Authentication\\src\\Microsoft.AspNetCore.Components.WebAssembly.Authentication.csproj",
@@ -21,6 +23,7 @@
2123
"src\\DataProtection\\StackExchangeRedis\\src\\Microsoft.AspNetCore.DataProtection.StackExchangeRedis.csproj",
2224
"src\\DefaultBuilder\\src\\Microsoft.AspNetCore.csproj",
2325
"src\\Extensions\\Features\\src\\Microsoft.Extensions.Features.csproj",
26+
"src\\FileProviders\\Embedded\\src\\Microsoft.Extensions.FileProviders.Embedded.csproj",
2427
"src\\HealthChecks\\Abstractions\\src\\Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.csproj",
2528
"src\\HealthChecks\\HealthChecks\\src\\Microsoft.Extensions.Diagnostics.HealthChecks.csproj",
2629
"src\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",

0 commit comments

Comments
 (0)