Skip to content

Detect trimming issues in CI #57338

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Microsoft.AspNetCore.Components.ResourceAssetProperty
Microsoft.AspNetCore.Components.ResourceAssetProperty.Name.get -> string!
Microsoft.AspNetCore.Components.ResourceAssetProperty.ResourceAssetProperty(string! name, string! value) -> void
Microsoft.AspNetCore.Components.ResourceAssetProperty.Value.get -> string!
Microsoft.AspNetCore.Components.UnusedTypeLoader
static Microsoft.AspNetCore.Components.UnusedTypeLoader.LoadUnusedType() -> System.Type?
static readonly Microsoft.AspNetCore.Components.ResourceAssetCollection.Empty -> Microsoft.AspNetCore.Components.ResourceAssetCollection!
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.Assets.get -> Microsoft.AspNetCore.Components.ResourceAssetCollection!
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.RendererInfo.get -> Microsoft.AspNetCore.Components.RendererInfo!
28 changes: 28 additions & 0 deletions src/Components/Components/src/UnusedTypeForTrimming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Components;

/// <summary>
/// For testing.
/// </summary>
public static class UnusedTypeLoader
{
/// <summary>
/// For testing.
/// </summary>
public static Type? LoadUnusedType()
{
// We're intentionally loading an unreferenced type here.
// This should return null on a published build.

#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
return typeof(UnusedTypeLoader).Assembly.GetType($"Microsoft.AspNetCore.Components.{nameof(UnusedTypeForTrimming)}")!;
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
}
}

internal sealed class UnusedTypeForTrimming
{
public string UnusedProperty { get; } = "Foo";
}
7 changes: 7 additions & 0 deletions src/Components/test/testassets/BasicTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public static async Task Main(string[] args)
{
await SimulateErrorsIfNeededForTest();

if (UnusedTypeLoader.LoadUnusedType() is null)
{
// Fail quickly if loading an unreferenced type fails.
// This should make it obvious if trimming issues get caught by E2E tests.
throw new InvalidOperationException("Could not load the type dynamically!");
}

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.RootComponents.Add<Index>("root");
Expand Down
Loading