36

I'm having kind of an odd problem with my Team Foundation Service build. I queue it up and it starts just fine, but then it fails with the following error:

C:\a\src\Platform\Prod\Platform.Web\Platform.Web.csproj (436): The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568.

So then I re-queue the build per the message/URL and...it happens again. I've Googled around but I can't seem to figure out what the issue is. I can build just fine in Visual Studio and the solution is configured for package restore. Any thoughts?

Thanks in advance.

Matt
  • 14,906
  • 27
  • 99
  • 149
benjy
  • 4,664
  • 7
  • 38
  • 43

10 Answers10

49

If someone still having this issue on tfs build server you need to go the following:

  1. Make sure all projects in solution you attempt to build have the latest Microsoft.Bcl.Build package (just update it in package manager).
  2. After build failed see all project (in tfs build log summary) that generate this error ("The build restored NuGet packages ...")
  3. Open each of those project's .proj file and comment out whole target element started with 'Target Name="EnsureBclBuildImported"'
  4. Check in and retry the build

It seems somehow after upgrading not all projects get those old block of build code removed and it's causing problems (as I understand it it's not longer needed after microsoft changed their bcl build process).

Michael Logutov
  • 2,551
  • 4
  • 28
  • 32
  • This definitely solved my issues, after trying to sort out the issue for a few hours. – reallyJim Oct 29 '13 at 13:21
  • 22
    the BCL targets file has been nothing but a headache since it was introduced. this isn't actually a solution because the next time you update the package the faux errors are re-added. i really hope whoever manages this at Microsoft can get a clue and undo the mess they've created. it has caused more problems than it has solved, and it continues plaguing us over time. this is a clear case of a lack of dogfooding and oversight, it only ever works in the simplest scenario and it's meant to solve an imaginary problem. frustrating garbage !@#$ – Shaun Wilson Nov 20 '13 at 00:16
  • 2
    @ShaunWilson: I cannot agree with you more. It has been a huge pain for us as well. – Dave New Feb 19 '14 at 08:02
  • 1
    Seconding (thirding?) this. Making the build-process a dynamic dependency of the build-process itself is bound to fail. Coupled with the limitations of TFS's CI build, this is a recipe for disaster. – Jostein Kjønigsen Feb 19 '14 at 08:59
17

The solution to this is specified in link in the error message itself.

This is happening due to an Improvement as specified in that page:

The Improvement

We’ve updated Microsoft.Bcl.Build to use a different approach. The new version will use a conditional import similar to what NuGet’s automatic import feature does. This will always allow the project to load in 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:

Building a second time will fix this error. Please note that this error will only appear if packages were missing so it’s not like you always have to build twice.

Then it specifies as below for the case of build server / continuous integration (CI):

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

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

So, I suppose for your issue resolution the above two steps should be followed.

RinoTom
  • 2,278
  • 2
  • 26
  • 40
  • 3
    Apparently I somehow completely missed that even though I read the document multiple times. Awkward. Thanks. – benjy Sep 26 '13 at 14:50
  • 3
    Is this the actual solution for the question? The question is about TFS builds and not builds in VS. Im looking for the solution for TFS if anyone can help. Thanks – Jamie Hammond Jan 21 '14 at 13:42
  • 3
    @Jamie Hammond The answer explicitly mentions 2 options for the CI server. – John H Apr 23 '14 at 13:25
  • It sounds like this covers Nuget pre 2.7/ Post 2.7 the TFS build server is supposed to be able to do this automagically when you use the newest Build Templates. – George Stocker Feb 10 '15 at 14:00
  • @George, it's supposed to post 2.7, it doesn't though. Judging from our problems. Having to go with checking in the .targets file into CI. – Craig Brett Mar 09 '15 at 08:48
  • @CraigBrett Make sure you're using the newest build templates from 2013. If you imported your build template from TFS 2012, you won't be able to use the newfangled awesome way of Nuget Package Restore. – George Stocker Mar 09 '15 at 11:57
  • My problem is how to implement `step 2`... Should add sort of nuget definition/command inside each .csproj? – Jaider Jun 26 '15 at 17:58
7

I have seen the similar problem in a Xamarin project and doing the following steps fixed my problem;

  1. Set project mode to Release and Rebuild All
  2. Set project mode back to Debug and Rebuild All
  3. Problem sorted.

Weird but worked for me, hope it helps.

Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20
0

Either the required TFSBuild targets files must be included in source control or the NuGet packages must be restored prior to attempting to build the solution.

Details on how to do this are provided on the nuget.org.

It basically involves creating a new build project file which first restores packages and then builds your solution.

amit_g
  • 30,880
  • 8
  • 61
  • 118
Scott Munro
  • 13,369
  • 3
  • 74
  • 80
0

Just had the same issue with TeamCity rather than TFS.

We're explicitly restoring packages before building, yet some projects still error out on the CI server.

We were able to solve it without hacking the csproj files.

We forced the build property BclBuildImported = True after restoring packages. The rogue targets in the csproj files are conditional on this property not being set.

May be a more robust way around it if you can set this property easily in TFS.

lesscode
  • 6,221
  • 30
  • 58
0

I ran into this issue on a Web API project, but we use NAnt.

The resolution was to update the Microsoft BCL Build Components nuget package to the latest (1.0.21) and things build fine now.

GotDibbs
  • 3,068
  • 1
  • 23
  • 29
0

Open the offending csproj in notepad or any other editor. Check if the target EnsureBclBuildImported is available in the csproj. If so, comment out the second error condition inside the target that directs the build to fail even if the package is available (its like fail the build regardless of the package is available or not!).

<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">

<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />

<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>

チーズパン
  • 2,752
  • 8
  • 42
  • 63
  • This just hides the error - but it doesn't actually solve it? How do I know if my project will run if I ignore this error? – AlbatrossCafe Jan 11 '16 at 20:40
0

I had to restore packages prior to the clean target of my build script. I mistakenly thought restoring packages prior to building was enough.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets='GatherBinaries' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>

    <Target Name='RestorePackages'>
        <Exec Command='tools\NuGet.exe Restore "Web.sln"'/>
    </Target>

    <!--
        must call RestorePackages prior to clean to avoid error the following error
        "The build restored NuGet packages. Build the project again to include these packages in the build."
        -->
    <Target Name='Clean' DependsOnTargets='RestorePackages'>
        <MSBuild Projects='Web.sln' Targets='Clean' Properties='Configuration=Release'/>
    </Target>

    <Target Name='Build' DependsOnTargets='Clean;RestorePackages'>
        <MSBuild Projects='Web.sln' Targets='Build' Properties='Configuration=Release'/>
    </Target>

</Project>
mcdon
  • 4,931
  • 3
  • 38
  • 36
0

Problem Story:

In my case, one of my teammate has used the VisualStudio 2017. There we have downgrade some nuget packages from the project file by writing versions with hands like postsharp etc. It has worked out on there and he pushed the updated code to the gitlab. On my side i have pooled the code from gitlab to my local repo and i have opened it with VisualStudio 2019 and i take this error

Solution:

1- I have opened the code with VisualStudio 2017 from my updated local repo and rebuilt it with it.

2- I have closed the VisualStudio 2017 and reopened with VisualStudio 2019. Rebuild is sucessfull this time

For all who faced

Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
0

The right solution can be found here: https://learn.microsoft.com/ru-ru/nuget/consume-packages/package-restore-troubleshooting

Just add to NuGet.Config:

<!-- Package restore is enabled -->
<configuration>
    <packageRestore>
        <add key="enabled" value="True" />
        <add key="automatic" value="True" />
    </packageRestore>
</configuration>
Yevheniy Potupa
  • 537
  • 5
  • 12