Thursday, September 20, 2012

Adding NuGet Support to F# Interactive in VS2012

About a year ago, I posted an approach for adding basic NuGet support to VS2010. This post provides an updated script for adding this support in VS2012.

There may be times when you wish to use NuGet from the F# Interactive, such as when you are creating F# scripts. Since NuGet focuses on Visual Studio projects, this functionality is not supported out-of-the-box; however, it’s fairly simple to get things up and running.

The first step that we’ll need to do is create an F# script that is able to interact with the official NuGet package source. The script found here does the trick. I won’t spend too much time explaining the code in this script, but at a high level this allows you to use the F# Interactive window to talk to the NuGet package source, pull down packages, and automatically add a reference to the F# script file that has focus. 

The last step to accomplish our goal, is to tell F# Interactive to run our script each time that it launches. This is pretty easy to do thanks to the functionality provided by Visual Studio for adding F# Interactive options. To set the applicable option, go to Tools | Options | F# Tools | F# Interactive. You can then add the following information to the F# Interactive options setting.

--use:C:\temp\fsiext.fsx

 The F# Interactive options window looks like the following after making this change:


Once this is in place you can do something like this:

- Create a new F# Library project and build it. 
- Open the Script.fsx file.
- Navigate to the F# Interactive window.
- Execute InstallPackage “FSPowerPack.Core.Community”;; in the F# Interactive.

This will cause the package to be pulled down from the official NuGet repository and will automatically add the appropriate references to the Script.fsx file. 

Here's what the screen should look like after completing these steps.


Disclaimer: There are a few known issues with this approach.

7 comments:

  1. Very useful. Many thanks.

    ReplyDelete
  2. Does MvvM light v4 support VS2012 RC?

    ReplyDelete
  3. Hi. I keep getting this error even though I have NuGet 2.1 on both VS2010 and VS2012:
    The schema version of 'FSharpx.TypeProviders.Documents' is incompatible with version 1.6.30117.9648 of NuGet

    ReplyDelete
  4. I'll look into it. Does it work correctly with other packages such as the PowerPack?

    ReplyDelete
  5. No, it fails with everything. It seems the #r "NuGet.Core.dll" is picking up some old version somewhere on my machine

    ReplyDelete
  6. You can try changing that #r to a direct path to the desired DLL version. 

    ReplyDelete
  7. FWIW --- I had issues ... not sure if they were these issues, but using the tip:
     right click solution and "Enable NuGet package Restore" helped me.

    ReplyDelete