Showing errors from a build-time tool with source

2019-02-26 04:28发布

问题:

My project has an MSBuild target which runs a tool which generated some files:

<Exec Command="$(MyTool) @(Content)"
      ConsoleToMSBuild="true"
      IgnoreExitCode="False"
      LogStandardErrorAsError="true" />

If the tool detects some error in its inputs (the @(Content) files), it outputs the error to standard error. This makes MSBuild to fail the build, and Visual Studio to show this text in the error list - all of which is great!

Now, the tool also knows the exact file&line where the error is, and I want the error list to show that, and that double-click would lead to the file. If the tool were an MSBuild Task rather than a standalone exe, I could call TaskLoggingHelper.LogError(..., file, lineNumber, ...). How can I accomplish the same with a standalone exe tools? Do I need to write a wrapper task which parses the tool's error? Is there such a task already available?

回答1:

I don't know of any readily available mechanism to do this but see e.g. Viewing PowerShell's Select-String output in Visual Studio: al you need to do is make your output match what VS expects and you automatically get desired behaviour. VS looks for messages like

<file>(<line>): error <code>: <message>

For instance msbuild, compiler, linker messages all adhere to this format. So if you have your custom tool output that format it will be shown in the Error List and you can doubleclick on it to navigate to the specified file and line.