TFS Online Build Fails - NuGet package Restore Not working - tfs

I would love to get your help to resolve this issue. The code compiles in local box but TFS build fails for a project saying -
Entity\DbModel.Context.cs (16): The type or namespace name 'Entity' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
Entity\DbModel.Context.cs (19): The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)
Entity\DbModel.Context.cs (26): The type or namespace name 'DbModelBuilder' could not be found (are you missing a using directive or an assembly reference?)
I am using EntityFramework 6.1.1. NuGet package for the project and Package Restore is enabled (in NuGet.targets file) -
<RestorePackages Condition=" '$(RestorePackages)' == '' ">true</RestorePackages>
I think that the package download for the EntityFramework is also failing in TFS even though the other NuGet packages for the same project are getting downloaded before the build the starts in TFS.
I am using 2 packages for this project -
<packages>
<package id="EntityFramework" version="6.1.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net45" />
</packages>
Build Log file -
RestorePackages:
"C:\a\src\.nuget\NuGet.exe" install "C:\a\src\<project name>\packages.config" -source "" -NonInteractive -RequireConsent -solutionDir "C:\a\src\ "
Restoring NuGet packages...
To prevent NuGet from downloading packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages'.
Installing 'Newtonsoft.Json 6.0.6'.
Successfully installed 'Newtonsoft.Json 6.0.6'.
All packages listed in packages.config are already installed.
But after Newtonsoft.Json, it didn't even download the EntityFramework Dlls.

Finally I made it work. It took a while.
At this point, 2 different options available :
Keep the NuGet.targets file and refer that NuGet.targets file from all the project (Didn’t work for me)
Explicitly call the NuGet.Exe package restore before the build
The option 1 didn’t work for me, that’s why to go ahead with the 2nd option,
I had to do:
Delete the NuGet.targets file from the .nuget folder (do not checkin that file)
Delete and do not check in the packages file
Open all the projects file in notepad and remove the reference of NuGet.targets file as mentioned in http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
Go to VS 2013 – Tools > Extensions and Update > VS Gallery – make sure that you have the latest NuGet
At this point, your project is ready to download Nuget packages right before any build, to test that delete the packages folder and start build, it should download all NuGet packages.
Now, for TFS Continuous build, instead of pointing the solution file directly in the build template, use a custom XML build.proj file as mentioned in the http://blogs.msdn.com/b/dotnet/archive/2013/08/27/nuget-package-restore-with-team-foundation-build.aspx.
This XML first calls NuGet.EXE restore path\.sln file, if the *.sln file is not mentioned it picks up any other solution file in the same directory
Check in the build.proj, NuGet.exe all in the root folder along with the solution file
Now, things are going smooth for me with the TFS online CI. Do not right click on the Visual Studio solution to enable the NuGet package restore - it will undo all of the above since it brings back the NuGet.targets file and direct reference to the file in each proj file.

Related

TFS not building after download

I just downloaded the current revision from TFS. However even after restoring the nuget packages it isn't building. Something about missing Nugets.
Any assistance would be appreciated. Is this because we are using different Visual studio versions?
I'm opening the csproj file to build my sln file.
Try following below steps to fix that:
Close Visual Studio
Manually delete the local “packages” folder
Reopen the solution, and rebuild. (Nuget should restore the
packages)
Source Link: Missing Nuget Packages on TFS Build Server
Or run the update-package -reinstall command in NuGet Package Management Console to reinstall all referenced packages.
If that still not work, just try this:
Right click on .csproj and open it in notepad or any editor and delete the following
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
Then build your solution and it should work.
And this thread for your reference: NuGet not restoring packages on build

NuGet Package restore failed for project PROJECT: Unable to find version 2.0.0 of package 'Microsoft.Net.Compilers'

I used the GitHub extension of Visual Studio 2015 to clone my project onto a new computer. I try to restore packages and I get an error that says:
NuGet Package restore failed for project PROJECT: Unable to find version 2.0.0 of package 'Microsoft.Net.Compilers'
I've looked into some other questions about similar issues, but none of those solutions have worked for me yet.
I tried deleting the packages folder, opening up up Visual Studios again and then rebuilding. That didn't resolve it.
I tried manually installing Microsoft.Net.Compilers in Package Manager Console.
PM> Install-Package Microsoft.Net.Compilers
I tried removing this bit of code from the csproj file (this seemed to work for some):
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
I tried reinstalling all packages with
Update-Package –reinstall
So far I haven't had any luck resolving the issue. Any help is appreciated.
EDIT:
I tried the response below and received this error:
Install-Package : Some NuGet packages are missing from the solution. The packages need to be restored in order to build the dependency graph. Restore the packages before performing any operations.
At line:1 char:16
+ Install-Package <<<< -Id Microsoft.Net.Compilers -Version 1.3.2 -Source nuget.org
+ CategoryInfo : InvalidOperation: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetMissingPackages,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
It also prompted me to restore packages. When I hit restore, I got the same error as usual.
Based on your error message looks like you are looking for a version that no longer exists and cannot tell which Package source you have selected. I feel like you are looking for version 2.0.0 which is not available in nuget.org repository. The latest one is 2.0.0-rc and it is pre release candidate.
Please try this command if you want to get the latest version
Install-Package -Id Microsoft.Net.Compilers -Version 2.0.0-rc -Source nuget.org
If you want the latest stable version (1.3.2), try this command
Install-Package -Id Microsoft.Net.Compilers -Version 1.3.2 -Source nuget.org
UPDATE
If the package still cannot be installed, then that package may be out of sync between packages.config, packages/ folder and .csproj file
Please follow these steps to perform manual cleanup
Close visual studio.
Open .csproj in a notepad or some text editor and manually remove all entries related to Microsoft.Net.Compilers
Open packages.config in a notepad or some text editor and and remove entry for the Microsoft.Net.Compilers package
Go to packages/ folder in windows explorer and delete the Microsoft.Net.Compilers folder
Now start the visual studio and open the solution.
Now try to install the package again.
Some of the entries that you may have to remove from .csproj as part of step 2 are these
<Import Project="..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />
<NuGetPackageImportStamp></NuGetPackageImportStamp>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props'))" />
</Target>
I had a similar error after a clean install of Visual Studio 2017 and had to do the following to get it to automatically restore missing NuGet packages successfully. In VS, go to "Tools > Options > NuGet Package Manager > Package Sources", and ensure the appropriate package sources show and are checked.
See below. The addition of the nuget.org package source at the top tells VS to go online to download the packages from NuGet if it can’t find the appropriate versions on the local machine.
I also had similar problem on VS 2019, this one worked for me, just go to Tools > Options > NuGet Package Manager > General and Clear all NuGet Cache.
See the image below
This might be a bit late but it will still help somebody. When you try to check in your code and you get this kind of an error, it means that you had the package installed and uninstalled it again, so you just need to locate the package under included changes, in my example i an using TFS, and exclude or undo. this will solve the issue.
I moved my project in relation to where the NuGet packages had originally been stored in my project, and I eventually discovered that this causes a problem with that .csproj file that might not be immediately obvious.
Following the move and after doing a NuGet Package Restore, a second entry had been added to the EnsureNuGetPackageBuildImports target of my .csproj file reflecting the relative path of the new location.
The target now looked like the following:
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props'))" />
</Target>
Notice the two entries, with different paths to where the packages were located. This meant one of them (the first one, which was from before I moved the project) would always fail.
The fix was simple enough. I just removed the first Error node from the Target.
I also found that there was a similar problem in the initial Import nodes of the root Project node.
I had the following:
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props')" />
Again, the fix was just to remove the erroneous Import node.
This kind of error can happen also using an old version of nuget.exe. For example, if you download the agent.zip of TFS 2015, inside it has version 3.2.1:
\Agent\Worker\Tools\nuget.exe
That version can give the error "Unable to find version '3.7.1' of package 'NUnit'." with a solution created with VS 2015. (NUnit3TestAdapter.3.9.0 doesn't give error, though)
Updating nuget.exe to 5.2 solves the problem.
I know why, it's in the C: \ Program Files (x86) \ Microsoft SDKs \ NuGetPackages folder. There are packages that you have installed before.
This was my error: "The project references NuGet packages that are missing on this computer. Use NuGet Package Restore to download them."
And this was my solution: I had to make sure that my Package Sources were checked in the Manage Nuget Packages window.
go to tools in navbar and tools and features and chek your freamworks are downloaded or not
enter image description here

Build failure - Restore package option not working in Visual Studio Team Services (was VS Online)

I am using a TFS Git project in Team Services and while my project compiles locally, I get a build failure when I check in my code to VS online and manually queue the build.
The errors at the moment are only pertaining to enterprise library data access dlls. I am using V6 of the library and this is error I get
The type or namespace name 'Practices' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
In my build definition I have checked the box against both Restore Nuget Packages and when that alone did not work, I checked the box against [Clean] as well. I still get that error. My packages.config files are checked in for each of the projects in the solution. I also have the Packages folder checked in but this folder has no dlls. It only has the various .nupkg files and respective .xml files checked in. Is this causing the problem?
Yes, you have to either checkin the DLLs or delete the nupkg files. The build server thinks you already have the packages so its not trying to restore them but the DLLs aren't there to reference.

How to fix namespace issues after TFS check-ins?

In setting up a solution for multiple developers, after checking in then checking out on another system, I consistently get the following error:
The type or namespace name 'OData' does not exist in the namespace
'System.Web.Http' (are you missing an assembly reference?)
Without checking in the bin folder, how can this be resolved on the working dev machine?
I found that the System.Web.Http.OData namespace is from Microsoft.AspNet.WebApi.OData which you can install from NuGet. So, to correct your current issue, right click the MVC project on that machine, select Manage NuGet Package, find Microsoft.AspNet.WebApi.OData to install it.
You can get visual studio to download the Nuget packages when you do a build locally, you can enable this through
Tools > NuGet Package Manager >Package Manager Settings
That way you're packages are not added to TFS and anything that your colleagues add to the solution will be automatically downloaded when you rebuild the code, when you first check out from source control

Referenced Dll's not found in Team Foundation Service (or: nuget packages not available on other pc)

when I create a build for the Team Foundation Service, I get all kind of reference dll's not found exceptions.
These references are added by nugget packages.
I've added the 'package restore' option on the solution which added 3 files in a .NuGet folder.
EDIT
When i got the solution from TFS on another pc, i got the same errors (missing dll's), so it's not only the TFS build service having problems.
The missing dll's are are missing files from installed nuget packages (some are part of the default VS template, Unity was a package i added later), which (the packages) are added on the first pc, but then are missing on the next pc (that's why i added the 'or' in the title of this question)
How can i get the Nuget added files on pc2 too?
I guess you've found a solution by now. I write this just to provide an answer for this question.
To have NuGet packages automatically downloaded on another PC, you need to enable NuGet package restore on build. You do this in two steps:
Right click the solution and select Enable NuGet Package Restore.
This will add a .nuget solution folder with NuGet.Config, NuGet.exe and NuGet.targets underneath it. These files should actually be checked in to source control, but the binary file is tiny. It will also modify the MSBuild scripts in all projects of the solution to import the NuGet.targets file to hook NuGet into the build process.
In Tools -> Library Package Manager -> Package Manager Settings make sure the option 'Allow NuGet to download missing packages during build' is checked.
This step must be done on all machines.
Now the BuildDependsOn property of all project build scripts should make the RestorePackages target in NuGet.targets kick in and download missing packages before you get build errors for missing references.

Resources