.NET Framework Bookmark and Share   
 index > MSBuild > How to capture error code in command line with MSBUILD?
 

How to capture error code in command line with MSBUILD?

Hi,

I am having problems with capturing error from msbuild in command line. Here is my example:

msbuild build.sln /t:Rebuild /p:configuration=Debug /p:platform="InvalidPlatform"

You can clearly see the build has failed. However, when I do echo %ERRORLEVEL% after running this command, the ERRORLEVEL is "1". Is it normal?

In fact if i type "msbuild sdfsdfdf", the error code will still be "0".

If I set ERRORLEVEL=1, then execute a successful msbuild compilation, the ERRORLEVEL is still "1". So, in my environment, I do not see msbuild changing ERRORLEVEL at all.

How can i run msbuild command in script or command line and effectively capture error code?

Thanks. A.
  • Edited bybaztheman Saturday, November 01, 2008 7:02 PM
  • Edited bybaztheman Saturday, November 01, 2008 7:11 PM
  •  
baztheman
Copy the %ErrorLevel% variable content to another variable directly after the call to msbuild. The following batch works for me:

@%WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe"%CD%\MsBuild\TheProjectToBuild.csproj"
setBUILD_STATUS=%ERRORLEVEL%
if%BUILD_STATUS%==0gotoend
ifnot%BUILD_STATUS%==0gotofail
:fail
pause
exit/b1
:end
exit/b0


http://blogoscoped.com/archive/2005-08-24-n14.html
marc_gsi
baztheman, an errorlevel of 1 means that an error occured, so getting 1 for your failed build is expected behaviour.

If you typemsbuild sdfsdfdf, then you should be getting a 1. Try it in the sample below (just replace the call to the proj). In the sample below, I get a 0 if the file to copy exists, and a 1 if it doesnt, ie an error is logged.

ECHO Off
@%WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe "c:\a\copy.proj"
IF NOT %ERRORLEVEL%==0 GOTO ERROR
GOTO DONE

:ERROR
ECHO %ERRORLEVEL%
ECHO --------------------------------------------------
ECHO ERROR HAS OCCURED
ECHO --------------------------------------------------
pause
GOTO END

:DONE
ECHO %ERRORLEVEL%
ECHO "AllOK"
pause
:END


--------------------- copy.proj sourcecode ---------------------
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="2.0" TargetFramework="2.0">
<Target Name="CopyFiles">
<ItemGroup>
<MySourceFiles Include="c:\c.cs"/>
</ItemGroup>
<Copy SourceFiles="@(MySourceFiles)" DestinationFolder="c:\MyProject\Destination"/>
<Message Text="hello"/>
</Target>
</Project>

Mike Fourie
Copy the %ErrorLevel% variable content to another variable directly after the call to msbuild. The following batch works for me:

@%WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe"%CD%\MsBuild\TheProjectToBuild.csproj"
setBUILD_STATUS=%ERRORLEVEL%
if%BUILD_STATUS%==0gotoend
ifnot%BUILD_STATUS%==0gotofail
:fail
pause
exit/b1
:end
exit/b0


http://blogoscoped.com/archive/2005-08-24-n14.html
marc_gsi
baztheman, an errorlevel of 1 means that an error occured, so getting 1 for your failed build is expected behaviour.

If you typemsbuild sdfsdfdf, then you should be getting a 1. Try it in the sample below (just replace the call to the proj). In the sample below, I get a 0 if the file to copy exists, and a 1 if it doesnt, ie an error is logged.

ECHO Off
@%WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe "c:\a\copy.proj"
IF NOT %ERRORLEVEL%==0 GOTO ERROR
GOTO DONE

:ERROR
ECHO %ERRORLEVEL%
ECHO --------------------------------------------------
ECHO ERROR HAS OCCURED
ECHO --------------------------------------------------
pause
GOTO END

:DONE
ECHO %ERRORLEVEL%
ECHO "AllOK"
pause
:END


--------------------- copy.proj sourcecode ---------------------
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="2.0" TargetFramework="2.0">
<Target Name="CopyFiles">
<ItemGroup>
<MySourceFiles Include="c:\c.cs"/>
</ItemGroup>
<Copy SourceFiles="@(MySourceFiles)" DestinationFolder="c:\MyProject\Destination"/>
<Message Text="hello"/>
</Target>
</Project>

Mike Fourie
Mike, Marc,

Thanks for the replies.

Mike, thats the issue, I am not seeing msbuild return 1 when there is an error. I know that is the expected behavior and hence the post.

Marc, I am not seeing msbuild return 1 when failed and hence the code you posted wont work either. See the outputs in the bottom of this post.

I noticed from both of your post that you two are using msbuild from framework 3.5, I am using the one from 2.0.50727.1433. Can anyone tell me if that makes a difference? I am still not sure why msbuild not returning non zero when encounter error.

Thanks.

B.


Here are the outputs...

C:\works\trunk\Tools\Build>bazthemail.bat

C:\works\trunk\Tools\Build>echo TEST1:0
TEST1:0

C:\works\trunk\Tools\Build>MSBuild.exe "TheProjectToBuild.csproj"
Microsoft (R) Build Engine Version 2.0.50727.1433
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist.
Switch: TheProjectToBuild.csproj

C:\works\trunk\Tools\Build>set BUILD_STATUS=0

C:\works\trunk\Tools\Build>echo TEST2:0
TEST2:0

C:\works\trunk\Tools\Build>if 0 == 0 goto end

C:\works\trunk\Tools\Build>exit /b 0
baztheman
All,

I am still confused about this, please help. Yes, I understand and expected that msbuild should return ERRORLEVEL=1 if error occurs. Lets go back to the simple test on the command line:

C:\works>set ERRORLEVEL=0

C:\works>echo %ERRORLEVEL%
0

C:\works>msbuild xxx
Microsoft (R) Build Engine Version 2.0.50727.1433
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist.
Switch: xxx

C:\works>echo %ERRORLEVEL%
0

Should i expect different result?

B.
bazthemail
So here is something strange... If i open a new command prompt window...

C:\works>echo %ERRORLEVEL%
0

C:\works>msbuild xxx
Microsoft (R) Build Engine Version 2.0.50727.1433
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist.
Switch: xxx

C:\works>echo %ERRORLEVEL%
1

C:\works>set ERRORLEVEL=0

C:\works>echo %ERRORLEVEL%
0

C:\works>msbuild xxx
Microsoft (R) Build Engine Version 2.0.50727.1433
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist.
Switch: xxx

C:\works>echo %ERRORLEVEL%
0

I noticed msbuild changed the ERRORLEVEL first time but not again. Is this an issue that I am looking at?


bazthemail
All,

I think i might have found the cause of my problem.

I should not have set the ERRORLEVEL like i treated it as a "variable". Windows programs generally cannot reset this.

http://blogs.conchango.com/merrickchaffer/archive/2008/02/28/resetting-errorlevel-in-batch-file-programming.aspx

If I do set ERRORLEVEL= to unset ERRORLEVEL, then let windows programs to put their return code in ERRORLEVEL, then it will work.

So, if i want to set ERRORLEVEL, I will do the following:

cmd /c exit 1

http://msmvps.com/blogs/martinzugec/archive/2006/03/27/88017.aspx

I hope nobody bump into this issue like me. If you do, that may be the solution.

B.
bazthemail
Copy the %ErrorLevel% variable content to another variable directly after the call to msbuild. The following batch works for me:

@%WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe"%CD%\MsBuild\TheProjectToBuild.csproj"
setBUILD_STATUS=%ERRORLEVEL%
if%BUILD_STATUS%==0gotoend
ifnot%BUILD_STATUS%==0gotofail
:fail
pause
exit/b1
:end
exit/b0


http://blogoscoped.com/archive/2005-08-24-n14.html
Thank you , this really help me!

Amar por amar es agua que no conocen los hombres.
Chesare

You can use google to search for other answers

Custom Search

More Threads

• MSBuild tasks priorities
• Msbuild Compatibility issue
• Construcing lines for WriteLinesToFile
• How do I test for empty properties set on the command line?
• managed vcbuild error/warning log file
• How to make application check for updates
• [Suggestion] Un-shuffle Output Window lines
• New Microsoft SDC Tasks Release 2.1.3071.0 (29 May 08)
• NMAKE
• How do I a custom Item Group from two different Item Groups