PCH combined with UNITY_BUILD generates error C1010 when compiling single file with Ctrl+F7 in Visual Studio since CMake 3.20
Since CMake 3.20, if you try to compile a single .c or .cpp file in Visual Studio with Ctrl+F7 in a target configured to use PCH and UNITY_BUILD both enabled, then you will see the next error:
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "[PATH_TO_PCH]/cmake_pch.hxx"' to your source?
In previous versions (since 3.17 to 3.19) this was working as expected, but after 3.20 some precompiled settings has been moved from file configurations to project configuration, making the setting for precompiled headers common for all source files in the project (both original source files and auto-generated unity build files). Prior to 3.20 this precompiled settings only affect to unity build auto-generated files.
When you don't use UNITY_BUILD, everything works because the force include rule applies to all the original files, but when you enable UNITY_BUILD, the force include for PCH only applies to auto-generated unity build files. One possible solution would be to apply the same force include rules to all files in the project. In fact this is one workaround I've tried for this problem, and seems to work, at least for me:
if(MSVC)
target_compile_options(${projectName} PRIVATE /FI$<$<COMPILE_LANGUAGE:C>:${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${projectName}.dir/$<CONFIG>/cmake_pch.h>)
target_compile_options(${projectName} PRIVATE /FI$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${projectName}.dir/$<CONFIG>/cmake_pch.hxx>)
endif()