Derek
Derek

Reputation: 8763

Can Exec task in msbuild fail if exitCode = 0 but there is logging to standard error?

I am curious about:

https://learn.microsoft.com/en-us/visualstudio/msbuild/exec-task?view=vs-2019

ExitCode Optional Int32 output read-only parameter.

Specifies the exit code that is provided by the executed command, except that if the task logged any errors, but the process had an exit code of 0 (success), ExitCode is set to -1.

Can msbuild fail on Exec even if process has an exit code of 0 but it logs to standard error?

I tried to make it fail with the following code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
   class Program
   {
      static int Main( string[] args )
      {
         using ( var stdErr = Console.Error )
         {
            for ( int i = 0; i <= 100; i++ )
            {
               stdErr.WriteLine( $"Failed to copy attempt {i}." );
               System.Threading.Thread.Sleep( 50 );
            }

            // Close redirected error stream.
            Console.Error.Close();
         }
         return 0;
      }
   }
}

It is being called as part of a standard props file.

   <Target 
          Name = "IvaraSign"
          AfterTargets="CoreBuild"
          BeforeTargets="CopyFilesToOutputDirectory" >
     <!-- Update version on main output -->
     <!-- this works when the solution being compiled is in the dotnet folder. -->
     <Exec Command="C:\Users\Derek.Morin\source\repos\ConsoleApp3\bin\Debug\ConsoleApp3.exe"/>
  </Target>

When I build in msbuild I can see the error text in the build output but the compiling passes.

Upvotes: 0

Views: 577

Answers (1)

Derek
Derek

Reputation: 8763

Does the MSBuild Exec task search STDOUT for the string "error"?

I was able to duplicate making exec file fail with the following

  static int Main( string[] args )
  {
     using ( var stdErr = Console.Error )
     {
        stdErr.WriteLine( $@"signtool.exe : error information: ""Error: Store IsDiskFile() failed."" (-2147024864/0x80070020) [d:\work\1\s\common\MAFPlugins\MAFScripting\Contracts\Contracts.csproj]" );
     }
     Console.WriteLine( "but I'm still going to return 0" );
     return 0;
  }

The workaround is to use IgnoreStandardErrorWarningFormat="true".

<Exec Command="C:\ConsoleApp3\ConsoleApp3.exe" IgnoreStandardErrorWarningFormat="true" />

Upvotes: 1

Related Questions