xmlvisual-studiomsbuildnmakeoutput-redirect

msbuild - Why do I get error MSB4025 when using output redirection in my VS NMake Build Command Line?


I use a Visual Studio makefile project and have individual build commands in the NMakeBuildCommandLine property of the vcxproj file.
When one of these commands uses output redirection like this:

echo "unwanted output"  >nul 2>&1

I get a build error when calling msbuild:

all.vcxproj(53,30): error MSB4025:
The project file could not be loaded. An error occurred while parsing EntityName. Line 53, position 33.
or
Die Projektdatei konnte nicht geladen werden. Beim Analysieren von 'EntityName' ist ein Fehler aufgetreten. Zeile 53, Position 33.

Why?

I found the solution by myself and want to provide it here.


Solution

  • This error can only happen, if you directly edit the NMakeBuildCommandLine within the vcxproj file.
    You can get the same error with other command properties too.

    The reason is, that > and & are invalid special characters.
    As stated in the MSBuild help, special characters need to be escaped.
    However in this help > and & are not listed, although they are invalid in XML elements and a vcxproj is xml.

    The MSBuild help says

    To escape a special character, use the syntax %<xx>, where <xx> represents the ASCII hexadecimal value of the character.

    which means to replace

    echo "unwanted output"  >nul 2>&1
    

    with

    echo "unwanted output"  %3enul 2%3e%261
    

    By try I found out, that the following variants do also work:

    echo "unwanted output"  &#62;nul 2&#62;&#38;1
    echo "unwanted output"  &gt;nul 2&gt;&amp;1
    

    Interestingly, only the last 2 variants are afterwards displayed correctly in the project properties > NMake > Build Command Line as >nul 2>&1.
    When editing the project properties > NMake > Build Command Line directly, and you enter >nul 2>&1, VS writes &gt;nul 2&gt;&amp;1 to the vcxproj.

    And since the last variant is more readable than the hexadecimal syntax, I would prefer this too.

    I tested with Visual Studio 17.2.5.


    There was also a similar answer here, but it does neither mention the character > (which is used for output redirection but not contained in the MSBuild help) nor does it mention NMakeBuildCommandLine (that's the problem I was coming from).
    So I decided to make this new question & answer to help others with a similar problem.