Build/Test Issues

An item with the same key has already been added

Started by jschreuder on 6,254 views

Hi Remco.

I've seen this issue posted before (example) but it is happening for me now with v2.16.0.13 in VS2013.

One of my projects fails to build with:

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at _Mono.Cecil.MetadataReader.InitializeRanges(Table table, Func`1 get_next)
   at _Mono.Cecil.MetadataReader.InitializeCustomAttributes()
   at _Mono.Cecil.MetadataReader.ReadCustomAttributes(ICustomAttributeProvider owner)
   at _Mono.Cecil.Mixin.<GetCustomAttributes>b__8(ICustomAttributeProvider provider, MetadataReader reader)
   at _Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
   at _Mono.Cecil.Mixin.GetCustomAttributes(ICustomAttributeProvider self, ModuleDefinition module)
   at _Mono.Cecil.AssemblyDefinition.get_CustomAttributes()
   at nCrunch.Compiler.StaticManipulation.BuiltAssembly.()
   at nCrunch.Compiler.StaticManipulation.BuiltAssembly..ctor(FilePath builtAssemblyFilePath, IList`1 adjustedAssemblyReferences, IDictionary`2 codeFilePathsByWorkspaceFilePath, Boolean instrumentForPerformanceMeasurement, Boolean detectStackOverflow)
   at nCrunch.Compiler.RemoteBuildRunner.(ComponentBuildParameters , FilePath , BuildOutput , DirectoryPath[] , FilePath[] )
   at nCrunch.Compiler.RemoteBuildRunner.Build(ComponentBuildParameters parameters)


The weird thing is if I edit this project, it seems to be related to a set of partial classes:

<Compile Include="Module.cs" />
<Compile Include="Module.A.cs">
  <DependentUpon>Module.cs</DependentUpon>
</Compile>
<Compile Include="Module.B.cs">
  <DependentUpon>Module.cs</DependentUpon>
</Compile>


When I comment out either Module.A.cs part or Module.B.cs part, and disable the related code my project builds correctly.
It doesn't appear to be because of the DependentUpon, as if I remove these lines, it still won't build.

Unfortunately my attempts to reproduce this with a new solution with similar structure failed for some reason, so I can't send you a sample solution.

I've submitted a set of logs (one where I've commented out a module and one where it is broken) for you to take a look at via the Contact form.

I hope you can provide a fix because NCrunch is awesome :)
Hi, thanks for sharing this issue.

Looking at the stack trace, this problem appears to be caused by some kind of metadata corruption or new IL arrangement that NCrunch/cecil is unfamiliar with. Unfortunately the only way for me to troubleshoot this problem is to have a code sample that can reliably reproduce it.

I'm not entirely convinced that the problem is being caused by the syntax you've described in the project file. Most likely it's related to code contained within these files that is causing the corruption or IL arrangement to surface. Check whether the files contain any interesting/complex snippets of code.. such as attributes containing nullable parameters or very rarely used features of the CLR.

If you are using any post-build assembly manipulation tools (such as PostSharp or Frody), try disabling these to see if there is any difference.
Remco wrote:Most likely it's related to code contained within these files that is causing the corruption or IL arrangement to surface. Check whether the files contain any interesting/complex snippets of code.. such as attributes containing nullable parameters or very rarely used features of the CLR.


I don't think I'm using any weird stuff.

Remco wrote:If you are using any post-build assembly manipulation tools (such as PostSharp or Frody), try disabling these to see if there is any difference.


I'm not using any of these types of tools.

Edited

Some more interesting clues that I hope will help with isolating the issue:

- The issue seems to be caused by an assembly-level attribute
- The issue is caused by having more than one range within the assembly with the same metadata token. This is a little beyond my current understanding of the CLR specification, but it does look to be a very wrong thing.

I realise sharing any of your source code with me isn't an option. What about compiled assemblies?
Thanks for sharing the binaries with me. I've managed to reproduce and fix the issue. You're welcome to give the fix a try if you like:

http://downloads.ncrunch.net/NCrunch_Console_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_Console_2.17.0.8.zip
http://downloads.ncrunch.net/NCrunch_GridNodeServer_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_GridNodeServer_2.17.0.8.zip
http://downloads.ncrunch.net/NCrunch_LicenseServer_2.17.0.8.zip
http://downloads.ncrunch.net/NCrunch_VS2008_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_VS2010_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_VS2010_2.17.0.8.zip
http://downloads.ncrunch.net/NCrunch_VS2012_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_VS2012_2.17.0.8.zip
http://downloads.ncrunch.net/NCrunch_VS2013_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_VS2013_2.17.0.8.zip
http://downloads.ncrunch.net/NCrunch_VS2015_2.17.0.8.msi
http://downloads.ncrunch.net/NCrunch_VS2015_2.17.0.8.zip
Remco wrote:Thanks for sharing the binaries with me. I've managed to reproduce and fix the issue. You're welcome to give the fix a try if you like:


Thanks for sticking with me to help resolve this :)

I've installed the new build and am getting a new error on the same project now:


System.NullReferenceException: Object reference not set to an instance of an object.
at nCrunch.Compiler.StaticManipulation.BuiltAssembly.(CustomAttribute )
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at nCrunch.Compiler.StaticManipulation.BuiltAssembly.()
at nCrunch.Compiler.StaticManipulation.BuiltAssembly..ctor(FilePath builtAssemblyFilePath, IList`1 adjustedAssemblyReferences, IDictionary`2 codeFilePathsByWorkspaceFilePath, Boolean instrumentForPerformanceMeasurement, Boolean detectStackOverflow)
at nCrunch.Compiler.RemoteBuildRunner.(ComponentBuildParameters , FilePath , BuildOutput , DirectoryPath[] , FilePath[] )
at nCrunch.Compiler.RemoteBuildRunner.Build(ComponentBuildParameters parameters)


I've submitted a new bug report via the Submit Bug Report menu item. Hope it helps!

Edited

Sorry. Here is attempt number 2. This is a blind pitch - so I really hope it works ...

http://downloads.ncrunch.net/NCrunch_Console_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_Console_2.17.0.9.zip
http://downloads.ncrunch.net/NCrunch_GridNodeServer_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_GridNodeServer_2.17.0.9.zip
http://downloads.ncrunch.net/NCrunch_LicenseServer_2.17.0.9.zip
http://downloads.ncrunch.net/NCrunch_VS2008_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_VS2010_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_VS2010_2.17.0.9.zip
http://downloads.ncrunch.net/NCrunch_VS2012_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_VS2012_2.17.0.9.zip
http://downloads.ncrunch.net/NCrunch_VS2013_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_VS2013_2.17.0.9.zip
http://downloads.ncrunch.net/NCrunch_VS2015_2.17.0.9.msi
http://downloads.ncrunch.net/NCrunch_VS2015_2.17.0.9.zip
Thanks - unfortunately still not a go, back to a similar error to the first one, but no longer around ranges but nested types:


System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at _Mono.Cecil.MetadataReader.InitializeNestedTypes()
   at _Mono.Cecil.MetadataReader.InitializeTypeDefinitions()
   at _Mono.Cecil.MetadataReader.ReadTypes()
   at _Mono.Cecil.ModuleDefinition.<get_Types>b__8(ModuleDefinition _, MetadataReader reader)
   at _Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
   at _Mono.Cecil.ModuleDefinition.get_Types()
   at  .()
   at nCrunch.Compiler.StaticManipulation.BuiltAssembly.Instrument(ComponentSourceLineMap sourceLineMap, IDictionary`2 instrumentationDirectivesByCodeFileId, IDictionary`2 codeFileIDsByFilePath, Boolean proxyProcessIsActive, Int32 componentId, IInstrumentationFilter[] instrumentationFilters)
   at nCrunch.Compiler.RemoteBuildRunner.(ComponentBuildParameters , FilePath , BuildOutput , DirectoryPath[] , FilePath[] )
   at nCrunch.Compiler.RemoteBuildRunner.Build(ComponentBuildParameters parameters)


I've sent in a new bug report. Thanks for being so patient with trying to figure this out.

Post a reply

Log in to reply.