Monday, 1 August 2016

Error AD0001: Compiler Analyzer … threw an exception of type 'System.InvalidOperationException' with message 'Feature 'IOperation' is disabled.'

If you upgrade to the latest beta release of one of the Roslyn compiler analyzer packages, you might notice they fail with an error like this:
Compiler Analyzer 'Microsoft.ApiDesignGuidelines.Analyzers.UsePropertiesWhereAppropriateAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Feature 'IOperation' is disabled.'.
The solution wasn’t immediately obvious to me, but I eventually tracked down this comment on a Github issue. According to the comment, the analyzers are using an API that needs to be enabled through configuration. To do this, you need open up the .csproj file and add a new property as a child of the first PropertyGroup element like this:

<PropertyGroup>  
  <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>  
  <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>  
  <Features>IOperation</Features> 

Or if you have lots of projects, run this PowerShell script to update them all:

Get-ChildItem *.csproj -Recurse | ForEach-Object {   
    $content = [xml] (Get-Content $_) 
    $xmlNameSpace = new-object System.Xml.XmlNamespaceManager($content.NameTable)   
    $xmlNameSpace.AddNamespace("p", "http://schemas.microsoft.com/developer/msbuild/2003")
    if (-not $content.Project.PropertyGroup[0].Features) {    
        Write-Host "Features missing in $_"
        $featureElt = $content.CreateElement("Features", "http://schemas.microsoft.com/developer/msbuild/2003")    
        $featureElt.set_InnerText("IOperation")

        $content.Project.PropertyGroup[0].AppendChild($featureElt)
    }  
    $content.Save($_)

    # Normalise line endings    
    (Get-Content $_ -Encoding UTF8) | Set-Content $_ -Encoding UTF8
}

No comments: