1

During an automated build, my nuget package needs to be non framework dependent, however I keep finding that the nuget package getting added is incorrectly adding a HintPath.

Within my nuspec I've defined the files that are part of the package:

<files>
    <file src="lib\xyz.dll" target="lib\xyz.dll" />
    <file src="lib\xyz.xml" target="lib\xyz.xml" />
</files>

However whenever I add the package to my project/solution, it incorrectly adds a hint path specifying:

<Reference Include="xyz, Version=11.0.0.0, Culture=neutral, PublicKeyToken=4a3c0a4c668b48b4">
  <HintPath>..\packages\xyz.11.0.0.0\xyz.dll</HintPath>
  <Private>True</Private>
</Reference>

This is causing the automated build server to not find the assembly and fail to build. I can manually fix the hint path, but would rather not.

I took a look at this post (Failed to add NuGet package) but I don't find it relevant. This post (NuGet package install uses specific assembly version in csproj files) seemed to be referring to the same problem but with no answer. Anybody have any thoughts?

Community
  • 1
  • 1
Randy Walker
  • 153
  • 1
  • 9

1 Answers1

2

You can work around this by using a custom MSBuild task.

Instead of adding the assembly to the lib directory create an MSBuild .targets file named after the package id and put your xyz assembly next to it.

\build
    \Net45
        \MyPackage.targets
        \xyz.dll
        \xyz.xml

Then in the MSBuild .targets file add the reference exactly how you want it to be. Something like:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Reference Include="xyz">
      <HintPath>$(MSBuildThisFileDirectory)\xyz.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

The above shows how to specify a hint path relative to the MSBuild .targets file. You said that you do want to use a hint path so you could remove that if xyz.dll can be resolved by MSBuild somehow, such as it being in the GAC.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
  • Perfect, let me try that out and I think that will be a winner – Randy Walker Aug 04 '16 at 13:31
  • Do I put the targets file in the NuGet package? Or do I put it in the consuming project? – Randy Walker Aug 06 '16 at 13:18
  • Put it in the NuGet package in the build directory. Then it will be added to the project when the NuGet package is installed. There are existing NuGet packages that use custom MSBuild targets such as Xamarin.Forms and Microsoft.Bcl.Build. The only thing that catches people out is that the .targets file needs to have the same name as the package id. – Matt Ward Aug 06 '16 at 13:55