Team Foundation Service build fails on NuGet package restore

放肆的年华 提交于 2019-11-28 17:13:48

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.

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).

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.

Scott Munro

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.

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.

Saurabh Jha

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>

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>

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.

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

The right solution can be found here: https://docs.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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!