Precompiled header builds fail on windows

Using Gradle 4.10.2 (or 5.5.1 - same problem with both), I’m trying to enable precompiled header support to an existing native project that was building successfully prior to this change. I use the /ZI compiler flag on all files compiled for debug build types.

I created a PCH file in the project’s headers directory, updated all the implementation files to include the PCH as the first include in the file, and added the preCompiledHeader property to the build script like so:

components {
    myProject(NativeLibrarySpec) {
        ...
        sources {
            ...
            cpp {
                preCompiledHeader "pch.h"
                ...
            }
        }
    }
}

The precompiled header task completes successfully, however, when the debug compile tasks start up, I get these errors for each file:

e:\dev\myProject\src\myLib\cpp\mySource.cpp(1): error C2859: E:\dev\myProject\build\objs\myLib\static\windows_x64\debug_static\myLibCpp\vc140.pdb is not the pdb file that was used when this precompiled header was created, recreate the precompiled header.
e:\dev\myProject\src\myLib\cpp\mySource.cpp(1): error C2859: E:\dev\myProject\build\objs\myLib\static\windows_x64\debug_static\myLibCpp\vc140.idb is not the idb file that was used when this precompiled header was created, recreate the precompiled header.

When I inspect the layout of the build folder, there are two sets of pdb and idb files, one copy in build/objs/myLib/static/windows_x64/debug_static/myLibCppPCH and another in build/objs/myLib/static/windows_x64/debug_static/myLibCpp.

Why doesn’t gradle ensure that the compilation pass made after the PCH task completes is configured consistently with the PCH pass? How can I get rid of this error?

I was able to workaround this problem using the configuration block below for the library where I’m using PCH. Fortunately we don’t build debug binaries on linux, so I hopefully won’t have to worry about any similar problems there.

			binaries.withType(StaticLibraryBinarySpec) {
				if (toolChain in VisualCpp) {
				  if ( buildType == buildTypes.debug ||
					   buildType == buildTypes.debug_static ) {
						def pchPath = file("$buildDir/objs/myLib/static/${targetPlatform.name}/${buildType.name}/myLibCppPCH").getAbsolutePath()
						cppCompiler.args "/Fd$pchPath"
					}
				}
			}

This seems like a bug. Could you open an issue on https://github.com/gradle/gradle-native with a short sample that reproduce the issue? Thanks.