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

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.

Related

Xamarin vstool build cannot find NuGet MSBuild targets

Background
I recently converted my Xamarin.Forms app from PCL to Net Standard format.
All of my projects now use PackageReference in the csproj file. Which means no more package.config or package.json.
We use TFS 2015 to build, sign, package our .ipa and .apk files. After conversion, the default MSBUILD build steps do not work as they look for mdtool and the new Visual Studio has vstool instead. So, I updated the build steps to use new tools via command line.
All my projects are NetStandard now (including iOS and Android).
Issue
I can successfully restore NuGet packages using restore MySolution.sln -force on Mac build server. But when I run vstool build MySolution.sln after that, I get this error:
error: NuGet packages need to be restored before building. NuGet
MSBuild targets are missing and are needed for building. The NuGet
MSBuild targets are generated when the NuGet packages are restored.
I am able to successfully run the nuget restore and vstool build locally on the build machine. But only when TFS runs the command via agent, it shows that error message.
Setup
Builds: TFS 2015 on Mac agent running Visual Studio 7.5
According to the error and your description, you need also check if your build agent has corresponding capability to support vsbuild.
Take a look at this related question MacOS - Visual Studio Support and give a try with this workaround:
As a work around we set the Xamarin.iOS variable manually in the build
agent and changed the mdtool path in the Xamarin iOS Build step to
"/Applications/Visual Studio.app/Contents/MacOS/vstool".
Besides you could also try to use the suggestion from Matt in the comment above.
Ok. I was finally able to get a successful iOS build on Mac server. This is the setup that works,
Using PackageReference in iOS .csproj
No package.config, project.json, or AssemblyInfo.cs file.
Running nuget restore .sln before building the iOS project.
Build solution using <path-to-vstool>\vstool build .sln -c:<configuration>
Now, I am working on the Windows machine for Android setup. Once I have that working, I will post my findings here.

dotnet restore doesn't pull same packages as Visual Studio 2017

I've been searching for the answer to this but so far haven't found one that has the same situation I'm having.
I have a DotNet Core 1.1.4 project and when I open the .sln project file in Visual Studio 2017 it pulls down the correct packages as soon as I open it. However for the command line if I run the commmand
dotnet restore --force
it is not pulling down packages. Strangely enough if I pull down packages using
dotnet restore --packages .\packages\
it will pull down several, but it appears to be the wrong packages. For example, in one of the .csproj files in this solution there's a reference to System.Interactive.Async 3.0.3 but the package that downloads is 3.0.0. Even if I run this
dotnet restore .\folder\project.csproj
it will not pull down this package that it later complains is missing when I go to build it.
This behavior is very confusing because as far as I can tell dotnet restore is supposed to do the same job Visual Studio 2017 is doing when it restores packages. The project folders do have a packages.config file but my understanding from prior searches is that dotnet restore ignores those. I've verified this reference is in the .csproj file as well the app.config file (not sure if that matters).
If I delete the contents of the .\packages\ folder and perform the dotnet restore .\folder\project.csproj command nothing appears in the .\packages\ folder even though when I open it in Visual Studio 2017 it populates there.
Why is this behavior different, and what do I need to do to get the dotnet restore command to restore the correct packages to the correct location?
If it helps here's the result of my dotnet --info command:
.NET Command Line Tools (1.1.5)
Product Information:
Version: 1.1.5
Commit SHA-1 hash: 2b517ddd29
Runtime Environment:
OS Name: Windows
OS Version: 6.1.7601 OS
Platform: Windows
RID: win7-x64
Base Path: C:\Program Files\dotnet\sdk\1.1.5
Found the answer to this. I downloaded NuGet.exe from here:
https://learn.microsoft.com/en-us/nuget/install-nuget-client-tools
Using the command
nuget restore
installs the correct packages where dotnet restore did not. I'm not sure why but it did fix my issue.

Is it possible to restore Nuget packages with a Xamarin.iOS build using TFS online?

I am trying to automate my build process for my Xamarin.iOS application. I have managed to get a Xamarin.Android and UWP build working find on my on site Windows build agent.
I am now trying to build a Xamarin.iOS application using my On Site Mac build agent.
I have added the Restore Nuget packages as the first step in this build definition but keep getting the error:
MSBuild auto-detection: using msbuild version '4.0' from
'/Library/Frameworks/Mono.framework/Versions/4.2.3/lib/mono/4.5'.
MsBuild.exe does not exist at
'/Library/Frameworks/Mono.framework/Versions/4.2.3/lib/mono/4.5/msbuild.exe'.
Error: /usr/local/bin/nuget failed with return code: 1 Return code: 1
But I'm not sure what it's trying to do here at the nuget installer stage.
So is this possible to do?
There is a known issue with nuget restore command on non-Windows operating systems:
Restore - Restore works with packages.config and project.json files
but will not yet work with *.sln solution files
So you can try to update the "Restore Nuget Packages" step to restore the packages.config file instead of solution file. If it still does not work, then try to change the "Installation type" from "Restore" to "Install".
NuGet 3.2
With NuGet 3.2, the following commands have been tested to work:
Config
Delete
Help
Install
List
Push
SetApiKey
Sources
Spec
Refer to this link for details: NuGet Compatibility.
I was having a similar issue. My log said:
Unable to find version '2.1.0' of package 'Acr.Support'.
[error]Error: /usr/local/bin/nuget failed with return code: 1
After some digging in the logs I tried to restore the nuget packages on my local mac. I opened the terminal and typed :
sudo cd /usr/local/bin
[enter password]
Your nuget executable should be located there.
I executed:
nuget
To see my nuget version, which was 2.12.0.0
I noticed that this is an old version so I updated it by:
sudo su
[enter password]
nuget update -self
I noticed that it updated to 3.4.4 rtm final, I started my Visual Studio Online flow again and it worked! Hopefully this helps anyone.

MSBuild fails due to invalid NuGet version on build server

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

Nuget versioning issue with package restore

I am unable to install a package (using package restore) due to some kind of versioning issue with Nuget. Here are the steps I took:
build project:
error : The schema version of 'MagicalUnicorn.MvcErrorToolkit' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
Extension manager says version is: 2.0.30619.9119.
Ok, restart VS as administrator, extension manager uninstall nuget.
restart VS as administrator again, search for nuget and install.
Extension manager again says version is: 2.0.30619.9119.
rebuild project:
error : The schema version of 'MagicalUnicorn.MvcErrorToolkit' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
Ok, so restart VS as administrator, extension manager uninstall nuget again.
This time download from http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
Run the installer, all seems ok.
Extension manager again says version is: 2.0.30619.9119.
rebuild project:
error : The schema version of 'MagicalUnicorn.MvcErrorToolkit' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
Grr! Any ideas?
It looks like problem in nuget command line tool. When you enable 'Nuget package restore' feature it adds .nuget folder to you solution with nuget.exe. And when you compile project this tool is using to download missing packages, not VS extension is used here. So try update this tool from command line:
cd .nuget
nuget.exe update -Self
Procedure screenshot:
The following worked for me in Visual Studio 2012:
Run VS 2012 as Administrator
Click Tools -- Extensions and Updates
In the left nav, Updates -- Visual Studio Gallery
Click Update on NuGet Package Manager
Install the update
Restart Visual Studio
None of the offered solutions worked for me (Visual Studio 2013, Nuget Package Manager Version 2.8.5).
I had same error but it was saying I had version 2.5.4.
I fixed it by going to Tools > Nuget Manager Console > Nuget Package Console. This open's up a command line window. At the top of the window was a prompt to download missing packages for my project.
Pressed the download button and errors went away upon build.
I was facing the same issue. I resolved it by selecting Tools on VS 2010 Menu --> Library Package Manager --> Package Manager Settings --> Check "Automatically check for updates". After that I restarted VS and was prompted for Nuget Update Installation. Once the update was installed, the Nuget package, HTTP Client in my case, got installed smoothly.
I faced to this problem and I checked all answers that were this page and at the end my problem did not solved. After lot of search on web I could update my nuget from original site
http://docs.nuget.org/docs/start-here/installing-nuget
and for example for vs2010 :
http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
At this site select your version of VS and then update it .
Note : before installing nuget,close all Visual Studios and after install,Open your project.
Thanks
This above did not work for me, completely. There is one last step that is IMPORTANT to note. After downloading the latest nuget version, you may notice that the same error prints in the output window. The reason is due to the .nuget folder containing a NuGet.exe which specifies a version number.
The solution (for me and perhaps to those of your that did not resolve your issue with the above directions), is to
Delete the .nuget folder from you solution.
Right click your web project and click Enable Nuget Restore.
The directions above should cause a NEW .nuget folder to be added, this time the NuGet.exe should be the newest version of nuget that you downloaded.

Resources