0
votes

I have 3 Post Build Events, the first event is invalid and should create an (exited with code 3) error when compiling my application. The question I have is why is VS 2017 not throwing the error when I compile my application?

if I remove the 2 xcopy events and leave the invalid event as the sole event in my Build Events then VS shows the error as expected but only when it is the sole build event.

My Build Events look like this (below), where the path to SignTool.exe is indeed invalid. The build event does indeed fail (the exe is not signed) but no error is reported unless I remove the 2 xcopy commands, Can anyone explain?

"C:\Program Files (x86)\Microsoft SDKsZZ\Windows\v7.1A\Bin\SignTool.exe" sign /f "C:\Data\Visual Studio\CodeSigningCertificate\Cert2020.pfx" /p deznads $(TargetPath)
xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Debug\*.* /y
xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Release\*.* /y
1

1 Answers

2
votes

Apparently, Visual Studio doesn't parse the post-build instructions into a set of individual commands, but rather executes it as a single batch file, then checks the final %ERRORLEVEL% to decide if it failed or succeeded.

In your example there are 3 commands and each would set %ERRORLEVEL% based on its own success or failure, so if commands #1 and #2 fail, but #3 succeeds the #3 result will override prior errors and the Studio will consider this a successful post-build step.

You can make your build event to stop on first error, but that's something you need to implement there. For example:

"C:\Program Files (x86)\Microsoft SDKsZZ\Windows\v7.1A\Bin\SignTool.exe" sign /f "C:\Data\Visual Studio\CodeSigningCertificate\Cert2020.pfx" /p deznads $(TargetPath)
if %ERRORLEVEL% EQU 0 xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Debug\*.* /y
if %ERRORLEVEL% EQU 0 xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Release\*.* /y

Here if command #1 or #2 set non-zero %ERRORLEVEL%, the batch won't execute the following commands and retains the error code in %ERRORLEVEL% variable, which will be reported by Visual Studio as a failed post-build step.

You can also implement more complex logic there, decide if the post-build event failed or not, and tell that to Visual Studio by returning 0 on success or other value on error with the exit N command.