0

Consider the project

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets='repro'>
    <Target Name = 'no_spaces'>
        <Exec Command = 'echo C:\path\to\file.ext (5) error ERR999'/>
    </Target>
    <Target Name = 'spaces'>
        <Exec Command = 'echo C:\path\to\file.ext (5) error ERR999:'/>
    </Target>
    <Target Name = 'repro'
        DependsOnTargets = 'no_spaces;spaces'/>
</Project>

whose output includes the lines

MSBuild version 17.5.0+6f08c67f3 for .NET Framework
no_spaces:
  echo C:\path\to\file.ext (5) error ERR999
  C:\path\to\file.ext (5) error ERR999
spaces:
  echo C:\path\to\file.ext (5) error ERR999:
C : \path\to\file.ext (5) error ERR999:  [C:\path\to\msbuild.proj]

In my console the last line only is shown in red. Note the added spaces in the last line. Those spaces seem to prevent IDE problem matcher regex from finding those lines.

  1. Why is does the last line start with C : \path instead of C:\path?
  2. Is there a way to prevent those spaces from being introduced?

github issue

alx9r
  • 3,675
  • 4
  • 26
  • 55

2 Answers2

1

I didn't receive your result when I ran your file.

The output of the spaces target for me was

spaces:
  echo C:\path\to\file.ext (5) error ERR999:
C : \path\to\file.ext (5) error ERR999:  [C:\Users\####\test.proj]
C:\Users\####\test.proj(6,9): error MSB3073: The command "echo C:\path\to\file.ext (5) error ERR999:" exited with co
de -1.

The last two lines are in red, i.e. the line with extra spaces is part of an error message. I don't understand the cause of the error. Although I don't understand why, the trailing ':' seems to be the issue.

But if you are just trying to output text, don't use exec echo. Use the Message task, e.g.

    <Target Name="msg">
        <Message Text="C:\path\to\file.ext (5) error ERR999:"/>
    </Target>

Update

There may be an issue within the Exec task within its error handling. The following example essentially disables the Exec task from handling and reporting a process error. What's interesting is that the reported exit code changes from -1 to 0.

    <Target Name="spaces">
        <Exec Command="echo C:\path\to\file.ext (5) error ERR999:" IgnoreExitCode="true" IgnoreStandardErrorWarningFormat="true">
            <Output TaskParameter="ExitCode" PropertyName="ExitCode" />
        </Exec>
        <Error Text="The command exited with code $(ExitCode)." Condition="'$(ExitCode)' != '0'" />
    </Target>

The output looks like:

spaces:
  echo C:\path\to\file.ext (5) error ERR999:
  C:\path\to\file.ext (5) error ERR999:

If there is a defect in the Exec task, the above example is a work-around.

Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14
  • Thank you for confirming what you see. Unfortunately using the message task doesn’t help me with my underlying problem: Those extraneous spaces interfere with the problem matchers which prevents those errors from the build showing in the IDE. I was using echo to demonstrate the problem. Those error lines come from build tools in my real project. – alx9r Mar 23 '23 at 02:42
  • I updated my answer with a possible work around. – Jonathan Dodds Mar 23 '23 at 14:44
  • Thank you for that option. I think that might be one step closer. Do you see whitespace in front of “C:\path…” in your output with the workaround? Some of the mscompile/msbuild matchers built into IDEs require no whitespace to match. I _think_ vscode’s $mscompile is one such matcher. – alx9r Mar 23 '23 at 15:07
  • With the 'work around' there is no extra whitespace inserted. – Jonathan Dodds Mar 23 '23 at 15:13
  • In the output shown there are what looks like two spaces after the newline and before “C:”. Those spaces seem to be the difference between IDEs matching and not. – alx9r Mar 23 '23 at 15:20
  • Do you mean the indent that MSBuild uses to indicate the messages are related to a specific target? That would be breaking for your `no_spaces` target as well. – Jonathan Dodds Mar 23 '23 at 15:32
  • 1
    Yes. But I was mistaken. Your suggestions led me to the [underlying cause](https://stackoverflow.com/a/75825528/1404637). Thank you. – alx9r Mar 23 '23 at 16:20
  • Ah -- I'll bet the incorrectly formatted line is confusing the error handling within the `Exec` task. – Jonathan Dodds Mar 23 '23 at 18:17
0

The problem is a missing colon in the error line emitted:

C:\path\to\file.ext(1) error MSB3073:  <== missing colon after (1)
C:\path\to\file.ext(1): error MSB3073: <== correctly formatted line

MSBuild checks output for error lines against a default regex. Comparing the matches of that regular expression with those two error lines usig PowerShell

'C:\path\to\file.ext(1) error MSB3073:','C:\path\to\file.ext(1): error MSB3073:' | % {
    $_ -match '^\s*(((?<ORIGIN>(((\d+>)?[a-zA-Z]?:[^:]*)|([^:]*))):)|())(?<SUBCATEGORY>(()|([^:]*? )))(?<CATEGORY>(error|warning))( \s*(?<CODE>[^: ]*))?\s*:(?<TEXT>.*)$' | Out-Null
    [pscustomobject]@{
        line = $_
        ORIGIN = $Matches.ORIGIN
        SUBCATEGORY = $Matches.SUBCATEGORY
    }}

yields

line                                   ORIGIN                 SUBCATEGORY
----                                   ------                 -----------
C:\path\to\file.ext(1) error MSB3073:  C                      \path\to\file.ext(1)
C:\path\to\file.ext(1): error MSB3073: C:\path\to\file.ext(1)

I suppose ORIGIN and SUBCATEGORY are joined with ' : ' by msbuild and then output as the error line which would explain the spaces.

The output from

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets='repro'>
    <Target Name = 'missing_colon_has_spaces'>
        <Exec Command = 'echo C:\path\to\file.ext(1) error MSB3073:'/>
    </Target>
    <Target Name = 'properly_formatted_has_no_spaces'>
        <Exec Command = 'echo C:\path\to\file.ext(1): error MSB3073:'/>
    </Target>
    <Target Name = 'repro'
        DependsOnTargets = 'properly_formatted_has_no_spaces;missing_colon_has_spaces'/>
</Project>

outputs the following lines when invoked as msbuild -t:missing_colon_has_spaces; msbuild -t:properly_formatted_has_no_spaces:

missing_colon_has_spaces:
  echo   C:\path\to\file.ext(1) error MSB3073:
C : \path\to\file.ext(1) error MSB3073:  [C:\path\to\file\msbuild-repro\msbuild.proj]
C:\path\to\file\msbuild.proj(4,9): error MSB3073: The command "echo   C:\path\to\file.ext(1) error MSB3073:" exited with code -1.

properly_formatted_has_no_spaces:
  echo   C:\path\to\file.ext(1): error MSB3073:
C:\path\to\file.ext(1): error MSB3073:  [C:\path\to\file\msbuild-repro\msbuild.proj]
C:\path\to\msbuild.proj(7,9): error MSB3073: The command "echo   C:\path\to\file.ext(1): error MSB3073:" exited with code -1.
alx9r
  • 3,675
  • 4
  • 26
  • 55