Add Packages, not References – a NuGet plugin for ReSharper

Generally 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 packagesxunit.1.9.1libnet20 and packagesxunit.extensions.1.9.1libnet20 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’”:

ReSharper quick-fix

If you select this, ReSharper will add a direct reference to packagesxunit.1.9.1libnet20xunit.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!

This entry was posted in ReSharper Tips&Tricks and tagged , , . Bookmark the permalink.

22 Responses to Add Packages, not References – a NuGet plugin for ReSharper

  1. Mark Schaller says:

    Whoa! This is great!

  2. Allon Guralnek says:

    Installed. Thanks!

  3. Byron Sommardahl says:

    Exactly what R# was missing.

  4. Rik Hemsley says:

    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?

  5. @Rik Absolutely no idea. What was it? )

  6. J Breen says:

    On my machine, the environment variable LOCALAPPDATA was not set. This should point to:

    C:Documents and Settings{$username}Local SettingsApplication 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.

  7. Matt Ellis says:

    @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.

  8. David Grant says:

    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!

  9. Matt Ellis says:

    It’s on the plan (https://github.com/JetBrains/resharper-nuget/issues/3) but I haven’t got to it yet. Pull requests welcome!

  10. David Grant says:

    Thanks Matt.

  11. Nathan Alden, Sr. says:

    +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.

  12. Alex says:

    Please make the plugin part of next version of Resharper

  13. Oskar says:

    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)

  14. Matt Ellis says:

    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.

  15. Oskar says:

    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.

  16. Matt Ellis says:

    Thanks Oskar. If you can recreate it, please let us know – starting an issue on GitHub would be brilliant.

  17. André says:

    Using this addon, causes “VSTS for DB doesn’t load correctly” error message at startup of Vs2010.

  18. Matt Ellis says:

    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

  19. Oskar says:

    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 :)

  20. Matt Ellis says:

    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.

  21. Matt Ellis says:

    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

  22. Ali Jawad says:

    I wish I found this plug-in 8 months ago… its freakin’ awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>