Package Restore Fails for Projects Importing a .targets File from a Missing NuGet Package

June 3rd 2013 NuGet Visual Studio

When moving a Windows Phone project referencing Microsoft.Bcl.Async package to a new computer I was unpleasantly surprised. Although I had NuGet package restore enabled on the solution, it not only failed to build, it even failed to load. After some investigation I found out that this was a known limitation in NuGet: if a package imports additional .targets files into the project, package restore can't restore the package in time because project loading fails before it is even triggered.

Since the issue isn't yet fixed in NuGet 2.5 (current version at the time of writing this), I thought I'd post instructions on how to quickly get such a project loaded:

  • Open the solution and wait until any offending projects fail to build.
  • Check the Output window for the error message (View > Output menu item if you don't have it open). You should find one similar to the following:

    The imported project "\packages\Microsoft.Bcl.Build.1.0.5\tools\Microsoft.Bcl.Build.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

  • Note the name of the package marked with bold in the text above.

  • Switch to Package Manager Console (Tools > Library Package Manager > Package Manager Console menu item) and execute the following command (of course replace package name with the one from your error message):

      Install-Package Microsoft.Bcl.Build
    
  • Once the package is successfully installed, you can load the offending project by right clicking on it in the Solution Explorer window and selecting Reload Project. Now you can rebuild the solution to restore any other required packages that are missing. Since you have the Package Manager Console open, you can instead click on Restore button that appeared in a pane at the top of that window.

It's not a perfect solution, but at least for me it's a good enough workaround until the issue finally gets fixed.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License