Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a specific version of the F# compiler in MSBuild projects?

I have some F# project files generated using visual studio.

On my computer, which has several F# versions installed, it seems to pick the latest one.

However, I want to use a specific F# compiler - the one installed using version 3.1.2.

How do I do so?

like image 438
Arafangion Avatar asked May 08 '17 02:05

Arafangion


1 Answers

I'm using a similar setup, just that I'm consuming the F# compiler from its nuget package - this works nicer in shared build environments.

You will need a .props file to be included in all of your F# projects, I've called it fsharp_project.props. By changing that props file, you can update the compiler version for all of your F# projects. Its contents should be as follows:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PackageRoot>C:\packages\Fsharp.Compiler.Tools.Nuget</PackageRoot>
    <FscToolPath>$(PackageRoot)\tools</FscToolPath>
    <FSharpVersion>v3.0</FSharpVersion>
  </PropertyGroup>
  <ItemGroup>
  <Reference Include="FSharp.Core">
    <HintPath>$(PackageRoot)\tools\fsharp.core.dll</HintPath>
  </Reference>
  </ItemGroup>
</Project>

You need to adjust the path to your local version of the F# compiler, and also to the core libraries that you wish to use.

Then, modify your .fsproj file to consume that file as follows:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <Import Project="C:\whereever\fsharp_project.props"/>
...

Further down in your .fsproj file you will see a reference of FSharp.Core.dll. Remove that - in the props file, there is already a reference to the version of the core libraries that come with the compiler.

Re-load your project and re-build, it will print out the full path of the fsc.exe that it is using.

As a side note: The same trick with .props files is also incredibly helpful for referencing the right version of FSharp.Core.dll in C# consumers of your F# code - that's a frequent source of runtime errors. Include a props file that only references FSharp.Core.dll in each .csproj, and you will be able to switch all C# projects to a new version of the core libraries by just updating the `.props' file.

like image 169
Anton Schwaighofer Avatar answered Oct 23 '22 15:10

Anton Schwaighofer