0
votes

Using MSBuild, the following builds and works fine:

<PackageReference Include="Publicise.MSBuild.Task" Version="1.3.0"/>

<Target Name="Publicise" BeforeTargets="BeforeBuild">
  <Publicise
    AssemblyPath="..."
    OutputPath="../"/>
</Target>

However, when I add another Package Reference (changing nothing else), it encounters errors on build:

<PackageReference Include="Publicise.MSBuild.Task" Version="1.3.0"/>
<PackageReference Include="ILRepack.MSBuild.Task" Version="2.0.13"/>

This results in error MSB4062:

The "Publicise" task could not be loaded from the assembly ...\ILRepack.MSBuild.Task.dll. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

Why is something completely separate preventing the task from being properly found, and how can I fix this?

1

1 Answers

1
votes

The issue is caused by these two msbuild task nuget packages accidentally.

And since you have installed the ILRepack.MSBuild.Task nuget package at the end. And the PackageReference node of ILRepack.MSBuild.Task is akways at the end. So $(TaskAssembly) is always loads from ILRepack.MSBuild.Task nuget package and the value from publicise.msbuild.task is being covered. And the issue The "Publicise" task could not be loaded from the assembly ILRepack.MSBuild.Task.dll makes sense.

C:\Users\xxx\.nuget\packages\publicise.msbuild.task\1.3.0\build\Publicise.MSBuild.Task.props

enter image description here

C:\Users\xxx\.nuget\packages\ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props

enter image description here

Also, when you project loads the nuget package, you can check under C:\xxx\source\repos\xxx(project_name)\xxx(project_name)\obj\xxx.csproj.nuget.g.props:

enter image description here

Loading the ILRepack.MSBuild.Task.props is always at the end and $(TaskAssembly) is always from ilrepack.msbuild.task due to being overwritten by the later installed package.

The error Publicise task(should be from publicise.msbuild.task) from ilrepack.msbuild.task could be understood.

Solution

So you should make publicise.msbuild.task at the end.

Solution 1)

open C:\xxx\source\repos\xxx(project_name)\xxx(project_name)\obj\xxx.csproj.nuget.g.props file,

modify like this:

<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
   
 <Import Project="$(NuGetPackageRoot)ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props" Condition="Exists('$(NuGetPackageRoot)ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props')" />

 <Import Project="$(NuGetPackageRoot)publicise.msbuild.task\1.3.0\build\Publicise.MSBuild.Task.props" Condition="Exists('$(NuGetPackageRoot)publicise.msbuild.task\1.3.0\build\Publicise.MSBuild.Task.props')" />
</ImportGroup>

make Publicise.MSBuild.Task.props at the buttom.

Then, save the changes and then click Build button rather than Rebuild button to test again.

Solution 2)

downgrade ILRepack.MSBuild.Task nuget package to version 2.0.0.

===============================

Update 1

Thanks for sharing your opinion about the workaround. Since these two nuget packages have to be used in your project, so these two solutions might not be very useful.

The error, conflict is caused by the author of the nuget packages and incidentally, you're using the same TaskAssembly property from these two nuget packages at the same time.

TreatAsLocalProperty="TaskAssembly" will not solve the issue. <packages_id>.props files from the nuget packages are still embedded in the project's CSPROj file. Whether the fields are the same as TaskAssembly or will conflict.

The better solution is that you should rename one of the TaskAssembly of the nuget packages to another, which would not cause conflict.

1) Open C:\Users\xxx\.nuget\packages\ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props file:

change TaskAssembly property to another like TaskAssembly_copy:

enter image description here