Search code examples
c#blazor.net-6.0blazor-webassembly

Unable to disable Blazor WASM trimming/linking/tree-shaking


Targeting the .NET 6 framework but building with the latest .NET 8 SDK.

I'm using a gRPC serialization library that throws an error when serializing a KeyValuePair.

I realized it's working fine on my dev computer (even a Release build), but fails when deploying using dotnet publish. When the program works, I get this warning in the browser console when loading the page:

This application was built with linking (tree shaking) disabled. Published applications will be significantly smaller.

I found this page that even mentions KeyValuePair. Following the instruction, I've added this portion to my Blazor WASM .csproj project file:

<PublishTrimmed>false</PublishTrimmed>

This does not seem to have any impact except I do see this warning while building:

Publishing without optimizations. Although it's optional for Blazor, we strongly recommend using wasm-tools workload! You can install it by running dotnet workload install wasm-tools from the command line.

Unfortunately the previous warning message is missing when opening the page and project still crash on serialization.

I've also tried adding this:

<BlazorWebAssemblyEnableLinking>false</BlazorWebAssemblyEnableLinking>

But this seems to be for an older version. Also had no impact

I tried to force the argument via the publish command:

dotnet publish Server.sln -c Release -p:PublishTrimmed=false

Still no difference.

But if I add

<EnableTrimAnalyzer>true</EnableTrimAnalyzer>

I do see some additional warnings in the build (objects that are unrelated to the one I'm having a problem with), so that proves the project file does take the properties in account.

I tried to add both properties to the ASP.NET server .csproj file too (not the WASM app). I tried to delete all bin/obj folders. I'm making sure to always refresh my page using a full Ctrl+F5 to clear the cache.

I've found many discussions online about setting PublishTrimmed to false fixing their issues. Why doesn't it work for me? It seems Blazor wants to run the optimization process no matter what.


Solution

  • Updating my Blazor nuget Microsoft.AspNetCore.Components.WebAssembly.*from "6.0.11" to "6.0.26" seems to have fixed my issues preventing me to disable trimming.