Packages are not getting restored - visual-studio

I tried to build a project from downloaded source code using VS2017 community edition. Got following error
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 ..\..\packages\Xamarin.Forms.3.2.0.637442-pre1\build\netstandard2.0\Xamarin.Forms.props.
not sure whats the issue. I tried
Delete packages folder and restore packages.
Delete obj and bin folder. Tried to clean rebuild.

First, restart Visual Studio, cleaning and building the solution most times is not enough.
Try unloading the project in Visual Studio and edit the csproj file.
Search for that props file reference in the file. Sometimes it gets duplicatedthe current version and one outdated version as well. Remove the old one and reload the project.
Check in your Forms, Android/iOS if the csproj is referencing the same Xamarin.Forms version,and are no strange lines/characters.
<PackageReference Include="Xamarin.Forms" Version="x.x.x.xxxxx" />
In the last resort, try to downgrade the XamarinForms version and check if it helps.

Related

Attempting to split projects out of a main solution and Nuget reference is not seeing the packages

With VS 2022 and multi-repo support I was excited to start splitting out some projects into their own solutions. But I ran into an issue with the associated test projects.
I picked one of the smaller projects and the test project and made a new folder in my workspace for it.
I then opened the project and added the test project.
Immediately I got an error, "This project references NuGet packages(s) that are missing on this computer. Use the NuGet Package Restore to download them ... The missing file is ..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.props.".
I didn't think much of this initially and ran the package restore and the error persisted.
I should also mention that the main project doesn't have this issue; the one I opened initially. One the project that I added to the new solution seems to have this issue.
When I run the package restore again, it says that all packages are already installed and there is nothing to restore.
In playing around, I unloaded the test project and looked at the project file. I noticed that it's referring to one folder up called "Packages":
<Import Project="..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
I then took the opportunity to convert to nuget package reference but selecting it in the right click menu. The above lines did not change in the project file. Using references, it was my understanding that it was using my local "profile\ .nuget" folder.
To test that it was looking at this folder, I created the "..\Packages" folder manually and copied in the missing nuget packages.
The error went away and I was able to compile it.
However, this doesn't fix the problem. If I were to check this solution out again, this folder would be missing.
I'm assuming that the project file is what is causing my problem. Does anyone know how I can get a project to force looking at my profile.nuget using package reference?
After much work I finally found a blog that talked about the differences between packages.config and packagereference. It appears the migration didn't complete properly and didn't clean up the packages.config configuration.
I backed up the .csproj file in question
I then unloaded the project and edited the .csproj file
Anywhere that the packages were reference, I removed the entries.
Then I reloaded the .csproj file and it compiled.
When I looked at the references folder in the solution explorer I saw that the icons changed for the packages that were not found prior.
Not sure why this happened, but this is what fixed it.

How do I fix the NuGet Package Manager not opening/working?

I have Visual Studio Professional 2019, and I'm working on debugging a project that has issues with its NuGet packages. The NuGet Package Manager button is present when right clicking the solution.
But when I click the button, nothing happens. Nothing opens. Absolutely nothing, no error code, no messagebox, no warning in the debugger. I've tried uninstalling and reinstalling my NuGet extensions, nothing changed. I uninstalled and reinstalled visual studio and made sure to enable the package manager, nothing. I restarted my computer and Visual Studio, nothing.
The thing is, I tried opening another project (this time one of my own), and the package manager works fine, which leads me to believe the guy who made this program disabled the package manager entirely, but somehow kept the button there? I don't know exactly what the hell he did, but right now I'm not able to actually work into getting the packages I need to even launch the program. I can't look at ANY part of it either until the packages are added, because Visual Studio won't display anything.
Projects and solutions can't change the Visual Studio menu options themselves. This issue might occur for you if you are missing a .sln file for your project or if it has become corrupted (you don't actually need a .sln file to open a project in Visual Studio). If you notice, Visual Studio's NuGet Package Manager option in the menu says
Manage NuGet Packages for Solution
(emphasis mine). If you don't have the .sln file and associated properties file, then Visual Studio can't do anything when you click on that menu... there is no 'Solution' (.sln file) for it to manage packages for, in essence
Check the original working repository of the code where the project was created and look for a .sln file and a .vssscc file, and copy those over to your new working repository and open the project via that .sln file, and NuGet Package Manager should open successfully now.
If you can't find them or if the working repository doesn't exist anymore, then you'll need to create a new solution in Visual Studio and import/copy all the project files into the new solution. I don't know of a way to 'rebuild' or 'recreate' a solution file and solution properties file for a project that doesn't have them anymore.
For reference, I have had this issue when running VS on a new computer and opening projects I created, so it is not necessarily some form of 'sabotage'.
Though perhaps not the case for the original question asker, one cause of the "NuGet Package Manager" just won't open issue is a syntax error in the project's packages.config file.
In my case, a merge conflict I didn't notice added merge text (the "<<<<<.." type stuff) to the config file. That lead to a silent read error, which I didn't notice until I opened the actual NuGet Package Manager Console.
This issue occurred for me when I opened a project directly. When I instead opened the project by opening a saved solution (*.sln file) that contained the project, it went away.
In my situation, the problem was duplicated targets in the Sdk Style projects.
I imported an AssemblyInfo.Pack.Common file that defined a PreBuild and PostPack target into a Sdk Style project. But these were also present in the project file itself. The solution was to remove that targets from the project file.
In my case, I was using Visual Studio 2022 and my project was targeting .NET Framework 4.6.2.
I resolved it by removing duplicate package reference in my packages.config file which happened out of merge.
From,
<package id="Microsoft.IdentityModel.Tokens" version="6.25.0" targetFramework="net462" />
<package id="Microsoft.IdentityModel.Tokens" version="6.25.1" targetFramework="net462" />
To,
<package id="Microsoft.IdentityModel.Logging" version="6.25.0" targetFramework="net462" />
This now opens the Nuget Package Manager for my project.

How to fix all Nuget references in a .net core solution quickly?

I am finding it a common issue where when I download a .NET repo from Github, the solution has missing references. See an example below. When using Nuget Package Manager, it says the packages are installed already. The packages folder is missing.
I tried using 'Update-Package -reinstall' and got a bunch of lines with 'No package updates are available from the current package source for project 'project name'.... That's probably due to the missing package files.
I can fix each reference manually but it's a time consuming process. Is there a way to force fix all these references in all the projects in one swoop using a command or a tool that 'cleverly' knows what to do? I am using Visual Studio 2019 16.3 preview 1
In VS2019, those .net core projects or .net standard projects use PackageReference instead of packages.config format to manage nuget packages. And in this way, the content in xx.csproj looks similar to this:
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.1.3" />
<PackageReference Include="MediatR" Version="6.0.0" />
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>
Every time you download a project which uses packageReference format, you only get the xx.csproj and related source files but without nuget packages. So when you open and load the solution or project in VS IDE, it will display the nuget packages name in solution explorer but with yellow warning triangle since now VS can't find those packages locally.
Is there a way to force fix all these references in all the projects
in one swoop using a command or a tool that 'cleverly' knows what to
do?
Just like what zivkan said in his answer, NuGet is normally configured to restore packages automatically if you've enabled those two options. Every time when we load the soultion in IDE, it will automatically restore the packages for us. Not sure the reason why it seems not to work in your machine, but you can check and try:
1.Go Nuget Package Manager=>Package Sources, make sure you've set https://api.nuget.org/v3/index.json as one the Available Package sources.
2.Right-click the solution in Solution Explorer, choose the Restore Nuget Packages to restore packages for whole solution.
3.Or you can Unload and reload the project to check if it helps.
4.By default, the packages are stored at C:\Users\xxx\.nuget\packages folder, you can check if those folders exists there.
(I delate all packages in that folder, so it's clear when I open the git solution in VS, the nuget is installing those necessary packages automatically, I'm not sure if there's any possibility that cause too many packages are being installed,so it will take some time? I check and find, to restore necessary packages for NorthWind solution, it require packages for over 370 MB)
I tried using 'Update-Package -reinstall' and got a bunch of lines
with 'No package updates are available from the current package source
for project 'project name'.... That's probably due to the missing
package files.
And for the reason why Update-Package -reinstall not work well in the project, maybe it's a similar issue like nuget issue #4103, for the packagereference in xx.csproj, several commands for now are not supported.
And after my check in my machine, the command obviously do not work for a PackageReference-format .net standard project. When I've installed related packages successfully, I run this command but only get No package updates are available from the current package source for project xxx, then I delete the installed packages, clean the cache and run the command again, I still get same message!
So I think this command can't recognize PackageRefernece format in .csproj, no matter whether I install related packages or not, it will always throw message No package updates are available ...
NuGet is normally configured to restore packages automatically. See the options page. The second Package Restore option, "Automatically check for missing packages during build in Visual Studio" is enabled by default on clean installs.
Here's a little video I recorded of what happens on my machine when I open the solution. Keep an eye out on the status bar at the bottom left of the window.
If your installation of Visual Studio does not behave in the same way with the automatic restore option enabled, then there is some other issue on your machine, but we don't have enough information from your question, as of the time I wrote this.

NuGet downloads all packages, doesn't update references

I have my project set to Allow NuGet to download missing packages as well as Automatically check for missing packages during build in Visual Studio. In my solution folder, there is a packages folder and it contains everything that I need for my project. However the references to them in the project are still broken.
I have tried removing the references and adding them with NuGet, but NuGet says the item is already in the project (it is in the packages folder) even though the reference is there and the project can't build. The only way that I can seem to get around it is to manually go into each of the packages in the packages folder and select every .dll.
Is there a better way to do this?
Open package manager console and type:
Update-Package -Reinstall
This should refresh all your references based on each project's packages.config file.

Opening project in Visual Studio fails due to nuget.targets not found error

So I downloaded Twitterizer from http://www.twitterizer.net/downloads/
I try to open it in Visual Studio and get all these nuget errors:
The imported project "C:\Twitterizer\.nuget\nuget.targets" was not found.
Confirm that the path in the <Import> declaration is correct, and that the file
exists on disk.
What is going on. How do I deal with this?
Install Nuget.
Right click on the solution and select "Enable NuGet
Package Restore". In Visual Studio 2013 and later, select "Restore NuGet Packages" instead.
Click Ok on the warning.
Close and re-open the solution.
Should now be hunky-dory.
An alternative is to edit the .csproj file with a texteditor and remove or comment out the segment.
This error normally happens when you are trying to open a .csproj directly, not through the solution file, and the .csproj imports the Nuget targets like this:
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" />
In order to solve it, you can either open the .sln and not the .csproj directly...or remove the import line above.
Notice that the mentioned error will only happen when you are using solution wise package restore, which isn't even recommended anymore.
Just ran into the same problem, but in my case the issue was a space in the folder name:
Nuget was telling me it couldn't find "C:\git\My Path" but I was able to navigate to "C:\git\My%20Path". Removing the space in the foldername "fixed" the issue.
I know I'm late to the party but here is a really good tutorial on how to fix this issue. I used this to fix my project.
Close down Visual Studio If the solution you are trying to migrate is
open in Visual Studio, then changes may be lost. Visual Studio may
overwrite/ignore your changes in some cases and the NuGet extension
will also try to re-enable Package Restore when it sees some projects
in the solution are missing it.
If you are using TFS Remove the NuGet.exe and NuGet.targets files from
the solution's .nuget folder. Make sure the files themselves are also
removed from the solution workspace. Retain the NuGet.Config file to
continue to bypass adding packages to source control. Edit each
project file (e.g., .csproj, .vbproj) in the solution and remove any
references to the NuGet.targets file. Open the project file(s) in the
editor of your choice and remove the following settings:
true ... ...
<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}.
If you are not
using TFS Remove the .nuget folder from your solution. Make sure the
folder itself is also removed from the solution workspace. Edit each
project file (e.g., .csproj, .vbproj) in the solution and remove any
references to the NuGet.targets file. Open the project file(s) in the
editor of your choice and remove the following settings:
true ... ...
<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}.
Migration Script
Many users have requested a migration tool to disable the
MSBuild-based package restore and convert to Automatic Package
Restore. The NuGet team has decided not to provide a supported tool
for this because of the high probability of edge cases that would be
unhandled. However, Owen Johnson has authored a PowerShell script that
can work in many cases. It's available on GitHub and can be used at
your own risk. In other words, be sure to commit to source control
before running it, just in case it doesn't work in your scenario.
Nuget.target Fix
When i get the nuget.targets not found error i use the package manager to uninstall-package one of the packages in the project and then reinstall it using install-package. It seems like it regenerates the nugets.target file then.
Easiest solution when you get this error in order to restore the missing NuGet.targets in Visual Studio Solution Explorer is to:
Right click on the solution file
From the context menu click "Enable Nuget Package Restore..." option
this will download the missing files in the ".nuget" folder :)
The above assumes you already have Nuget installed - if not follow the accepted answer above!
UPDATE:
Please note for Visual Studio versions beyond 2013 the option is called "Restore NuGet Packages"

Resources