dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.68k stars 1.06k forks source link

Unable to build project which depends on package containing pre-compressed static web assets #40413

Open Jack-Edwards opened 5 months ago

Jack-Edwards commented 5 months ago

Describe the bug

I'm no longer able to build my Blazor WASM project after upgrading the target framework from .NET 8.0 to .NET 9.0 (Preview 3).

Error message included in the "Exceptions" section, below.

I believe this is due to the fact that one of the NuGet packages my project depends on contains pre-compressed static web assets. I.e., the NuGet package already contains .js.gz and .js.br compressed copies of any .js files. This is a great thing for package maintainers to do to help keep Blazor WASM applications as small as possible.

This seems to conflict with a recent modification to the SDK: https://github.com/dotnet/sdk/pull/39213

It seems the dependent project is now responsible for compressing static web assets and pack is supposed to exclude them.

Are package maintainers supposed to stop distributing pre-compressed static web assets? How should a package maintainer proceed to support multiple framework versions?

I admit my understanding of these events may be wrong. Apologies if that's the case.

Exceptions (if any)

Microsoft.NET.Sdk.StaticWebAssets.targets(488,5): Error : Conflicting assets with the same target path '_content/BlazorSodium/blazorSodium.bundle.js.gz'.

maraf commented 5 months ago

cc @javiercn

rafaelbotelhoetibr commented 5 months ago

At least I found an workaround. Here goes:

  1. Install version 9.0.100-preview.2.24157.14-win-x64.
  2. Replace all the files in the source folder with the target folder below.
  3. Restart Visual Studio and the build should work.

Source folder: C:\Program Files\dotnet\sdk\9.0.100-preview.2.24157.14\Sdks\Microsoft.NET.Sdk.StaticWebAssets\targets Target folder: C:\Program Files\dotnet\sdk\9.0.100-preview.3.24204.13\Sdks\Microsoft.NET.Sdk.StaticWebAssets\targets

Jack-Edwards commented 5 months ago

I'm able to build the project I was referring to by using the mcr.microsoft.com/dotnet/sdk:9.0.100-preview.2 Docker image. The 9.0-preview image is where my issue occurs.

Similarly, GitHub workflows using the step below also fail:

uses: actions/setup-dotnet@v3
with:
  dotnet-version: 9.0.x
Jack-Edwards commented 5 months ago

Another poor work-around is adding an entry to your .csproj:

<PropertyGroup>
     <TargetFramework>net9.0</TargetFramework>
      ...
     <EnableDefaultCompressedItems>false</EnableDefaultCompressedItems>
</PropertyGroup>

<CompressionExcludePatterns> is another property we could set, but it appears to only respect file extensions patterns, such as **/*.ext. **/mySpecificFile.ext doesn't work on my end.

Jack-Edwards commented 5 months ago

I'm wrong. You can do this to exclude a specific file by name:

<CompressionExcludePatterns>blazorSodium.bundle.js</CompressionExcludePatterns>

However I can't figure out the syntax to exclude multiple, discrete files. Or maybe this only works for files coming from a NuGet package. I have vite setup in my project to compress my own assets down to .gz and .br; doesn't look like I can exclude these assets in the same manner.

epoitier commented 5 months ago

Another workaround to avoid static assets compression during build time, add the following in the csproj failing inside PropertyGroup: <DisableBuildCompression>true</DisableBuildCompression>