Skip to content

Commit

Permalink
Enable Trimming on FileProviders.Embedded (dotnet#47322)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
eerhardt authored Mar 20, 2023
1 parent 414e3eb commit 46d1c24
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 13 deletions.
10 changes: 5 additions & 5 deletions eng/CodeGen.proj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
]]>
Expand All @@ -89,7 +89,7 @@
-->
<Project>
<ItemGroup>
@(_TrimmableProject->'<TrimmableProject Include="%(Identity)" />', '%0A ')
@(_TrimmableProject->'<TrimmableProject Include="%(Identity)" />', '$([System.Environment]::NewLine) ')
</ItemGroup>
</Project>
]]>
Expand All @@ -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>
Expand Down
1 change: 1 addition & 0 deletions eng/TrimmableProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
8 changes: 6 additions & 2 deletions src/FileProviders/Embedded/src/EmbeddedFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -43,6 +44,8 @@ public EmbeddedFileProvider(Assembly assembly)
/// </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);
Expand All @@ -52,11 +55,12 @@ public EmbeddedFileProvider(Assembly assembly, string? baseNamespace)

_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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,15 +121,18 @@ public IChangeToken Watch(string filter)
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageTags>files;filesystem</PackageTags>
<NoPackageAnalysis>true</NoPackageAnalysis>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ internal static void UseStaticWebAssetsCore(IWebHostEnvironment environment, Str
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");
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/Identity/Core/src/Microsoft.AspNetCore.Identity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions src/Tools/Tools.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 46d1c24

Please sign in to comment.