Improved Package Restore

Spil wij&rsquo,ve previously explained wij receive various reports on how our NuGet packages don&rsquo,t play nicely with NuGet&rsquo,s package restore feature. Ter this blog postbode I&rsquo,ll talk about an update wij shipped to our infrastructure package Microsoft.Bcl.Build that reduces the influence.

To recap, some of our packages need to participate ter the build ter order to work decently. Examples of tasks that require build participation include generating roping redirects or selecting the adequate native dependency based on the CPU architecture the build is producing.

Build participation is implemented by adding an invoer to an MSBuild .targets verkeersopstopping. Generally, build participation isn&rsquo,t optional which is why wij determined not to use NuGet&rsquo,s feature to add a target opstopping invoer. NuGet will add the invoer with a condition that checks whether the .targets opstopping already exists, i.e. whether the NuGet package wasgoed already restored.

Both solutions aren&rsquo,t ideal:

A non-optional invoer ensures that building will fail when packages are missing because the imported target verkeersopstopping can&rsquo,t be found. However, this also means that you can&rsquo,t open the solution te Visual Studio (see picture below). Also, the error message isn&rsquo,t truly actionable spil it doesn&rsquo,t include any instructions on how to fix the punt.

An optional invoer permits the project to geyser and build. However, even if you have package restore enabled, the very first build may not work because by the time the project wasgoed build, the targets verkeersopstopping wasgoed still missing. This can result te build cracks or &ndash, even worse &ndash, the build may succeed but with incorrect outputs.

The imported project “<,solution-path>,\packages\Microsoft.Bcl.Build.1.0.8\contraptions\Microsoft.Bcl.Build.targets” wasgoed not found. Confirm that the path ter the declaration is juist, and that the opstopping exists on disk.

Wij&rsquo,ve updated Microsoft.Bcl.Build to use a different treatment. The fresh version will use a conditional invoer similar to what NuGet&rsquo,s automatic invoer feature does. This will always permit the project to flow te Visual Studio.

However, Microsoft.Bcl.Build also adds a target to your project that will run after the build is finished. This target checks whether the current build restored packages and if so fail the build with an actionable error message:

The build restored NuGet packages. Build the project again to include thesis packages te the build. For more information, see

Building a 2nd time will fix this error. Please note that this error will only emerge if packages were missing so it&rsquo,s not like you always have to build twice.

This solution doesn&rsquo,t address build server / continuous integration (CI) screenplays. Te order to successfully use package restore on the build server, you have two options:

  1. Check-in the .targets opstopping.
  2. Explicitly run NuGet package restore prior to building your project/solution.

Prior to version Two.7, NuGet makes package restore a bit more complicated than most people would like it to be:

  1. Very first of all you need to by hand enable it for the solution. This will add a NuGet.exe, NuGet.config and NuGet.targets verkeersopstopping to your solution and you are expected to check those files ter.
  2. Explicitly running package restore for the solution isn&rsquo,t a single step. You need to run a guideline for each project, such spil nuget.exe install .\Project1\packages.config .

NuGet Two.7 makes this a lotsbestemming lighter:

  1. You no longer need to enable package restore explicitly &ndash, when building ter VS all packages are restored automatically.
  2. Running package restore on a build machine is now super effortless. You only need to check-in NuGet.exe (nothing else) and you can waterput it wherever you want. It can even be ter a well-known location on your build server and excluded from every solution if desired. Prior to the build you simply run nuget.exe restore path\to\my\solution.sln .

Also, the NuGet team is talking to all major providers of build/CI servers (incl. TFS) so that at some point the 2nd step can be treated automatically by the build servers. For more details, have a look at the NuGet Two.7 release notes and the fresh Package Restore documentation.

Related movie: Understand Bitcoin and How to Make Money with It

Leave a Reply

Your email address will not be published. Required fields are marked *