Packages restored, but your project is still missing references?

blog header image

Here is a classic error that happens to me a lot. It's extremely simple, yet sometimes I find myself spending too much time trying to remember what it is that goes wrong.

Symptoms: You have retrieved a GitHub project you created as part of another solution onto a new machine. You don't get any errors when restoring nuget packages, but still you are missing a lot of assemblies when you try to compile.

I have a bad habit. I build a lot of addons for systems such as Episerver (that's not a bad habit in itself), but when I do, I typically start out with a regular Episerver site on my local machine. Then I add some code to it that solves a given problem. So far so good. At some point I decide "Hey, this would make a great add-on, I should split it out in a class library and share the code or build a nuget". And so I do.

But often I'll stay in the same solution as my initial experimentation site - after all, it's handy to have a site to test out the library in right away. But I don't feel like including the experimentation site in the Github repo, so I often just move the class library there on it's own.

Now, the problem arises if the solution is located in a much different location than the project and you include nuget packages. What happens is that your "packages" folder (where nuget packages are downloaded to) is a subfolder to the solution directory, even though the packages.config is in the project.

And when you add references to nuget packages it will download them to the packages folder and add a relative link in the project file. Like this: 

<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\..\..\Projects\MyProject\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>

Notice how the HintPath is very relative? That's a problem on other machines that doesn't have that path, hence the assemblies cannot be loaded from the packages folder and are missing.

The solution is obviously not to make such a mess of things as I do. But if you have, manually edit the .csproj file and ensure the HintPaths points to the correct packages folder - and that you have a local solution for your project.

Recent posts