John Mills
John Mills

Reputation: 10263

Code Analysis tool errors with "The given key was not present in the dictionary."

I get the following unhelpful error while building my .NET project when Code Analysis runs. It happens both in Visual Studio and building from the command line with MSBuild.

  * 1 total analysis engine exceptions.
MSBUILD : error : CA0001 : The given key was not present in the dictionary.

Any ideas on what is broken?


Edit:

Have found the code that causes it. I have a method in my code.

Public Function Generate(ByVal input As XDocument) As XDocument
   ' My code
End Function

If I add the following line as the first line of code the error starts happening, if I remove it, the error stops.

Contract.Requires(Of ArgumentNullException)(partCover IsNot Nothing, "input")

It doesn't really make sense as I'm using Code Contracts all through out my project. The only thing different about this method from any other is that it has about 200 lines of XML literals in it. The method transforms the input document to another XML format much like an XSLT. My guess is that it has something to do with that.

Upvotes: 4

Views: 1840

Answers (2)

cement
cement

Reputation: 2905

I had a similar problem:

<Exceptions>
  <Exception Keyword="CA0001" Kind="Engine">
   <Type>System.Collections.Generic.KeyNotFoundException</Type>
   <ExceptionMessage>The given key was not present in the dictionary.</ExceptionMessage>
   <StackTrace>   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.FxCop.Engines.Phoenix.PreScanPass.AnonymousMethodQueue.ReportDeclaringMethodFound(FunctionSymbol anonymousMethod)
   at Microsoft.FxCop.Engines.Phoenix.PreScanPass.AnonymousMethodPhase.ExamineInstructionForAnonymousMethodDeclaration(Instruction instruction)
   at Microsoft.FxCop.Engines.Phoenix.PreScanPass.AnonymousMethodPhase.Execute(Unit unit)
   at Phx.Phases.PhaseList.DoPhaseList(Unit unit)
   at Microsoft.FxCop.Engines.Phoenix.PreScanPass.Execute(ModuleUnit moduleUnit)
   at Phx.Passes.PassList.DoPassList(ModuleUnit moduleUnit)
   at Microsoft.FxCop.Engines.Phoenix.PhoenixAnalysisEngine.AnalyzeInternal()
   at Microsoft.FxCop.Engines.Phoenix.PhoenixAnalysisEngine.Analyze()
   at Microsoft.FxCop.Common.EngineManager.Analyze(Project project, Boolean verboseOutput)    </StackTrace>
</Exception>

I did some investigation and found that problem appears only if run FxCop from Visual Studio 2010. Since it works fine in VS2012 most likely the bug in FxCop has been fixed.

After commenting out my changes method-by-method I figured that it was caused by 2 anonymous methods (works fine of there is only one) in same class. Once I converted them to "named" methods error disappeared.

Hope that helps.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941545

This is an internal error in the tool. There's isn't much you can do about it, although it sounds like you found a temporary workaround. It is possible that it resolves itself when you keep working on the source code. Albeit that would require a glass that's half full.

You can report the bug at connect.microsoft.com, they'll need a small sample of your code that can reproduce the bug for them.

Upvotes: 4

Related Questions