I recently picked up a solution that my brother had previously started and I was confounded by an error received during the pre-build events on a particular project. The script was simple enough
@if $(SkipPlatformVerification) == true (
echo "Warning Platform Verification Task is disabled"
) else (
echo "PVT is Enabled"
)
He was checking an environment variable to alert whether or not the platform verification task was enabled.
I started the build and was presented with the error “The command "@if == true (
echo "Warning Platform Verification Task is disabled"
) else (
echo "PVT is Enabled"
)"
exited with code 255.
After a quick Google search I found that the very detailed “exited with code 255” can mean a myriad of things and would not point me towards a solution, it just let me know there was a problem with the script.
My first solution was obvious, I did not have the environment variable [SkipPlatformVerification] and it was failing to read a value thus throwing the error. After adding the environment variable (and rebooting VS) and building again, it did indeed succeed this time. Looking at the output is what I found interesting. The statement appeared in the output as below.
@if true == true (
echo "Warning Platform Verification Task is disabled"
) else (
echo "PVT is Enabled"
)
"Warning Platform Verification Task is disabled"
Visual Studio apparently pre-evaluates the $(variable) syntax in the same way that javascript handles variables in that by the time the statement is evaluated at run time, the variable has been replaced by the variable value itself. So in the case of our statement above, when I didn’t have the environment variable created the output looked like this.
@if == true (…
… which is obviously a syntax error.
However using the syntax ‘%Variable%’ is not pre-evaluated and looking at the build output shows the following.
@if %SkipPlatformVerification% == true (
echo "Warning Platform Verification Task is disabled"
) else (
echo "PVT is Enabled"
)
The simple solution to the problem is to treat everything as a string. In the case of the instance where the environment variable is missing, the output looked like this and completed successfully.
@if '' == 'true' (
echo "Warning Platform Verification Task is disabled"
) else (
echo "PVT is Enabled"
)
While this ended up to ultimately be a simple syntax problem, I wanted to make note of the way Visual Studio handles the different variables for future reference.