Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd msbuild errorlevel is always 0

I have this code in my build.bat file

for /R %~dp0 %%A In (*.sln) do (
c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe %%A /t:rebuild /nologo /verbosity:minimal /flp:Verbosity=detailed;LogFile=%~dp0\Logs.txt;append=true /m /p:Configuration=Debug;Platform="Any CPU" /p:VisualStudioVersion="12.0"
if not %errorlevel%==0 set Failed+=1
pause)

My problem is that %errorlevel% always 0 even when log file have errors and warnings.

like image 429
Vladimir Rodchenko Avatar asked Dec 09 '14 15:12

Vladimir Rodchenko


2 Answers

Comment of JozefZ helped me:

Use either SETLOCAL EnableDelayedExpansion and !errorlevel! instead of %errorlevel% or (better) return to If ErrorLevel 1 syntax. Setting EnabledDelayedExpansion will cause each variable to be expanded at execution time rather than at parse time: parsing, the command interpreter evaluates variables line-by_line and/or command_by_command but all code block in () parentheses considers to be one command. On the other hand, If ErrorLevel 1 should to be read as if ErrorLevel is greater than or equal to 1 thus not equal to 0

like image 54
Vladimir Rodchenko Avatar answered Sep 24 '22 15:09

Vladimir Rodchenko


Took me a bit to sort this out but this is what worked for me:

msbuild mySolution.sln
if errorlevel 1 exit /b errorlevel

I did not need to use SETLOCAL EnableDelayedExpansion

like image 24
Kim Avatar answered Sep 25 '22 15:09

Kim