How to configure MSBuild to locate the F# build targets

Oct 4, 2013

Problem

When the AutoFixture.AutoFoq build run on the CodeBetter CI server for the first time, the following error message appeared:

MSB4057: The target “Build” does not exist in the project.

That was strange since the build run successfully in a development workstation with VS 2012 and .NET 4.5 installed.

Solution (CI Server)

By moving the project to a different agent, with .NET 4.5 installed, the problem was solved on the CodeBetter CI server.


However, the same issue was reported again - this time in a development workstation with VS 2013 RC.

Solution (Workstation)

Adam Chester has provided a solution which seems to work pretty well for VS 2012, VS 2013, and the CodeBetter CI server.

The solution can be applied to all F# projects using the MSBuild platform.

The most important changes are shown below. You can also see the diff as an image here:

Image


A note for VS 2012 users

While VS 2013 RC applies these changes automatically for F# projects, in VS 2012 the project file must be tweaked manually:

###Step 1 - Append:

<TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>

###Step 2 - Find and Replace:

<Import Project="$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')" />

With:

<Choose>
  <When Condition="'$(VisualStudioVersion)' == '11.0'">
    <PropertyGroup>
      <FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
    </PropertyGroup>
  </When>
  <Otherwise>
    <PropertyGroup>
      <FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
    </PropertyGroup>
  </Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" Condition="Exists('$(FSharpTargetsPath)')" />

###Step 3 - Find and Replace:

<Reference Include='FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'>

With:

<Reference Include='FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'>

The F# build targets, compiler, and run time should be located successfully now.

How to enable Code Analysis for F# projects

Sep 8, 2013

As of today, in Visual Studio 2012 the Run Code Analysis command is not available for F# projects.

Code Analysis is a Visual Studio feature for analyzing managed code assemblies and gathering information such as possible design, localization, performance, and security improvements.

Code Analysis can be used as a stand-alone tool with the FxCopCmd command-line application and it is also possible to be included to automated build processes such is MSBuild.

Enable Code Analysis in MSBuild

To enable Code Analysis in MSBuild, include the following elements in the project file:

<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.fs</CodeAnalysisModuleSuppressionsFile>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>My.ruleset</CodeAnalysisRuleSet>    

Problem

Unfortunately, even with the above elements, Code Analysis will not run for F# projects during the MSBuild process.

Solution

Edit the Microsoft.CodeAnalysis.Targets file which is located at:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\CodeAnalysis

Append or '$(Language)'=='F#' at the following locations:

<DefineConstants Condition="'$(Language)'=='C#' ">CODE_ANALYSIS;$(DefineConstants)</DefineConstants>
<PropertyGroup Condition="'$(Language)'=='C#' or '$(Language)'=='VB'">

You may also download the modified Microsoft.CodeAnalysis.Targets file from here.

The GitHub way of blog discussion and commenting

Apr 28, 2013

Using the underlying power of GitHub, blog authors often take a step further and roll their own discussion and commeting systems.

The best examples are:

With the evolution of blog-aware static site generators, like Jekyll, GitHub is becoming a central part for hosting both blog content and comments.

Hackers, Developers, Programmers

Apr 18, 2013

The definitions about Hackers, Developers, and Programmers, that I like best are:

"Hackers are generally loners who don't care if others can figure out their code (at least while they are in the mode or role of hacking). Thus, they build their own little world in it that fits themselves nicely so that they can hack fast, but the rest of the world be damned."

- c2.com/wiki

"On the 'programmer' vs. 'developer' debate: 'Programmer' focuses on the 'craft'. 'Developer' focuses on the business. Both are valuable."

- @ploeh on twitter

I am a programmer.

XCOPY deployment for Code Contracts

Mar 4, 2013

If you don’t wish to install Code Contracts through the Windows Installer:

  • Go to \Program Files (x86)\MSBuild\(version)\Microsoft.Common.Targets\ImportAfter folder.
  • Create a new file with name CodeContractsAfter.targets and paste the following XML:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <CodeContractsInstallDir Condition="'$(CodeContractsInstallDir)'==''">CONTRACTS_PATH</CodeContractsInstallDir>
  </PropertyGroup>
  <Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" 
          Project="$(CodeContractsInstallDir)MsBuild\v4.0\Microsoft.CodeContracts.targets" />
</Project>
  • Extract the contents from the Code Contracts Windows Installer using the lessmsi tool.
  • Locate the Contracts folder in the extracted contents.
  • Replace the CONTRACTS_PATH in CodeContractsAfter.targets with the actual Contracts folder location.

subscribe via RSS