10101010101001

0101010101001

Archive for January 2013

Unknown build error, ‘An item with the same key has already been added.’

with 2 comments

I got this error today when trying to build a C# solution today.
In my case the error was caused by the fact that I had the same file added twice in the .csproj file. (This happened after a merge in the *.csproj file).
To find the exact file would have been very hard since there can be thousands of files in a *.csproj.
The first thing to do is to find the exact project which is causing the issue. Visual studio is using msbuild in the background to do the builds, but it’s outputing very little information from msbuild.
This can be controlled by going to Tools->Options->Projects and Solutions->Build and Run. Set MSBuild project output verbosity to Diagnostic.
Now do a build again. After it fails display the output window. Debug->Windows->Output. Select “Show output from” -> Build. Find the first “Build failed” text.
Now you know which project’s build failed.
Go to Build->Clean, then right click on the specific project that is causing the problem and select build. (Do not build entire solution).
If you do that instead of building the entire solution, you will sometimes get an error with the exact file which is causing the build problem.
If it still doesn’t show you the file, you will have to go through the csproj and find out which line is duplicate.
Now unload that project (right click -> unload). Right click the unloaded project -> edit. Now search for that file which is causing the build problem and ensure that it is only added once in the csproj.
E.g. if the file is called EquipmentDetailsTitleBarPopup.xaml, look for duplicates of

    <Page Include="Features\Equipments\EquipmentDetailsTitleBarPopup.xaml">

and remove one of them.
Now reload the project (right click -> reload) and build again.

This fixed it for me.

Advertisements

Written by Liviu Trifoi

January 18, 2013 at 1:35 pm

Posted in .NET, C#, Visual Studio 2010