GeneratePackageOnBuild with TypeScript bug
When using TypeScript and GeneratePackageOnBuild together causes the following on the second build:
Two assets found targeting the same path with incompatible asset kinds
Reproduce
Create Razor class library
First lets create a new Razor/Blazor class library.
dotnet new razorclasslib -n Bug
confirm it builds
cd .\Bug\
dotnet build
Enable pack on build
Open the csproj
file and add the GeneratePackageOnBuild
property inside of the PropertGroup
:
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
dotnet build
Confirm NuGet creation
Open the generated NuGet using NuGet Package Explorer
confirm the staticwebassets
are included in the NuGet
Add some TypeScript
Create a directory for our TypeScript files and navigate into it.
mkdir ts
Push-Location ts
Create hello.ts
New-item "hello.ts" -Value "console.log('Hello World');"
Pop-Location
Add TypeScript build tools
We need to be able to transpile the TypeScript to JavaScript and put it in the wwwroot
folder. Lets add the tooling to do this.
dotnet add package Microsoft.TypeScript.MSBuild --version 4.8.4
Configure the TypeScript build output for the wwwroot
folder by setting the TypescriptOutDir
property inside of a PropertyGroup
in the project file:
<TypescriptOutDir>wwwroot</TypescriptOutDir>
Include the TypeScript target as a dependency of the PrepareForBuildDependsOn
target by adding the following target inside of a PropertyGroup
in the project file:
<PrepareForBuildDependsOn>CompileTypeScript;GetTypeScriptOutputForPublishing;$(PrepareForBuildDependsOn)</PrepareForBuildDependsOn>
Now confirm that when we build the project it creates a hello.js
file in the wwwroot
folder.
dotnet build
Confirm the NuGet contains staticwebassets
using NuGet Package Explorer.
The Error
Now run dotnet build again.
dotnet build
You will see the following error:
Two assets found targeting the same path with incompatible asset kinds
Workaround
Set GeneratePackageOnBuild to false and ensure you do a dotnet clean
prior to running dotnet pack
.
References
Typescript integration (docs)
Add TypeScript support with NuGet (docs)
NuGet Package Explorer (MS Store)
Razor Class Library isn't adding typescript output to static web assets (Github issue)