Problem
In my ASP.NET Core project, I referenced the NuGet package tinymce.7.3.0.nupkg
, which contains the following build\TinyMCE.targets
file:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<SourceScriptFiles Include="$(MSBuildThisFileDirectory)..\content\scripts\tinymce\**\*.*" />
</ItemGroup>
<Target Name="CopyScriptsToProject" BeforeTargets="Build">
<Copy SourceFiles="@(SourceScriptFiles)" DestinationFolder="$(ProjectDir)\wwwroot\lib\tinymce\%(RecursiveDir)" />
</Target>
</Project>
After installing this package, the TinyMCE files are copied to the wwwroot\lib\tinymce
directory during the build.
I created a similar NuGet package Moonglade.MonacoEditor
with the following build\Moonglade.MonacoEditor.targets
file:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<SourceFiles Include="$(MSBuildThisFileDirectory)..\content\monaco\**\*.*" />
</ItemGroup>
<Target Name="CopyScriptsToProject" BeforeTargets="Build">
<Copy SourceFiles="@(SourceFiles)" DestinationFolder="$(ProjectDir)\wwwroot\lib\moonglade-monaco\%(RecursiveDir)" />
</Target>
</Project>
This package also successfully copies files to wwwroot\lib\moonglade-monaco
during the build.
However, when both packages are referenced in the same ASP.NET Core project, only the files from Moonglade.MonacoEditor
are copied, and no files from the TinyMCE package are copied to wwwroot\lib
.
Solution
After work 996. I found that in MSBuild, target names must be unique. If two `.targets` files define targets with the same name (e.g., CopyScriptsToProject
), the latter will overwrite the former, resulting in only the files from the last loaded package being copied.
To resolve this issue, I need to rename the target, ensure each `.targets` file has a unique target name. Since I do not own TinyMCE, I have to rename target in my own Moonglade.MonacoEditor
package.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<SourceFiles Include="$(MSBuildThisFileDirectory)..\content\monaco\**\*.*" />
</ItemGroup>
<Target Name="CopyMonacoScriptsToProject" BeforeTargets="Build">
<Copy SourceFiles="@(SourceFiles)" DestinationFolder="$(ProjectDir)\wwwroot\lib\moonglade-monaco\%(RecursiveDir)"
/>
</Target>
</Project>
Now, both packages can correctly copy their own files!
Rodwell
After spending ages on this I just came to the same conclusion, searched in Google and found this article so thanks for the confirmation.
I was hoping to solve this by using a known unique variable as part of the target name but despite CoPilot assuring me to the contrary this doesn't seem possible so manually making the name unique seems the only solution.