Add Packages, not References – a NuGet plugin for ReSharper

Posted on by Matt Ellis

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!

Comments below can no longer be edited.

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

  1. Mark Schaller says:

    November 20, 2012

    Whoa! This is great!

  2. Allon Guralnek says:

    November 20, 2012

    Installed. Thanks!

  3. Byron Sommardahl says:

    November 20, 2012

    Exactly what R# was missing.

  4. Rik Hemsley says:

    November 21, 2012

    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. Jura Gorohovsky says:

    November 21, 2012

    @Rik Absolutely no idea. What was it? )

  6. J Breen says:

    November 21, 2012

    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:

    November 21, 2012

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

    November 28, 2012

    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:

    November 28, 2012

    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:

    November 28, 2012

    Thanks Matt.

  11. Nathan Alden, Sr. says:

    December 11, 2012

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

    January 8, 2013

    Please make the plugin part of next version of Resharper

  13. Oskar says:

    January 9, 2013

    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:

    January 9, 2013

    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:

    January 11, 2013

    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:

    January 14, 2013

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

  17. André says:

    January 15, 2013

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

  18. Matt Ellis says:

    January 15, 2013

    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:

    January 21, 2013

    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:

    January 22, 2013

    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:

    January 24, 2013

    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:

    August 28, 2013

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

  23. Eric Liprandi says:

    September 4, 2015

    Is this still needed/up-to-date? I have 9.1 and I don’t see in the list of extensions to be loaded.

    • Matt Ellis says:

      September 4, 2015

      This is no longer needed. As of ReSharper 9.1, the plugin’s functionality has been rolled into the main product.

  24. Shane says:

    September 13, 2017

    If this is a part of the application now, where would the option be to do this?

    • Matt Ellis says:

      September 13, 2017

      It works in exactly the same way – there’s no change to the UI. If ReSharper is offering to add a reference to resolve a type, it will check to see if the assembly it’s adding is part of a nuget package. If so, it will add a nuget reference rather than a simple assembly reference.

      • Rambalac says:

        June 6, 2018

        It’s not

      • Marco says:

        June 24, 2019

        Doesn’t work here either. I’m using VS 2017 with ReSharper Ultimate 2017.2.2. Any idea why ?

        • Matt Ellis says:

          June 25, 2019

          Please can you raise an issue at https://youtrack.jetbrains.com with some details on what’s happening – what type you’re trying to add, where the type should come from (which package) and what’s actually happening (which assembly is added instead). Thanks!

Subscribe

Subscribe to .NET Tools updates