Skip to content

VS/CSharp: Link tags always generated for C# sources inside the binary directory

This merge request fixed an issue, that caused generated C# source files, that are located in the project binary directory (for out-of-source builds) to be incorrectly added to the .csproj file via <Link> tags. The fixed (correct) behavior has since changed again and the fix does no longer work. For example, using configure_file to automatically generate the AssemblyInfo.cs file from a template, like this:

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/AssemblyInfo.cs.in" "Properties/AssemblyInfo.cs")

add_library(${PROJECT_NAME} SHARED "Properties/AssemblyInfo.cs")
csharp_set_designer_cs_properties("Properties/AssemblyInfo.cs")

generates the following include statement:

<Compile Include="CMAKE_CURRENT_BINARY_DIR\Properties\AssemblyInfo.cs">
  <Link>Properties\AssemblyInfo.cs</Link>
</Compile>

However a link to a file within the project directory is invalid and Visual Studio generates the following warning (and does not include nor compile the file):

'CMAKE_CURRENT_BINARY_DIR\Properties\AssemblyInfo.cs' could not be added to the project. Cannot add a link to the file 'CMAKE_CURRENT_BINARY_DIR\Properties\AssemblyInfo.cs'. This file is within the project directory tree.

Manually removing the link and only stating the relative path in the Include attribute fixes this problem (see this for more information).

As a workaround, I currently let configure_file create the AssemblyInfo.cs file outside of CMAKE_CURRENT_BINARY_DIR:

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/AssemblyInfo.cs.in" "${CMAKE_BINARY_DIR}/Config/${PROJECT_NAME}/AssemblyInfo.cs")

add_library(${PROJECT_NAME} SHARED "${CMAKE_BINARY_DIR}/Config/${PROJECT_NAME}/AssemblyInfo.cs")
csharp_set_designer_cs_properties("Properties/AssemblyInfo.cs")

This way the <Link> tag becomes valid again. However, this solution is not very intuitive and CMake should instead not create a link tag for sources within the project directory.

Edited by Brad King
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information