Search code examples
visual-studionugetcsprojtargets

Update CSproj Reference path when nuget install package


Details about Problem

I currently use nuget for packaging my internal dependencies. (Nuget packages are stored on network disk).

Now I'm trying to improve this process with: - Add Release dll - Add debug dll + pdb - Add sources - Add targets file

So I create this type of structure in my nuget package

|   My.Package.9.9.9.9.nupkg
|
+---build
|   \---net461
|           My.Package.targets
|   \---net45
|   .
|   .
|   .
|
+---lib
|   \---net461
|       +---Debug
|       |       My.Package.dll
|       |       My.Package.pdb
|       |
|       \---Release
|               My.Package.dll
|   \---net45
|   .
|   .
|   .
+---src
    |   xxxx.cs
    |
    .
    .
    .

So now when I add this package in my project under Visual studio, my targets file seems to not work properly or not used. ( the HintPath is not set with different value for the release mode and the debug mode) .

My CSproj is like that when install the nuget package

...
<ItemGroup>
    ...
    <Reference Include="UnifiedLogin, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
        <HintPath>packages\My.Package.9.9.9.9\lib\net461\Release\My.Package.dll</HintPath>
    </Reference>
</ItemGroup>
<Import Project="packages\My.Package.9.9.9.9\build\net461\My.Package.targets" Condition="Exists('packages\My.Package.9.9.9.9\build\net461\My.Package.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>Ce projet fait référence à des packages NuGet qui sont manquants sur cet ordinateur. Utilisez l'option de restauration des packages NuGet pour les télécharger. Pour plus d'informations, consultez http://go.microsoft.com/fwlink/?LinkID=322105. Le fichier manquant est : {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('packages\My.Package.9.9.9.9\build\net461\My.Package.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\My.Package.9.9.9.9\build\net461\My.Package.targets'))" />
</Target>
....

And my targets file is like

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup Condition="'$(Configuration)' == 'Debug'">
        <Reference Include="UnifiedLogin">
            <HintPath>..\packages\My.Package\lib\Debug\My.Package.dll</HintPath>
            <Private>True</Private>
        </Reference>
    </ItemGroup>
    <ItemGroup Condition="'$(Configuration)' == 'Release'">
        <Reference Include="UnifiedLogin">
            <HintPath>..\packages\My.Package\lib\Release\My.Package.dll</HintPath>
            <Private>True</Private>
        </Reference>
    </ItemGroup>
</Project>

So my question is, how I can use my release folder in release and my debug folder in debug :D My targets files is not good ? Where I did a mistake ?

Thanks in advance for your help


Solution

  • Problem solved, missing net461 in my targets file