Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet update and conditional references

Our nupkg packages contain multiple versions of the same dll (x86, x64, AnyCPU) and in the csproj files, in references I use conditional references to pick a specific dll depending on the current platform set. As a result I have multiple references to the same library (just different platform compilation).

here's a fragment of my csproj file:

<Reference Include="xxxx" Condition="'$(Platform)'=='x86'">            
   <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x86\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx" Condition="'$(Platform)'=='x64'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x64\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx" Condition="'$(Platform)'=='AnyCPU'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\AnyCPU\xxxx.dll</HintPath>
</Reference>

This construction works very well in both MSBuild and in Visual Studio.

Unfortunately after nuget update the csproj references get messed up. Here's the result:

<Reference Include="xxxx" Condition="'$(Platform)'=='x86'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x86\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx" Condition="'$(Platform)'=='x64'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x64\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx">
  <HintPath>..\..\packages\xxxx.2.7.0.1094\lib\net45\x86\xxxx.dll</HintPath>
</Reference>

So looks like only one reference got updated and... the Condition section got dropped as well as the first dll on the list was used.

Not what I was expecting. Any ideas how to best work around that problem? Anyone using conditional references in your csproj's with nuget? Any advice would be greatly appreciated!

like image 946
Michal Rogozinski Avatar asked Apr 30 '14 16:04

Michal Rogozinski


1 Answers

Nuget lets you deploy a .targets file that is automatically included in your project (see Nuget docs). Yo you can include the conditional references in the custom targets file and deploy the dlls in the tools folder of the package so they are not added as references by Nuget automatically.

Lets assume your package is named 'PackageWithConditionalReferences'. The folder structure your nuget package is created from could look like this:

tools
    lib\net45\x86\xxxx.dll
    lib\net45\x64\xxxx.dll
    lib\net45\AnyCPU\xxxx.dll
build
    PackageWithConditionalReferences.targets

where PackageWithConditionalReferences.targets has content:

    <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>    
    <MyLibDir>$(MSBuildThisFileDirectory)..\tools\net45</MyLibDir>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="xxxx", Condition="'$(Platform)' == 'x64'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(MyLibDir)\x64\xxxx.dll</HintPath>   
      <Private>True</Private>
    </Reference>
    <Reference Include="xxxx", Condition="'$(Platform)' == 'x86'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(MyLibDir)\x86\xxxx.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="xxxx", Condition="'$(Platform)' == 'AnyCPU'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(MyLibDir)\AnyCPU\xxxx.dll</HintPath>
      <Private>True</Private>
    </Reference>
  </ItemGroup>

</Project>

Make sure your .targets file is named liked the package. After installing the package a restart of VisualStudio is neccessary for the references to get visible (tested with VisualStudio 2015).

like image 161
Alex Avatar answered Nov 15 '22 14:11

Alex