C# XAML files with the same name in different directories will cause the generated files to overwrite one another
When creating a CSharp executable using WPF I ran into this problem when porting over an existing project.
It seems that if we have 2 xaml files with the same name but under different folders and namespaces, when they get converted to cs code by the build they somehow resolve to the same file name at the root level of the obj dir.
E.g: From the directory structure below bot MainWindow implementations get resolved to objdir/$(Configuration)/MainWindow.g.cs
- Window1
\- MainWindow.xaml // Namespace Window1.MainWindow
- Window2
\- MainWindow.xaml // Namespace Window2.MainWindow
This is the error produced by the compiler:
2>------ Build started: Project: Example, Configuration: Debug x64 ------
2>Build started 7/26/2018 9:22:26 AM.
2>CustomCommand_Debug_3a67eba62519129ba43b29491fa0c58b:
2>Skipping target "CustomCommand_Debug_3a67eba62519129ba43b29491fa0c58b" because all output files are up-to-date with respect to the input files.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets(52,5): error MSB3105: The item "C:\Users\BeernaertL\Desktop\cmake_csharp_wpf_example-master-build\Example\obj\x64\Debug\MainWindow.g.cs" was specified more than once in the "Sources" parameter. Duplicate items are not supported by the "Sources" parameter.
2>
2>Build FAILED.
2>
2>"C:\Users\BeernaertL\Desktop\cmake_csharp_wpf_example-master-build\Example\Example.csproj" (default target) (1) ->
2>"C:\Users\BeernaertL\Desktop\cmake_csharp_wpf_example-master-build\Example\zranqmcx.tmp_proj" (_CompileTemporaryAssembly target) (3) ->
2>(CoreCompile target) ->
2> C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets(52,5): error MSB3105: The item "C:\Users\BeernaertL\Desktop\cmake_csharp_wpf_example-master-build\Example\obj\x64\Debug\MainWindow.g.cs" was specified more than once in the "Sources" parameter. Duplicate items are not supported by the "Sources" parameter.
2>
2> 0 Warning(s)
2> 1 Error(s)
2>
2>Time Elapsed 00:00:00.54
As far as I investigated, the solution files are the same compared to a native example minus all the link tags.
I'm no C# expert nor am very familiar with the windows toolkits. That being said, my leading theory is that it has something to do with the fact that we have out of source builds as opposed to in source builds which seems to be the default for C#. It could be that the MSBuild targets/compiler have issues resolving the paths? I got that clue from the following output from the diagnostic msbuild output:
Input file 'C:\Users\BeernaertL\Desktop\cmake_csharp_wpf_example-master-build\Example\Window1\MainWindow.xaml' is resolved to new relative path 'MainWindow.xaml' at directory 'C:\Users\BeernaertL\Desktop\cmake_csharp_wpf_example-master-build\Example\Window1\'. (TaskId:65)
I've attached a zip with a reproducible case. It's a modified version of https://github.com/bemehiser/cmake_csharp_wpf_example/, which illustrates the problem.
I hope this is enough info to go on.