Recreate, update, fix, generate packages.config from references

Jul 7, 2011 at 2:28 PM

I failed to find any discussion on this topic, so with this subject field packed with all the keywords I could think of I'd like to ask about an issue that I suppose many ReSharper users are facing with NuGet. When working on a multi-project solution, if you add a package reference to one project and then try to use it in a different one, ReSharper will happily suggest to add a reference to the needed library from ..\packages. Only thing is, that since it's not aware of NuGet, it will not create or update packages.config accordingly, which can lead to all sorts of problems. Especially when you don't check packages in, but instead rely on PreBuild and nuget.exe to download them.

Is there any supported way to automatically fix packages.config? Is there maybe a ReSharper plugin that could fix this? For now we've developed a simple console app to scan csprojs and fix packages.configs, perhaps it would be useful to contribute it as a command to nuget.exe?

What do you guys think?

Jul 7, 2011 at 3:39 PM

Hmmm, I don't believe this R# behavior has been discussed before, but I can see how it would cause pain. Note that in the general case, fixing up packages.config is not enough, because installing the package via NuGet may involve other things in addition to the assembly reference (static files, config transform, ...).

Ideally, some smart would be added to turn this R# behavior into a full NuGet call to install the package.

Jul 7, 2011 at 4:47 PM

Sure, except sometimes you actually don't want the additional stuff being installed. Imagine you have a class library that depends on something weby like elmah - that perhaps jsut adds some error handlers, configuration helpers or whatever, but you don't want to run web.config transforms as they make no sense. It's just about referencing the dll to compile, although you still need packages.config to force the project to download its dependencies, even when it gets built before the actual web projects that uses elmah fully. Am I making sense?

Anyway it's much easier to accidentally add a direct reference with R# than with vanilla VS and the usual "Add reference" dialog. Some solution would be great!

Jul 7, 2011 at 5:21 PM

Theoretically, there could be a feature that analyzes a project's dependencies and turn them into NuGet package references if the referenced assemblies are found to live inside the packages folder. In fact, it should be possible to write a little PowerShell script that does this by using the DTE to enumerate references. It may be a good first step to try that as a proof of concept (which could be made available as a Tools package).

You may also want to open an issue to track to idea of having something more built in to NuGet. It could be a new command like 'Install-Packages -infer' :)

Jul 7, 2011 at 5:27 PM

I should add that technically, the fact that you only want the assembly reference and not other parts of the package is mostly orthogonal to the main issue of having the assembly accidentally be referenced via R#. Note that if you add Elmah to a class library project, it won't do any config transforms, so it still does the right thing.

So if this new feature detects a missing NuGet package reference, it should run the full NuGet package install and not just fix up the packages.config. This way, it will apply whatever part of the package makes sense to the project. e.g. if you used the R# feature on a Web Project, you would want the transforms during the 'fixup'.

Jul 7, 2011 at 6:47 PM

You are totally right, I was writing from the top of my head, didn't know a package install differentiates between project types. Still not sure if this should be a PS script, core command or R# plugin, but I created an issue and copied your comments here: http://nuget.codeplex.com/workitem/1265

Jul 7, 2011 at 6:50 PM

Great, thanks!