Description
The linker in Blazor WASM is including dependencies that aren't in use (in normal scenarios these would be stripped out by the compiler, which is what happens with the linker disabled (ok)).
When adding a reference (RCL project/nuget) to a project with Microsoft.AspNetCore.Components.WebAssembly.Build
and BlazorWebAssemblyEnableLinking
set to true
, the linker will add the dependency project as a boot dependency inside blazor.boot.json
even if it's not used at all.
If this reference is not used, the linker should detect it and not add it as a boot project. I suspect the problem is somewhere where we take the linker output and enumerate the blazor binaries based on it.
This issue doesn't happen with BlazorWebAssemblyEnableLinking
disabled (the unused project is not even inside _framework/_bin
).
To Reproduce
Please follow the steps to get this reproduced on your own repository.
In order to check the bug, this minimal example repository with the bug can be used.
Properly Working Example: With no Linker
- Create a Blazor WASM project (host)
- Add
<BlazorWebAssemblyEnableLinking>false</BlazorWebAssemblyEnableLinking>
- Create a RCL project (lib) with a Component and
wwwroot/test.css
as StaticWebAsset - Reference
lib
fromhost
and use the Component from it - Build and Run
- The DLL of
lib
appears insideblazor.boot.json
and gets downloaded during startup (ok) - Navigate to
_content/lib/test.css
and see that the StaticWebAsset is available (ok) - Remove the runtime usage of
lib
by not using the Component anymore so the compiler strips it out - The DLL of the project is no longer inside
blazor.boot.json
and doesn't get downloaded during startup (ok) - The StaticWebAsset
_content/lib/test.css
is still available (ok)
Buggy Example: With Linker enabled
- Use the project from the working example
- Change it to use
<BlazorWebAssemblyEnableLinking>true</BlazorWebAssemblyEnableLinking>
- Reference
lib
fromhost
and use the Component from it - Build and Run
- The DLL of
lib
appears insideblazor.boot.json
and gets downloaded during startup (ok) - Navigate to
_content/lib/test.css
and see that the StaticWebAsset is available (ok) - Remove the runtime usage of
lib
by not using the Component anymore so the compiler strips it out - The DLL of the project is still inside
blazor.boot.json
and still gets downloaded during startup (fail) - The StaticWebAsset
_content/lib/test.css
is still available (ok)
Further technical details
- ASP.NET Core version: 3.2.0-rc1.20223.4
- dotnet version: 3.1.201
Comments
This is a rather big bug. When including a NuGet package and using a small portion of it, the linker will still add every sub-reference, even if not used and the DLL could be skipped.
This should still preserve StaticWebAssets from the projects that don't need a runtime.