Fixing NuGet packages and references once and for all
Solution 1
New Projects
There is one single thing you need to do in order for packages to work correctly with git:
When you create a repository, make sure you add a .gitignore tailored for Visual Studio development. You can search google for such a file or take it from here.
This will make sure you don't commit anything that will cause problems later.
This should solve a lot of the problems that usually happen when you check in packages to a code repository. You can add/remove packages and upgrade packages and also be able to clone the repository to a new machine and packages will be restored automatically.
Existing Projects
This is good for new projects. If you already have a project with a great big mess, then it is very hard to fix it since Visual Studio keeps package versions in several places - packages.config and app.config (and web.config where relevant).
Option 1 - Fix the current project
You can remove all packages (remove all the references from all projects and delete the package contents).
Make sure that the packages no longer appear in any file (e.g. packages.config or app.config). Now add the .gitignore and then start adding the packages back.
Option 2 - Create a new project
If it doesn't work, start a new project, add the .gitignore, transfer all the code (only your code) and install the packages.
Solution 2
I'm not quite familiar with how Git works in VS, but I recall one time recently when using TFS that unbinding the solution from source control, fixing all packages, cleaning and re-building before binding it again solved a bunch of issues regarding NuGet references.
I apologize if Git works way different within VS.
Related videos on Youtube
vaindil
Updated on June 12, 2022Comments
-
vaindil almost 2 years
Windows 8.1 Enterprise x64, Visual Studio 2015, MVC 5, EF 6, VS Online using git
I'm a relatively new developer with Visual Studio (about nine months), and ever since I started I've had an incredibly difficult time with references and NuGet packages. All of my references were working correctly on Friday when I shut my computer down for the weekend. I didn't touch the computer at all the entire weekend, now I've booted up today and I have a ton of reference errors:
(There are 6,262 errors total but I obviously can't screenshot the entire thing.)
Like I said, this sort of problem occurs very often, and it's incredibly frustrating. Things I've tried:
- Ensure all references throwing the errors actually are referenced
- Un/reinstall the packages throwing the errors in NuGet
- Completely delete the contents of the
packages
folder and let NuGet restore them (all reinstalled, views have the same errors as before) - Ensure every single reference is set to
Copy Local
- Close the solution, delete all
bin
andobj
folders, reboot computer, clean, rebuild - Ensured all necessary references are in the relevant
web.config
(either insideViews
or at the root) - Check for NuGet option to restore missing packages (did not appear, all packages are in my local
packages
folder) - Other things on SO that I'm sure I've forgotten to list here
I'm at my wits' end with these packages and references. It's a different fix every time, and this time I can't figure it out. Am I missing some obvious fix, something I overlooked? Is there a way to somehow take a backup when this is working and restore it whenever things break? Any ideas whatsoever, whether helping with the current problem or for fixing the underlying problem, would be very much appreciated.
Additional note
The problem is NOT only with views--controllers are also throwing errors. Specifically:
The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?)
(It literally says
Controller
, that's not something I changed for privacy or whatnot.)These are thrown despite the fact that I have
using
statements for all of the necessary namespaces. Theusing
statements themselves work properly, but the error is thrown in the code. The automatic fix suggested is to manually reference everything: for example,var sb = new StringBuilder();
wants me to change the line tovar sb = new System.Text.StringBuilder();
. Testing that fix does not correct the problem, the same error is thrown but onSystem
instead ofStringBuilder
.Additional requested information
- I use git with VS Team Services (but packages are ignored with
.gitignore
). - I and one other person work on this, but the other person hasn't touched it at all (not even pulling from the remote repo) in several weeks.
- References in the
.csproj
file are in the format..\..\..\packages\
(correct for the location relative to the.csproj
file). - Targeting .NET 4.6 (always have been, that's not new)
- All references resolve, no exclamation marks in the references list.
-
CodeCaster over 8 yearsDo you use version control? What happened to the project file between it working and stopping to work? The more important issues are ironically warnings in this case, about unresolved references. Do multiple people work on this project? How exactly are the references specified in the
.csproj
XML? It must start with..\packages
, no more dots before (assuming your.sln
is one directory up from your.csproj
). -
vaindil over 8 years@CodeCaster I use git with VS Online, but packages are ignored with
.gitignore
. I and one other person work on this, but the other person hasn't touched it at all (not even pulling from the remote repo) in several weeks. References in.csproj
file are in the format you specified, yes...\..\..\packages` (correct for the location relative to the
.csproj` file). -
DavidG over 8 yearsDo all the references resolve? What version of .Net Framework are you targetting?
-
vaindil over 8 years@DavidG Yes, all references resolve. Targeting .NET 4.6.
-
Joe Phillips over 8 yearsIs package restore enabled in VS? "Automatically check for missing packages during build in Visual Studio"
-
vaindil over 8 years@JoePhilllips, Yes, I tried that in two separate steps in my list of things I tried.
-
CodeCaster over 8 yearsThe point is, as long as the binaries are in your
bin
folder, it'll compile. After a clean / rebuild, the reference DLLs will be removed from thebin
and looked up in theHintPath
(..\packages
) again. Then you'll get yellow exclamation mark icons on your references, compiler warnings (The reference ... could not be found). In 99% of the cases I've seen, it was a hintpath error. Double-check that your..\packages
actually resolves. -
vaindil over 8 years@CodeCaster I can 100% assure you that the references do resolve. No yellow exclamation marks are present, and I manually checked
HintPath
and walked through it to make sure they point to the correct location. I removed the references and re-added them, they're found. -
CodeCaster over 8 yearsOk. Couple more: does this happen for your other developer as well? Does it happen for all projects on your machine? Anyway don't let this harm your Visual Studio experience; what you encounter is not normal. It's especially weird that
System.Linq
cannot be resolved. -
vaindil over 8 years@CodeCaster It does not happen for the other developer--he deleted the entire repository, pulled it clean, and VS cleaned and rebuilt successfully. I'm going to attempt this in a moment; I'm repairing VS first. It does not happen with any other projects on my machine. (Visual Studio is good for the most part, but I absolutely cannot stand the way NuGet and references work because of these common issues. Seems to cause more problems than it's worth, though I have no idea what would work better.)
-
vaindil over 8 years@CodeCaster Repairing VS then deleting/re-cloning the repo did not fix the problem. I verified that the repo does not contain a
packages
folder, and didn't create one when cloned. Used NuGet to restore missing packages, all of them resolve again but the issue in controllers/views remains. -
John Koerner over 8 yearsCould it be a problem with your nuget cache on your machine? Try renaming the folder
%userprofile%\.nuget
-
Preet Singh over 8 yearsThe way nuget restore packages is by looking at your /Packages/repositories.config file and pakages.config file in very project directory in your solution. because of this reason these files need to be checked in into your source control.For example my repo...config looks like
<?xml version="1.0" encoding="utf-8"?> <repositories> <repository path="..\DataAccess.InMemory\packages.config" /> ...
and packages file<?xml version="1.0" encoding="utf-8"?> <packages> <package id="FluentValidationNA" version="1.2.16" targetFramework="net452" />
-
Ruskin over 8 yearsHave you got Resharper 2015.2 installed? I had the exact same problem where built in .net types weren't getting recognised, e.g. String had a red suigly under it. Also, does this happen with new projects you create or just this one? Can you share your project?
-
Neil T over 8 yearsCould you please screenshot all the compiler warnings (not errors) you're getting in Visual Studio?
-
CodeCaster over 8 years"From now on, you won't have any problems with packages" - this is wrong, plenty of issues are possible when your only action is adding the proper entries to
.gitignore
. Package versions also aren't kept in app/web.config. -
daramasala over 8 years@CodeCaster of course there can always be problems. Judging from the OP's description, I think it will solve his problems. Also package versions are referenced in web.config and if they are not synced with the versions in other files you get trouble.
-
CodeCaster over 8 yearsTo me it looks like you posted this as "The definitive shotgun approach to fix all NuGet problems", which it isn't. Also, show me where in the web.config package versions are referenced.