How to fix nuget reference problems from projects used in multiple solutions

I doubt my company is the only one facing problems with nuget when it comes how to packages are referenced.

For good or bad reasons, it really doesn’t matter, we have the following structure in out trunk.


We primarily work under /Projects in out billable customer projects, while still maintaining a shared codebase under /Shared

The projects in /Shared are referenced from various solutions and this is what often happens.

  • UtilityProject1 referes to a nuget package while in solution1.
  • Later, Solution2, which also refers to Solution1, fails to build. How come?

As it turns out, UtilityProject has an assembly reference to to whatever was in the nuget package and the <HintPath> element for that reference is set to point at Solution1 in the project file. Hence, Solution2 wont build unless Solution1 is built (and have the packages fetched) on the local developer machine.

I really dont know how to fix this problem, but below is a powershell script that atleast removes the symptoms (for csharp projects that is).

$projects = Get-ChildItem -Filter "*.csproj" -recurse
foreach($project in $projects)
  $content = [System.IO.File]::ReadAllText($project.FullName)
  $newContent = [System.Text.RegularExpressions.Regex]::Replace($content,'(.*<HintPath>).*(\\packages\\[^<]*</HintPath>)','$1$(SolutionDir)$2')
  if ($content -ne $newContent) {
    Write-Host 'Updating $project.FullName'
    [System.IO.File]::WriteAllText($project.FullName, $newContent)

The idea is to replace absolute <HintPath> values with values relative to whatever solution is current. SolutionDir defaults to the current solution folder for most projects generated by Visual Studio. This kind of works, as long as someone runs this script within required intervals.