How to capture error code in command line with MSBUILD?
-
Saturday, November 01, 2008 6:59 PMHi,
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 by baztheman Saturday, November 01, 2008 7:11 PM
All Replies
-
Monday, November 03, 2008 7:46 AM
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" set BUILD_STATUS=%ERRORLEVEL% if %BUILD_STATUS%==0 goto end if not %BUILD_STATUS%==0 goto fail :fail pause exit /b 1 :end exit /b 0
http://blogoscoped.com/archive/2005-08-24-n14.html- Marked As Answer by Rong-Chun Zhang Friday, November 07, 2008 3:47 AM
-
Monday, November 03, 2008 11:45 AMModerator
baztheman, an errorlevel of 1 means that an error occured, so getting 1 for your failed build is expected behaviour.
If you type msbuild 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>- Marked As Answer by Rong-Chun Zhang Friday, November 07, 2008 3:47 AM
-
Monday, December 29, 2008 10:40 PMMike, 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 -
Wednesday, July 01, 2009 4:37 PMAll,
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. -
Wednesday, July 01, 2009 5:25 PMSo 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?
-
Wednesday, July 01, 2009 9:18 PMAll,
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. -
Saturday, September 19, 2009 12:10 AM
Copy the %ErrorLevel% variable content to another variable directly after the call to msbuild. The following batch works for me:
Thank you , this really help me!
@%WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe "%CD%\MsBuild\TheProjectToBuild.csproj" set BUILD_STATUS=%ERRORLEVEL% if %BUILD_STATUS%==0 goto end if not %BUILD_STATUS%==0 goto fail :fail pause exit /b 1 :end exit /b 0
http://blogoscoped.com/archive/2005-08-24-n14.html
Amar por amar es agua que no conocen los hombres.

