MSBuild fails due to invalid NuGet version on build server - visual-studio

I updated to VS2015 this week and along with that updated all NuGet packages in my website solution.
The site builds and run fine locally, however when doing my gated check in to my build server, I get the following when the build fails
As you can see the packages are complaining about the NuGet version, but I'm unsure how to update the NuGet version on the build server. I do have admin privileges to the server, but following the guild described at: http://blog.stangroome.com/2014/02/04/update-nuget-exe-version-used-by-team-build-2013-package-restore/ did not help. For one, my folder was actually C:\Program Files\Microsoft Team Foundation Server 11.0\Tools unlike the C:\Program Files\Microsoft Team Foundation Server 12.0\Tools described in the guide, but additionally I did not have a NuGet.exe within the folder.
I've done a full hard drive search of NuGet.exe, but could not locate it in any directories aside from the directories that are directly related to project source code (.nuget\NuGet.exe).
I have opened visual studio on the server and updated the NuGet version within "Extension and Updates" hoping that would fix the issue, unfortunately it did not.
Any ideas on how to fix the issue/errors presented in the screenshot?

The problem is that you're using the old-style, deprecated-since-NuGet 2.7-style "package restore" that's based on modifying your CSProj files to include references to a NuGet.targets file and a copy of NuGet.exe that gets put into a .nuget folder and ends up source controlled.
Basically, the version of nuget.exe that's on the build server is irrelevant right now -- your CSProj files are pointing to the file in your .nuget folder.
TFS 2013 and beyond has support for automatic package restore without using the NuGet.targets method. You can migrate off of it pretty easily: https://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore

Related

Assets file project.assets.json doesn't have a target for 'net6.0' - VS2022

Just a few days ago got this error, after updating to Visual Studio Community 2022 v17.2 (from v17.1.6):
Error NETSDK1005
Assets file 'C:.........XXXXXX.Web\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. XXXXXX.Web C:\Program Files\dotnet\sdk\6.0.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets 267
Uninstalled everything related with VS2022 + Installer
Rebooted
Fresh Git Cloned the project I'm working on (I work on several computers all with Win 10 and all with the latest updates, this is the only VS installation that presents this problem)
Reinstalled VS 2022 v17.2 (with .NET 6.0, the usual install)
The .csproj file has everything in place:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);(SpaRoot)\**\node_modules\**;</DefaultItemExcludes>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
Always delete the 'bin' and 'obj' folders before build or rebuild....
Cannot get away with the compilation, and always receiving that NETSDK1005 error...
Getting desperate :(
Thanks in advance for any help
P.S. - already checked question 70711153
I found the problem and it indeed had to do with restoring NuGet Packages, in that I have a connection to a corporate NuGet repository, and the call to it was breaking due to wrong credentials.
What was troubling was that the error did not identify the nature of problem with the connection or the username of the credentials getting refused.
On the logged in user popup dialog window, where the several used usernames are presented, there was one username that was required to re-enter its password.
That was all it took.
Visual Studio > Tools > Options > Azure Service Authentication. ReBuild and the NuGet Packages will be restored and build successful.
We had this issue in our Azure DevOps pipeline and it ended up being that the "NuGet Restore" task was using an old version of NuGet. You can see which version the pipeline is using if you check the logs for the "NuGet Restore" task and look for the "Detected NuGet" line.
We:
added in the "NuGet Tool Installer" task before the "NuGet Restore" task
Under the "Version of NuGet.exe to install", list the version you want to use, or the minimum version (e.g. >=6.1.0)
(this step is possibly overkill) Under the "NuGet Restore" Task, check "Disable local cache"
This happens because NuGet writes a file named project.assets.json in the obj\ folder & the .NET SDK uses it to get information about packages to pass into the compiler. In .NET 5, Nuget added a new field called TargetFrameworkAlias, and thus in MSBuild versions < 16.8 or NuGet versions < 5.8, it is possible that you can generate an assets file without the TargetFrameworkAlias as it will read the property and not find it.
You can resolve this issue by ensuring you are on MSBuild version 16.8+ & using NuGet version 5.8+.
In my case I have commented out the TargetAlias line and it published successfully.
Reference: https://developercommunity.visualstudio.com/t/error-netsdk1005-assets-file-projectassetsjson-doe/1248649
For me, I was getting this when updating my projects from .NET Core 3.1 to .NET 6. I had my .NET 6 code in another Git branch and when I switched from the main 3.1 branch to the 6 branch and then tried to build the solution, I would get that message.
After some trial and error, the solution that worked for me was doing the Git checkout and the restore via command line.
Close the solution
From the Developer Powershell (or using regular Powershell or the Visual Studio 2022 Command Prompt), navigate to the local directory that has your repo, and then:
git checkout [branch name]
dotnet restore
Then back in Visual Studio, reopen the solution and build, which would work.
For me, this happened after switching from .net6.0 to .net 7.0 in asp.net core / blazor project. The error occured when trying to publish the project to IIS.
Solution was to switch the "target framework" in the publishing configuration (.pubxml) in the "Publish"-tab.
For me this fix worked:
If you don't have the dotnet cmd line tool, download and install the .NET 6 SDK.
Open a cmd prompt and run the command:
dotnet restore <path to your solution>
(for instance: dotnet restore c:\app\myapp.sln)
nuget restore resolved the same issue
and/or dotnet restore
I had this bug in a solution with several SDK plus non-SDK C# projects.
What fixed my case:
Close the solution.
Separately open the first project of the solution that Visual Studio failed to build.
Build the project. --> "Error not found and build is OK"
Reopen solution. --> "Error disappeared"
I got the same error when publishing Web API to the cloud.
Use Tools ->Command line -> Developer command prompt in Visual Studio 2022, enter AZ login, and after login, restart the visual studio, it is working for me again.
Had the same problem in Azure Devops, using a Windows 2019 build server with VisualStudio 2022
Error:
##[error]C:\Program Files\dotnet\sdk\6.0.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): Error NETSDK1005: Assets file 'C:\agent\vso_work\4\s<..>.API\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project.
Resolved by adding a "NuGet Tool Installer" using version >=6.0.0
Before the NuGet restore task
enter image description here
we got this problem when added cache on gitlab, and started to use dotnet restore,
it happend because we missed the "runtime" parameter to the restore command,
- dotnet restore --packages .nuget --runtime win-x64
- dotnet publish --no-restore --runtime win-x64
In my case the problem was I had updated the Nuget package version in one assembly but not in another, so check you have the same nuget package versions across your solution.
For me, it works to set the target framework to another framework like .NET Core 3.1, build the application, set the framework to the original framework and rebuild.
In my case I had inadvertently added a couple of projects to the solution that were in another folder. I received no errors until I changed NET version from NET6.0 to NET6.0-windows on one of the projects. The solution then didn't build, with 100s of errors, but each individual project built OK.
I noticed that "project.assets.json' doesn't have a target" error among all the errors, pointing to the outside folder.
Bringing those projects into the solution folder fixed the error.
I got the same error sometime back. This worked for me: Logout from visual studio and login to visual studio account
I had an error:
Ensure that restore has run and that you have included 'net6.0' in the
TargetFrameworks for your project. You may also need to include
'win10-x64' in your project's RuntimeIdentifiers.
I removed bin and obj folders for this project and rebuilt the project. dotnet clean, dotnet restore didn't work for me.
I had the same problem ("...\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore...") with clean batch compilation of my sln: msbuild 17.4, nuget 4.7.
I replaced string
nuget.exe restore my.sln
with string
msbuild.exe my.sln /t:Restore
that was before
msbuild.exe my.sln /t:Build
and everything worked.
I got the same error this morning.
This worked for me: right click on solution explorer in visual studio -> 'Restore NuGet Packages'.
Hope this helps.

Project references are not resolved after updating NuGet packges through CLI

I have a nightly process running on TeamCity that will loop through my packages on a locally hosted NuGet repo and update all packages in all solutions to the latest prerelease version.
When I pull the latest code the morning after. The solution builds fine, but my project references are not loaded.
Unresolved project references: https://imgur.com/a/v7Klbkm
Unresolved code: https://imgur.com/a/EEzWgUe
I'm using packages.config to manage my NuGet packages.
In order to continue working I have to "reload" my Visual Studio projects by either manually asking the properties of the missing reference, or by changing something in the .csproj file.
That is my little workaround, but for some solutions with 10+ projects this becomes quite cumbersome.
Other means of updating packages work fine, if I try to update the packages through Visual Studio I have no issues. It only fails when using the NuGet.exe command line utility.
Here's the command we use to update the packages through NuGet.exe on TeamCity during a nightly build:
NuGet.exe update <path_to_packages.config> -Id <current_package_to_update> -pre -NonInteractive
This is the version of NuGet we're using:
NuGet Version: 4.8.1.5435
Here's an excerpt of the logging:
found PFW.Utilities with version 2019.9.0-build0009 in file C:\Sources\Xmp\ModXmp\packages.config
http://srvppratonexus.prato.be:8888/nexus/service/rest/repository/browse/nuget-prato-prereleases/PFW.Utilities/2019.9.0-build0009
MSBuild auto-detection: using msbuild version '16.1.76.45076' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\bin'.
Feeds used:
http://srvppratonexus:8888/nexus/service/local/nuget/nuget-group/
https://api.nuget.org/v3/index.json
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Attempting to gather dependency information for multiple packages with respect to project `C:\Sources\Xmp\ModXmp\ModXmp.csproj`, targeting `.NETFramework,Version=v4.6.1`
Gathering dependency information took 10.79 sec
Attempting to resolve dependencies for multiple packages.
Resolving dependency information took 0 ms
Resolving actions install multiple packages
Found package 'PFW.Utilities 2019.9.0-build0019' in 'C:\Sources\Xmp\packages'.
Removed package 'PFW.Utilities 2019.9.0-build0009' from 'packages.config'
Successfully uninstalled 'PFW.Utilities 2019.9.0-build0009' from ModXmp
Package 'PFW.Utilities.2019.9.0-build0019' already exists in folder 'C:\Sources\Xmp\packages'
Added package 'PFW.Utilities.2019.9.0-build0019' to 'packages.config'
Successfully installed 'PFW.Utilities 2019.9.0-build0019' to ModXmp
Executing nuget actions took 192.26 ms
When I pull the latest code the morning after. The solution builds
fine, but my project references are not loaded.
When you call nuget.exe to update nuget packages for the project using packages.config format outside the VS IDE, it not only modified the packages.config file, but also modified the project file(.csproj). So VS IDE can't load them well unless a reload.
Not sure what's the real solution you have, you can Go Tools => option => Environment => Documents, make sure you've enabled the Detect when file is changed outside the environment. Then you can get reload all dialog when you modify the project file outside VS IDE.
Reload all dialog looks like this:
That is my little workaround, but for some solutions with 10+ projects
this becomes quite cumbersome.
If for some reason that dialog didn't occur, you can manually open one .csproj using notepad, then add one empty line into it and save. VS will detect the change and show the dialog, then click reload all to reload all projects in the solution.(For the solution with many projects)

Nuget package that has no dependents

I am using mspec (machine specification) unit test framework for .NET project.
I used nuget to get all required packages.
There is a specific package, that is not part of the code. It is the "Console" application for the mspec that I used to run my unit tests from outside VS, from the command line.
(It is located here):
When I use Nuget to install it, Nuget doesn't add it to the normal "packages.config" file inside of the Test project.
It is adding it to a "packages.config" folder that exists in ".nuget" folder.
But when my colleague open the project and build, his VS doesn't get that package.
How to change that, to force VS to get the package automatically?
Nuget doesn't add it to the normal "packages.config" file inside of the Test project. It is adding it to a "packages.config" folder that exists in ".nuget" folder.
That is because you are using NuGet 2.7 or later and have a solution that is still configured by old package restore method "MSBuild-integrated restore". This will cause builds to fail with an error stating that you have not given consent to restore packages. Visual Studio will skip automatic package restore.
How to change that, to force VS to get the package automatically?
To resolve this question, you can follow below steps:
Close Visual Studio to avoid file potential file locks and conflicts.
If using TFS: a. Remove nuget.exe and nuget.targets from the solution's .nuget folder and remove those files from the solution workspace. a. Retain nuget.config with the disableSourceControlIntegration setting as explained in Omitting packages with Team Foundation Version Control.
If not using TFS: a. Remove the .nuget folder from the solution and the solution workspace.
Edit each project file in the solution, remove the <RestorePackages> element, and remove any references to the nuget.targets file. For more detail information, you can refer to Migrating to automatic restore.

NuGet not restoring packages on build

I have just created a workspace on a new machine, got latest of our project from TFS, enabled NuGet Restore, and I get the following (skimmed-down) output:
1>------ Rebuild All started: Project: Caching, Configuration: Debug Any CPU ------
1> Restoring NuGet packages...
1> 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'.
1> All packages listed in packages.config are already installed.
1> Caching Framework -> C:\MyProjLocation\Caching\bin\Debug\Caching.dll
2>------ Rebuild All started: Project: Library, Configuration: Debug Any CPU ------
2>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "LumenWorks.Framework.IO". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
2>C:\MyProjLocation\Library\SomeClass.cs(2,7,2,17): error CS0246: The type or namespace name 'LumenWorks' could not be found (are you missing a using directive or an assembly reference?)
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
None of the NuGet packages were restored (there are over 10 - I removed them from the above output for readability sake).
It looks like NuGet is not even trying to restore the packages for the second project (Library).
I have already tried moving the NuGet targets import below the CSharp targets import in the Library.csproj file, as mentioned here, but it's still not working:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
I am running the latest version of NuGet (2.7.41101.371) on Visual Studio 2013.
Edit: The packages.config exists and the NuGet Package Manager has the Library project ticked with the correct packages.
I had to go into Source Control and delete all of the files in the packages folder (except repositories.config) before NuGet would restore the missing packages. The idea is that you are using package restore rather than checking your packages in to source control. If it sees the packages in source control, it won't download them.
Have you deleted the NuGet.targets file from disk too?
If the NuGet.targets file is there, Visual Studio / NuGet.exe will try to do the MSBuild package restore.
See this doc for more info.
I have the same issue, also on the local machine. Although both Package Manager Console and nuget.exe restore MySolution.sln report that everything is installed, there is no packages folder to be found in the solution directory and no references to packages are being resolved.
I checked all project files and they expect packages to be placed in ..\packages folder, the same folder where the solution file itself is located.
The way I made it work is to run:
nuget.exe restore MySolution.sln -PackagesDirectory packages
This forced nuget.exe to download all packages to the specified folder and all references were restored.
Remember that from NuGet 2.7, the targets file is not supported, msbuild suppose to use some integrated way of restoring packages but it fails very often.
In fact, for my own work I prefer using Paket, which always work, when you get used to it. It also supports target files and nice way to create NuGet packages.
I'm not sure about the science behind this, but it worked for me just now after trying to build a freshly-downloaded Visual Studio project, and getting several MSB3245 warnings followed by a build failure due to missing references:
In Visual Studio, right-click on the project with the missing NuGet references, and select "Manage NuGet Packages..."
The Manage NuGet Packages dialog will open. I also saw a message quickly display and then auto-close, with text along the lines of "Restoring NuGet Packages..."
Close the Manage NuGet Packages dialog (without actually changing anything on the dialog), and retry the build.
Edit: Going back in my TimeSnapper auto-screenshot history (no affiliation with that tool -- I'm just a fan), it looks like there was also a message displayed at the top of the Manage NuGet Packages, along with a "Restore" button: "Some NuGet Packages are missing from this solution. Click to restore from your online package sources."
Although the "Restore" apparently automatically happened for me, clicking that button manually might also do the trick to resolve the missing packages issue.
Had to uninstall nuget packages and do a refresh install in order to make it work properly. This might help some of you facing the same issue
I was having the same problem. In my case, it was a NuGet.Configin the parent directory that was setting <add key="repositorypath" value="C:\CxCache" />. So the nuget restore was copying the packages to a folder I didn't know of. See NuGet Configuration Inheritance.
Deleting the NuGet.Config in the parent directory solved the problem.
On Mac; I commited the code to git and deleted everything (main folder), then downloaded it again. Worked afterwards.

Visual studio not detecting package update

I have a dependency on specific nuget package. When I update my solution to use new package version and commit/push solution to remote GIT repository, other contributors to solution, after pull, are still using old DLL version of nuget package, even though package-restore is activated. However, when they open CSPROJ file, it shows that reference should be to the new version. Basically Visual Studio is not generating any warning/error if there is a mismatch between used DLL from bin/obj folder and referenced DLL in csproj file. To be more concise:
Solution uses DLL version 0.5
I upgrade via Nuget to DLL version 0.6
My Visual Studio shows that version 0.6 is used
I commit/push solution to GIT
Another colleague pulls solution from GIT
His Visual Studio shows DLL version 0.5, there is no error/warning
His CSPROJ file references DLL version 0.6
Solution fails on runtime as there is change in internal implementation
How do we fix this:
Colleague deletes his bin/obj
Visual Studio notices that DLL's are missing
He uses Manage Nuget Packages on solution level and just clicks OK, nothing else
Nuget downloads DLL's and issue is solved
Now this is really stupid workaround and in my opinion this should be automated. Is there anything that we might be doing wrong that is causing this to happen?
GIT for VS doesn't take into consideration nugget packages (neither build generated files in \obj and \bin folders).
For Git to manage packages for versioning edit .gitignore file by commenting (#) line below :
# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
#!packages/*/build/
(nb : if you want to backup and rename .gitignore file, go there)

Resources