Visual Studio 2015 can't find nuget package references and DLLs in packages folder - visual-studio

I've been given a C# project by someone else. I opened the project and tried to install all the Nuget packages by doing a Update-Package -reinstall.
It downloaded all the packages in the packages folder.
/Projects
/WebApiTest (root folder)
WebApiTest.csproj
WebApiTest.sln
/packages (packages are loaded in this folder)
But the entire project still says that it's missing all the references. I editted the csproj file to see where it tries to load the packages from, and that is:
<ItemGroup>
<Reference Include="Antlr3.Runtime">
<HintPath>packages\Antlr.3.4.1.9004\lib\Antlr3.Runtime.dll</HintPath>
</Reference>
<Reference Include="EntityFramework">
<HintPath>packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer">
<HintPath>packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
... etc.
So I assume it starts looking in the same directory as the csproj file is in. In that same directory is also the packages folder that has all the DLL files. So the path seems to be right.
Why is my project still not able to load the references?

This post is starting to get old, but as I have encountered this issue myself and fixed it, I'll share a possible solution.
I had to manually uninstall and re-install the packages by right-clicking on the problematic project, Manage NuGet packages..., then uninstall/reinstall from there.
Be careful on the packages version: be sure to grab the same, in case the project isn't using the latest.
A noticeable effect for me after this procedure is that the paths in the csproj file got updated.
Hope it'll help someone :)

Open up the csproj file for your project in a plain text editor and fix the paths for the DLL's. You may see something like:
<HintPath>packages\etc\etc\etc...</HintPath>
I changed those occurrences to:
<HintPath>..\packages\etc\etc\etc...</HintPath>
I pushed this change from the computer that the project was working fine on and pulled them back down on the bad machine and it was no longer an issue.
Note: This wouldn't be an issue if your project file was in the same folder as your packages folder.

Does NuGet successfully restore the packages from the command line?
Try running this from the command line, from the same folder as your solution:
nuget restore WebApiTest.sln
Note, you may need to fully qualify the execution of nuget (e.g. C:\nuget\nuget.exe), or have it in your path.
You may need to download the NuGet executable, if you haven't already.
If that doesn't work, hopefully it will shed some light on why!

What fixed the missing project references for me was to simply:
Close Visual Studio
Delete the "packages" folder where all the nuget packages are stored (probably at your solution-level or else project-level directory)
Open the solution again in Visual Studio
If you have automatic package restore on, just rebuild, else first restore the nuget packages (from the solution context menu) and then rebuild.

Related

Nuget packages are installed outside of the project solution

Let's say I have a solution called MyFirstSolution and inside I have project with name MyFirstProject. Inside that project I'm installing nuget packages like Dapper, EntityFramework etc...
How things should work:
packages folder should be created into the folder MyFirstSolution and inside should be downloaded and installer all nuget packages referenced into the project.
The problem I'm having is that the packages folder is not created and the nuget packages are downloaded one level above the MyFirstSolution folder into some folder called XYZHelper.
When I download a solution from some repository nuget packages are restored to that same folder, not to the solution itself and references in csproj file says that they should be in solutionFolder\packages so all of my projects don't build.
If I copy the packages manually to the folder that they should be, it's all good but after every build a copy is made into that XYZHelper folder.
I don't have any postbuild events or anything like it configured into the projects. It is something with the visual studio I guess, but I can't figure out what.
How things should work: packages folder should be created into the
folder MyFirstSolution and inside should be downloaded and installer
all nuget packages referenced into the project.
Hi Stdfan, not sure about your VS version. But for VS2015 and earlier versions, the nuget packages are controlled by packages.config file. And things should work like what you mentioned.
But for VS2017 and VS2019, they have two methods to manage nuget: Packages.config and PackageReference. And for PackageReference format, the packages are stored in C:\Users\xxx\.nuget\packages. So if your vs version is VS2017 or VS2019, you can try if changing the format to PackageReference help resolve this issue.
The problem I'm having is that the packages folder is not created and
the nuget packages are downloaded one level above the MyFirstSolution
folder into some folder called XYZHelper.
Direction1:
Like zivkan suggested,I also think something affects the restore process.Normal for Packages.config format, the folders would be stored in packages folder. But according to
this document, we can customize nuget.config file to control nuget behavior. So please check locations where nuget.config exists,there might be some changes in the nuget.config for computer or some settings in nuget.config for users which causes this issue.(The nuget.config for user won't exist unless we create it there)
Direction2:
When I download a solution from some repository nuget packages are
restored to that same folder
As the restore process is invisible in build output, so there is possibility that the nuget restore works well, but something in build process move the content of packages folder into XYZhelper.
Check customize your build. Please check your directory structure for the Directory.build.xx file, it can affect your build process if it exists in any folder of the structure: C:\xxx\lancel\source\repos\
I don't have any postbuild events or anything like it configured into
the projects. It is something with the visual studio I guess, but I
can't figure out what.
This is not about VS normal settings. I think some custom file causes this issue(no matter nuget.config or directory.build.xxx), and please check if you've installed any third-party software or vs extension. Try close vs, delete the .vs, bin and obj folders and then run vs as safe mode.
Target folder of nuget packages can also be set using the envirenment variable NUGET_PACKAGES.
And you can do this in your existing project-file like this:
<Project Sdk="Microsoft.NET.Sdk">
...
<Target Name="NugetPublicfolder" BeforeTargets="PreBuildEvent">
<Exec Command="SET NUGET_PACKAGES=C:\MyProjectA\libraries\"/>
</Target>
...
</Project>
Now when you build/publish the project the libraries will be placed in C:\MyProjectA\libraries\
Actually the nuget packages are first downloaded to the socalled http-cache folder (%userprofile%\AppData\Local\NuGet\v3-cache), where it will be extacted from to the above folder (which is called the globalPackagesFolder folder)
The solution packages folder can be changed with a nuget.config (the setting name is called repositoryPath). So, check for nuget.config files that might be setting this value. You can get an exact list of nuget.config files used by a restore by downloading nuget.exe from nuget.org/downloads and running nuget restore MyFirstSolution.sln. Near the end of the output it will list every nuget.config file that was read.
When there is no config file that changes the solution packages folder (repository path), it always defaults to a subdirectory named packages in the same directory as the .sln file. The only way I know of to change this location for packages.config projects is with a nuget.config file, so I feel confident you should be able to solve it by finding the right config file.

Binaries are added to /bin without a reference, why?

I am using a custom built NuGet package with a set of binaries from DevExpress. Whenever I build the project from Visual Studio, a lot of "extra" DevExpress binaries, that I have not added a reference too, is also added to the bin folder.
I have installed a DevExpress suite of some sort so it might be that some of the GAC'ed binaries are added, but why ? I do not wish to have these binaries cluttering my bin folder.
VS references:
Bin folder:
Might be that assemblies knows its dependencies. The Assembly Explorer show you which:
In addition to Rolf's answer.
If the reference is set to "local" in the project, the dll's will be moved to the output folder (\bin)
In the project those refenrences will have a <private>True</Private> tag:
fx:
<Reference Include="log4net, Version=2.0.8.0, ...>
<HintPath>..\packages\log4net.2.0.8\lib\net40-full\log4net.dll</HintPath>
<Private>True</Private> <-- HERE
</Reference>

Is NuGet automatic package restore in Visual Studio 2015 a fairy tale/mythical beast?

I have been trying to figure out why automatic package restore in Visual Studio 2015 doesn't work.
As I understand it there is nothing to do but check a couple of settings. When you build, it looks for missing packages and downloads them automatically.
I have a solution that has 15 individual projects. The majority of them will not compile because of "missing packages".
I do not have any of the legacy NuGet (.nuget folder etc.) in any of these projects and I have the latest and greatest version of NuGet.
Visual Studio simply will not download the missing files. I deleted the solution package folder and it does re-create and download all of the packages when I build, but each individual project still shows missing references.
If I go to the package manager console and issue a
Update-Package -reinstall
then the packages download and everything works. I'm just wonder why it doesn't do this automatically.
It's supposed to right?
NuGet Restore only restores files in the packages directory, but does not restore files inside your project or modify your project.
For example, if there has a package will add some reference dlls or add some content files into your project. After deleting these reference dlls and content files from your project, it will not be added when restoring this package. This will cause the your project could not find the missing pacakges when compile.
So we need use "Update-Package -reinstall" command to force reinstall the package references and content files into project.
Update the example for sharing projects in a team:
Following is my solution structure, the CommDLL project installed some NuGet packages and entire solution is managed by a source control.
I download this solution on another machine from source control use another user account and install another NuGet packages into the CommDLL project. Then use some content from the new installed package and build the project successful. Please make sure the package dlls has been added into your project and it has been set the correct HintPath in .csproj file.
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
Next, I check in this modified project into the source control. Please make sure you the .csproj file and packages.config file are checked in.
Now I get the latest version on another machine to get the modified content. After check out the latest version from source control, the package references are shown with a warning because the project is not compiled, which means the packages are not restored. Please rebuild this project, it will restore the packages for your project (make sure your Visual Studio 2015 has enable "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in Visual Studio" before rebuild this project).

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.

Error using nuget in VS2012 "missing packages"

When I build my project from within VS2012 I get the following error message
This project references NuGet package(s) that are missing on this computer.
Enable NuGet Package Restore to download them.
I have the nuget options set for NuGet to download missing packages.
Yet I still get the error.
I have nugget 2.7 installed. With VS2012 update 3
Please follow below mentioned steps:
Step 1:
Please enable Nuget Package Restore by right clicking on solution [as mentioned in below screenshot]
Step 2: [Follow this if the issue / error is not resolved by following step 1]
Still if you face the issue, please open .csproj file in notepad and check for the package path which might look like
So your solutions directory structure will be like:
\SolutionDirectory\
Package Directory:
\SolutionDirectory\packages
Project Directory:
\SolutionDirectory\ProjectName\ProjectName.csproj
Please open this .csproj [in which you're getting error] in notepad and search for packages path and update it to its relevant path.
For e.g. my .csproj contained, if .csproj file contains ..\..\packages then update that path with ..\packages
As Dan was alluding to, if your solution has a .nuget folder (from enabling package restore), then nuget 2.7's automatic package restore feature is disabled, as per http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore.
If automatic package restore is disabled, then any package that installs a project target files will cause your build to fail until you manually restore that package in your solution, as described by http://blogs.msdn.com/b/dotnet/archive/2013/06/12/nuget-package-restore-issues.aspx. (Note that the workarounds described in the link are out-dated now that nuget 2.7 automatic package restore is available.)
So, if both these things are true, then delete the NuGet.targets file in the .nuget folder, and Nuget will then restore the missing package before invoking MSBuild. You can delete nuget.exe in the .nuget folder as well, as it will no longer be used.
The answers are very helpful for me to find the solution. Since the resolution to my specific issue requires one more step, I report it here in case it is helpful to others.
The error I was getting:
Error 117 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 ..\..\Windows Phone 8\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets.
I followed the answers and deleted all the suggested things, but the error still kept showing up. I finally got rid of it by deleting the following line in the .csproj:
<Error Condition="!Exists(...
Delete NuGet
Remove with Notepad .targets that depending NuGet from project file
Install latest version of NuGet
Restart Visual Studio and reopen project/solution.
If you want you can add new .nuget files with latest version of NuGet
Open csproj file in notepad and remove Error Condition elements from here (or make these conditions work):
<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'))" />
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.101.0\build\net45\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.101.0\build\net45\System.Data.SQLite.Core.targets'))" />
</Target>
Remove the .nuget directory from the solution if it exists. Edit the project file and remove the elements Import and Target that contain references to the .nuget folder, Save and reload the project.
try this,
List item
right click on the solution
click manage NuGet
click setting on the left bottom corner
check the Allow NuGet to download missing package.
Simply Right Click on Solution and "Enable NuGet Package Restore" solve my problem.

Resources