Add Packages, not References - a NuGet plugin for ReSharper
November 20th, 2012 by Matt EllisGenerally speaking, ReSharper and NuGet get along just fine, happily ignoring each other. NuGet automates downloading, extracting and referencing third party assemblies. ReSharper simply uses those assemblies, without needing to know how they were referenced. The assemblies’ metadata is available for use in IntelliSense, importing namespaces, type navigation, etc.
Unfortunately, there is an overlap where this blissful ignorance can cause problems.
ReSharper has a quick-fix that appears when you use a type that isn’t referenced in the current project. If that type lives in an assembly referenced in another project, ReSharper will offer to add a new reference to this assembly, but it doesn’t use NuGet to do this. Fortunately, we have just released a plugin that will add support for this.
To demonstrate, imagine you have a solution with a couple of projects, one of which is a test project. You want to use xUnit.net, and so you add the “xUnit.net” and “xUnit.net: extensions” packages with NuGet. This adds xunit.dll and xunit.extensions.dll as references. The files live in the packages\xunit.1.9.1\lib\net20 and packages\xunit.extensions.1.9.1\lib\net20 folders.
Later, you add a new project. This is also going to be a test project, and you’re so excited about the code that you start typing before you add any references:

Since you haven’t referenced xunit.dll, ReSharper marks the use of FactAttribute as an error. If you position the cursor on the [Fact] statement, you get a quick-fix (as shown by the red light bulb icon) offering you the option to “Reference ‘xunit’ and use ‘Xunit.FactAttribute’”:

If you select this, ReSharper will add a direct reference to packages\xunit.1.9.1\lib\net20\xunit.dll, and will not use NuGet to do so. This means NuGet’s metadata (packages.config) is not updated, and NuGet doesn’t get a chance to run any install scripts.
ReSharper shouldn’t bypass NuGet like this, and this is an outstanding and well voted issue on our YouTrack instance.
Today, we’re releasing a plugin that adds NuGet support for this situation. Once you’ve installed the plugin, you won’t see anything different – the same menu is displayed – but when you select the “Reference ‘xunit’ and use ‘Xunit.FactAttribute’” option, it invokes NuGet, which adds the references, runs any scripts and creates/updates packages.config.
And since NuGet is doing the installing, it will also install any dependencies. If you repeat the same scenario with xUnit’s TheoryAttribute (which provides parameterised testing), NuGet installs the xunit.extensions package, which in turn causes the xunit package to be installed. Previously, ReSharper would just have added a reference to xunit.extensions.dll, and then offered another quick-fix to add the reference to the required xunit.dll.
It should be noted that neither ReSharper nor NuGet are downloading any packages here. ReSharper is only offering to import types that it knows about from references in other projects, which means the packages are already downloaded and referenced elsewhere in the solution. This plugin just ensures that the normal NuGet installation process is followed.
The plugin is Open Source, and is available on GitHub. You can download a pre-built version: simply extract and run the appropriate batch file for your version of ReSharper. It supports 7.1 and 6.1.1 (if you’re still on 7.0, there’s no excuse: get the free upgrade to 7.1 now!). So download it, use it, report issues and feature requests, or fork it and contribute!

November 20th, 2012 at 4:05 pm
Whoa! This is great!
November 20th, 2012 at 4:55 pm
Installed. Thanks!
November 20th, 2012 at 7:07 pm
Exactly what R# was missing.
November 21st, 2012 at 1:46 am
I had ‘fun’ while moving a 110 project .sln to nuget. Half the projects (*.Tests) are using nunit. Guess what I was wishing for in R# the whole time?
November 21st, 2012 at 1:51 am
@Rik Absolutely no idea. What was it? )
November 21st, 2012 at 1:22 pm
On my machine, the environment variable LOCALAPPDATA was not set. This should point to:
C:\Documents and Settings\{$username}\Local Settings\Application Data
The plugin was installed to the root of the c:\. It’s worth verifying that the plugin appears in Resharper “options” within the “Plugins” section.
November 21st, 2012 at 1:49 pm
@J Breen - what version of Windows are you running? Is it Windows XP? Looking at this page (http://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx) it suggests that the environment variable is set for Vista and later. If you can let me, I’ll update the batch files.
November 28th, 2012 at 5:27 pm
This is great. Will ReSharper also use NuGet to uninstall packages when the optimising references features are used (http://www.jetbrains.com/resharper/webhelp/Navigation_and_Search__Finding_Usages__Optimizing_References.html).
This is the reverse of the case mentioned in the post and is also required to ensure the NuGet packages.config and installed packages remains in sync with the project references.
Thanks!
November 28th, 2012 at 7:21 pm
It’s on the plan (https://github.com/JetBrains/resharper-nuget/issues/3) but I haven’t got to it yet. Pull requests welcome!
November 28th, 2012 at 9:38 pm
Thanks Matt.
December 11th, 2012 at 6:41 pm
+1 billion for David Grant’s suggestion. This is one of my biggest pain points with ReSharper. The use of NuGet effectively renders the Remove Unused References feature useless.
January 8th, 2013 at 11:04 pm
Please make the plugin part of next version of Resharper
January 9th, 2013 at 12:01 pm
I can not get this to work with Visual Studio 2012 (ReSharper 7.1.1).
In Help > About I have Installed plugins: NuGet support for ReSharper.
Still, if I type a class name (from a NuGet package installed in another project) and use ReSharper to add references, a direct reference to the .dll is added without updating NuGet (packages.config)
January 9th, 2013 at 2:27 pm
Hi Oskar. Is there any chance you can send me the solution, or a link to download it, please? You can send it to matt.ellis at jetbrains.com.
January 11th, 2013 at 12:26 pm
I am sorry but the solution is a part of a business product which I can not publish. I can try to reproduce the same behaviour in a new solution using public NuGet packages.
January 14th, 2013 at 1:06 pm
Thanks Oskar. If you can recreate it, please let us know - starting an issue on GitHub would be brilliant.
January 15th, 2013 at 9:44 am
Using this addon, causes “VSTS for DB doesn’t load correctly” error message at startup of Vs2010.
January 15th, 2013 at 1:13 pm
Hi André. We’re aware of this issue, but we’re having trouble recreating. Could you add any more details to this issue, please: https://github.com/JetBrains/resharper-nuget/issues/8
Thanks
Matt
January 21st, 2013 at 9:56 pm
Matt: You’ve got mail, I reproduced the issue I mentioned above. Tried to look into the GitHub issue tracker last time I went there but was unsure of the right details for the submission
January 22nd, 2013 at 11:54 am
Thanks Oskar. As I said in my email, there is a nasty bug in the 1.0 plugin when NuGet 2.2 is installed which causes packages not to get referenced, and either a file reference to be made, or no reference at all.
This is fixed in the GitHub source, and I’m hoping to get a binary release out very soon.
January 24th, 2013 at 4:18 pm
There’s a new version available (1.1), that fixes the incompatibility with NuGet 2.2, and fixes the crashes with VSTS DB Pro:
https://github.com/JetBrains/resharper-nuget/wiki/Release-Notes