Build Error when using TimeWarp.State in multiple Razor Class-Libraries
I have a Blazor-WASM Applikation that has TargetFramework=net8.0 with Razor Class-Libraries that are referenced from the client-project by project references. I use the Library TimeWarp.State. My problem is that I get a build error on the client-project if more than one Razor Class-Library references TimeWarp.State. The error is as follows:
Severity Code Description Project File Line Suppression State Details Error (active) MSB4018 The "DiscoverPrecompressedAssets" task failed unexpectedly. System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at Microsoft.AspNetCore.StaticWebAssets.Tasks.DiscoverPrecompressedAssets.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() BlazorWASMWithTimeWarp.State.Client C:\Program Files\dotnet\sdk\9.0.300\Sdks\Microsoft.NET.Sdk.StaticWebAssets\targets\Microsoft.NET.Sdk.StaticWebAssets.Compression.targets 289
The problem seems to be that each Razor Class Library Project that references TimeWarp.State contains a folder "types" in wwwroot that contains linked files fe. one is called "Constants.d.ts". I assume that the "DiscoverPrecompressedAssets" task tries to add a file from one Razor Class Library that was already added from the other Razor Class Library.
How can I solve this issue? Should I move all states into a single projects that is referenced from the other projects?
Here is an example demonstrating the problem: TimeWarp.State-Issue.zip
@claude Please review
Claude finished @StevenTCramer's task —— View job
Analyzing TimeWarp.State Multiple Razor Class Library Build Issue
I'll analyze this build error issue where multiple Razor Class Libraries referencing TimeWarp.State cause MSB4018 "DiscoverPrecompressedAssets" task failures.
Todo List:
- [x] Read repository setup instructions (CLAUDE.md)
- [x] Analyze TimeWarp.State project structure and build configuration
- [x] Examine static web assets generation (wwwroot/types folder)
- [x] Research MSBuild static web assets handling and duplicate detection
- [x] Identify root cause of duplicate assets issue
- [x] Provide technical recommendations for resolution
Root Cause Analysis
The problem is in the TypeScript build configuration at Source/TimeWarp.State/tsconfig.json:6:
"declarationDir": "wwwroot/types"
The Issue:
- TimeWarp.State compiles TypeScript files and outputs
.d.tsdeclaration files towwwroot/types/ - When multiple Razor Class Libraries reference TimeWarp.State, each library gets a copy of these static web assets
- During build, ASP.NET Core's Static Web Assets system discovers assets from all libraries
- Multiple libraries contribute identical assets with the same paths (like
Constants.d.ts) - The
DiscoverPrecompressedAssetsMSBuild task fails because it tries to add duplicate keys to its internal dictionary
Technical Solutions
Solution 1: Asset Scoping (Recommended)
Add to TimeWarp.State.csproj:
<PropertyGroup>
<StaticWebAssetBasePath>_content/$(PackageId)</StaticWebAssetBasePath>
</PropertyGroup>
Solution 2: Explicit Asset Control
<PropertyGroup>
<EnableDefaultBlazorWebAssemblyItems>false</EnableDefaultBlazorWebAssemblyItems>
<ResolveStaticWebAssetsInputsDependsOn>
$(ResolveStaticWebAssetsInputsDependsOn);
RemoveDuplicateStaticWebAssets
</ResolveStaticWebAssetsInputsDependsOn>
</PropertyGroup>
<Target Name="RemoveDuplicateStaticWebAssets" BeforeTargets="ResolveStaticWebAssetsInputs">
<ItemGroup>
<StaticWebAsset Remove="@(StaticWebAsset)"
Condition="'%(StaticWebAsset.SourceType)' == 'Discovered' and '%(StaticWebAsset.SourceId)' != '$(MSBuildProjectName)'" />
</ItemGroup>
</Target>
Solution 3: For User Applications
Add to consuming project's .csproj:
<PropertyGroup>
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
</PropertyGroup>
Architectural Recommendations
For the TimeWarp.State library (Long-term):
- Scope TypeScript assets under
_content/TimeWarp.State/path - Mark build tools as private in MSBuild references
- Consider moving TypeScript definitions to a separate development package
- Implement asset cleanup targets to remove unnecessary files from packages
For Users (Immediate workaround):
- Consolidate states into a single shared project as suggested in the issue
- Use explicit static web asset configuration in the main application project
- Test with minimal reproduction to verify fixes
Priority Actions
- Immediate fix: Implement Solution 1 (Asset Scoping) in TimeWarp.State
- Documentation: Add guidance about multiple RCL usage to TimeWarp.State docs
- Testing: Create integration tests that verify multiple RCL scenarios work correctly
The core issue is that static web assets are being discovered and processed multiple times when they should be unique per package. Asset scoping is the cleanest solution that maintains functionality while preventing conflicts.
Interesting these types are included for people that use TypeScript if they want to do JSInterop they have the types.
the error is just the first one I think it would fail on the .js files too.
Dang I don't see solution yet.