8

I have the following exec task, performing checkin of assemblyinfo.cs files. I'm trying to return the exit code, but for some reason it is always empty.

<!--Checkin if all succeeded-->
<Exec Condition=" '$(LocalCompilationSuccess)' != 'Failed' and '$(LocalTestSuccess)' != 'Failed' " ContinueOnError="True"
              Command='&quot;$(TfCommand)&quot; checkin /recursive /comment:"$(NoCICheckInComment) $(BuildDefinitionName): build succeeded, checkin changes." /override:"TeamBuild $(BuildDefinitionName)" $/SomeProject/Trnk' WorkingDirectory="$(SolutionRoot)"  >
  <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
</Exec>

I've tried to read the exit code in 2 ways:

'%(ErrorCode.Identity)'
'$(ErrorCode)'

Both are empty. Any suggestions?

jaspernygaard
  • 3,098
  • 5
  • 35
  • 52

1 Answers1

20

In general it works as you have shown.

For reference, here is a more "selfcontained" example:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <Target Name="help">
    <Exec ContinueOnError="True" Command='cmd.exe /c dir'>
       <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
    </Exec>
    <Message Importance="high" Text="$(ErrorCode)"/>
  </Target>
</Project>

A couple of things you may want to consider however:

  • Make sure your Exec even executes, that is Condition evaluates to True.

  • Output the ErrorCode property using the Message-Task, to see if it is actually set (to the value you expect). However, make sure MSBuild will show the output, by either using Importance='high' or by running msbuild.exe /v:d to enable detailed messages.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • Issue caused by condition preventing the method from being invoked. Thanks – jaspernygaard Jun 21 '12 at 06:52
  • 2
    Under mono/xbuild I also had to specify `IgnoreExitCode="true"` in `` task for property to be populated. – weirdan Jan 06 '16 at 21:39
  • 3
    Additionally, I suggest setting `IgnoreStandardErrorWarningFormat="true"` due to https://github.com/nunit/nunit-console/issues/242. Otherwise `ExitCode` might be `-1` instead of the actual exit code of the started process. – matthid Nov 18 '19 at 12:19