How to debug project.json/nuget build issues? - tfs

Visual Studio 2015 Enterprise
Nuget.exe 3.4.4.1321
Project: Windows Forms Class Library
OK I have project.json file setup in my solution and this builds fine. I have NO IDEA how msbuild finds the packages project.json asks for, but that's fine.
I check the files into TFS 2012 (ya I know, our company won't upgrade yet)
TFS fails to build. Says "type cannot be found..."
How on earth do you DEBUG these sorts of issues when you have NO IDEA where MSBuild even LOOKS for packages?
I can GUESS where MS Build may be looking, but how can I confirm this?
csproj hintpaths had their drawbacks, but at least you KNEW where msbuild would look for your package
with Project.Json I have no idea where msbuild is looking!
The package IS there on the build server, right where I expect it to be. How to tell msbuild.exe where to look for packages?

According to your error message, it seems that something wrong with the reference using. For trouble shooting your issue, please try the following.
Please use Package.config for your project to install packages in TFS build. Check if it works to make sure your assemblies and packages are fine.
If Package.config doesn't work. Please provide the detail steps for me to reproduce your issue.
If package.config works but project.json doesn't works. Look at this document: Converting a csproj from package.config to project.json
To debug a build: https://msdn.microsoft.com/en-us/library/jj635150.aspx?f=255&MSPPError=-2147217396
Note: To always use the latest version of the package available, you could add the following to Nuget.config file. So it will get the latest version when you use packages.config. The Nuget.config file is at: C:\Users\UserName\AppData\Roaming\NuGet
(nuget spec dependencies, get latest version?)
<configuration>
<config>
<add key="dependencyversion" value="Highest" />
</config>
</configuration>

project.lock.json will report the resolution path for nuget packages.
This will appear at the VERY BOTTOM of the project.lock.json file:
sample:
"packageFolders": {
"c:\\nuget_packages": {}
Adding this key in nuget.config tells nuget where to restore packages:
<config>
<add key="globalPackagesFolder" value="c:\nuget_packages" />
</config>
This ONLY works for nuget 3+ with project.json file, not with packages.config

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

The default XML namespace of the project must be the MSBuild XML namespace

I cloned the ASP.NET Core SignalR Repo locally, and try opening the solution from within the following environment.
IDE
Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01055
DOT NET CLI
λ dotnet --info
.NET Command Line Tools (1.0.0-preview2-1-003177)
Product Information:
Version: 1.0.0-preview2-1-003177
Commit SHA-1 hash: a2df9c2576
Runtime Environment:
OS Name: Windows
OS Version: 6.1.7601
OS Platform: Windows
RID: win7-x64
I end up seeing a lot of these kinds of error messages:
..\Repos\SignalR\src\Microsoft.AspNetCore.SignalR\Microsoft.AspNetCore.SignalR.csproj
: error : The default XML namespace of the project must be the
MSBuild XML namespace. If the project is authored in the MSBuild 2003
format, please add
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the
element. If the project has been authored in the old 1.0 or
1.2 format, please convert it to MSBuild 2003 format. ..\Repos\SignalR\src\Microsoft.AspNetCore.SignalR\Microsoft.AspNetCore.SignalR.csproj
I want to know how to fix this the correct way.
The projects you are trying to open are in the new .NET Core csproj format. This means you need to use Visual Studio 2017 which supports this new format.
For a little bit of history, initially .NET Core used project.json instead of *.csproj. However, after some considerable internal deliberation at Microsoft, they decided to go back to csproj but with a much cleaner and updated format. However, this new format is only supported in VS2017.
If you want to open the projects but don't want to wait until March 7th for the official VS2017 release, you could use Visual Studio Code instead.
I ran into this issue while opening the Service Fabric GettingStartedApplication in Visual Studio 2015. The original solution was built on .NET Core in VS 2017 and I got the same error when opening in 2015.
Here are the steps I followed to resolve the issue.
Right click on (load Failed) project and edit in visual studio.
Saw the following line in the Project tag: <Project Sdk="Microsoft.NET.Sdk.Web" >
Followed the instruction shown in the error message to add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to this tag
It should now look like:
<Project Sdk="Microsoft.NET.Sdk.Web" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Reloading the project gave me the next error (yours may be different based on what is included in your project)
Saw that None element had an update attribute as below:
<None Update="wwwroot\**\*;Views\**\*;Areas\**\Views">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Commented that out as below.
<!--<None Update="wwwroot\**\*;Views\**\*;Areas\**\Views">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>-->
Onto the next error: Version in Package Reference is unrecognized
Saw that Version is there in csproj xml as below (Additional PackageReference lines removed for brevity)
Stripped the Version attribute
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" />
I now get the following:
Bingo! The visual Studio One-way upgrade kicked in! Let VS do the magic!
The Project loaded but with reference lib errors.
Fixed the reference lib errors individually, by removing and replacing in NuGet to get the project working!
Hope this helps another code traveler :-D
#DavidG's answer is correct, but I would like to add that if you're building from the command line, the equivalent solution is to make sure that you're using the appropriate version of msbuild (in this particular case, it needs to be version 15).
Run msbuild -version to see which version you're using or where msbuild to check which location the environment takes the executable from and update (or point to the right location of) the tools if necessary.
Download the latest MSBuild tool from here.
If getting this error trying to build .Net Core 2.0 app on VSTS then ensure your build definition is using the Hosted VS2017 Agent queue.
I was getting the same messages while I was running just msbuild from powershell.
dotnet msbuild "./project.csproj" worked for me.
if the project is not a big ,
1- change the name of folder project
2- make a new project with the same project (before renaming)
3- add existing files from the old project to the new project (totally same , same folders , same names , ...)
4- open the the new project file (as xml ) and the old project
5- copy the new project file (xml content ) and paste it in the old project file
6- delete the old project
7- rename the old folder project to old name
I had the same problem and solved it by using dotnet instead of msbuild.

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

TFS Online Build Fails - NuGet package Restore Not working

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.

How to remove the nuget Packages folder from TFS?

I just went through a dll hell. Turns out that TFS has been synchronizing packages, and I ended up with two versions of Microsoft.Web.Optimization and its dependencies at once. NuGet was convinced that I have the newest versions installed, and even reinstallation changed nothing. But at the same time, my projects wouldn't build or, if they'd build, they wouldn't start, complaining that they can't find the library version given in the manifest.
I managed to solve it by uninstalling the packages from NuGet console, physically removing their folders from the hard disc, and then doing a fresh build (which re-installed the missing latest versions). But the package folders I deleted manually are still sitting around in the Source Control Explorer. And I can't delete them, the Delete button is greyed out when I select one of them. I am afraid that they will come again when I get newest version next time.
How do I prevent it? I would like to remove them from the solution completely. In the worst case, it would be OK to ignore them, but as they are on a level above the projects, I can't enter them into a project's .tfignore file, and Visual Studio didn't let me add a .tfignore file to the solution itself.
Would it be an option to not checking in the Packages folder to TFS? NuGet now supports automatic package restore, which automatically restores packages on build.
Here is doc for migrating to this new model: http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore.
And this may save you a lot of hassle with checked-in packages folder.
Arguably best answered in this post:
https://stackoverflow.com/a/40409464/1690217
The NuGet documentation provides instructions on how to accomplish this and I just followed them successfully for Visual Studio 2015 against VSTS (Azure-hosted TFS). Everything is fully updated as of Nov 2016. This also still works for Visual Studio 2017 RTM (March 2017).
I added the following files as explained above:
.tfignore
\packages
!\packages\repositories.config
.nuget\nugget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
I had to delete the packages folder and checkin the delete, then package restore will kickin and rebuild the folder but VS will no longer automatically add it to source control.

Resources