How-To's

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!

image description