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

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.

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.

Found .NET SDK, but did not find dotnet.dll

I recently installed Visual Studio 2022 preview.
Today I was in a workshop and there was some feature that didn't work, and it turned out it was because I had the preview version of .NET 6 installed.
So I then uninstalled Visual Studio 2022 preview.
Now, when I type dotnet --version (or other command like dotnet --info) in a command window, I get the following message:
Found .NET SDK, but did not find dotnet.dll at [C:\Program Files\dotnet\sdk\6.0.100-preview.7.21379.14\dotnet.dll]
I have tried to repair, and then reinstall DotNet SDK 5.0.400. Restarted machine a few times as well, to no avail.
I am guessing the uninstaller removed the files, but failed to restore the registry to the previous version.
How do I fix this?
So right after posting my question, I discovered that the uninstaller had left some leftovers on my disk. There was a folder with some files, called 6.0.100-preview.7.21379.14. After I deleted it, the command "dotnet --version" now returns "5.0.400".
As Mak wrote below, the folders mentioned reside inside this folder:
C:\Program Files\dotnet\sdk
I also ran into the same problem recently. I had to go to the below path in Windows and delete all 6.*.* folders.
C:\Program Files\dotnet

How to push nuget package to Azure Artifacts?

Hi I have created very basic class library project and created .nupkg. I am trying to push package using the below command nuget.exe push -Source -ApiKey az . To my bad whenever I do it says
'nuget.exe' is not recognized as an internal or external command,
operable program or batch file.
When I search over the internet It says to download nuget.exe so I navigated to nuget download page. To my bad It says nuget.exe comes by default with VS 2019. I am using VS 2019. I am not able to understand whats wrong with these documentation Or I am misunderstanding all these documentations? Can someone help me to identify where should I download or If I exist where is the path and how should I run above command? Any help would be appreciated. Thanks
nuget.exe can be downloaded from https://www.nuget.org/downloads under "Windows x86 Commandline".
Visual Studio 2019 Contains all NuGet tools, so there are no VSIX packages to install for NuGet support as in previous VS versions.
Do note that depending on your selected workloads, Visual Studio will also install the dotnet CLI as part of the .NET SDK. So you can also use dotnet nuget push, which has slightly different arguments (--source and --api-key, check with dotnet nuget push -h)

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)

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.

Resources