title: ANTLR MSBuild error: Duplicate items are not supported by the "Sources" parameter
body: <p>I'm posting this partly because I want to learn more about MSBuild and partly in the hope that other's don't spend as much time as I did find the solution (google searches of the error text were not helpful in my case).</p>
  
  <p>I recently upgraded to Visual Studio 2017. It would crash on occasion (and subsequently every time that particular .sln was loaded). My workaround was to launch the same .sln in Visual Studio 2015, compile (which would fail due to C# 7 features), after which Visual Studio 2017 would load the solution correctly.</p>
  
  <p>This eventually reached an unacceptable annoyance threshold. The event log contained Visual Studio 2017 crash entries with the following exception:</p>
  
  <p><b>Exception Info: System.IO.FileLoadException
     at Antlr4.Build.Tasks.AntlrClassGenerationTaskInternal.HandleOutputDataReceived(System.Object, System.Diagnostics.DataReceivedEventArgs)</b></p>
  
  <p>A google search revealed that an upgrade to a later version of ANTLR was required:
  <a href="https://github.com/tunnelvisionlabs/antlr4cs/issues/173" rel="nofollow noreferrer">https://github.com/tunnelvisionlabs/antlr4cs/issues/173</a></p>
  
  <p>After upgrading from 4.3.0 to 4.6.4, Visual Studio 2017 was no longer crashing but I received the following error on build:</p>
  
  <p><B>The item "(omitted)Lexer.cs" was specified more than once in the "Sources" parameter.  Duplicate items are not supported by the "Sources" parameter.</B></p>
  
  <p>The *.cs files generated by ANTLR were getting included (at least) twice in the build process. After learning many, many ways not to fix the problem I eventually fixed the problem by removing the following lines at the bottom of the Antlr4.CodeGenerator.targets file:</p>
  
  <pre><code>&lt;ItemGroup&gt;
    &lt;Compile Include="@(Antlr4GeneratedCodeFiles)" /&gt;
    &lt;!-- The WinFX "GenerateTemporaryTargetAssembly" target requires generated code files be added here. --&gt;
    &lt;_GeneratedCodeFiles Include="@(Antlr4GeneratedCodeFiles)" /&gt;
  &lt;/ItemGroup&gt;
  </code></pre>
  
  <p>Does anybody know what else was including those files in the build? (This is my question)</p>
  
  <p>(The project contains WPF User Controls and the intermediate "g.cs" etc. files  are also located in the IntermediateOutputDirectory and would have to be included in the build which is my best guess).</p>
  
  <p>If it is possible to conditionally include *.cs files in a build it might be worth modifying the default Antlr4.CodeGenerator.targets file to that effect.</p>