msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Consider SARIF support for tool output -> MSBuild logs

Open rainersigwald opened this issue 4 years ago • 7 comments

The Microsoft C++ team is considering adding SARIF support to the MSVC toolchain. That allows capturing much more information about errors/warnings than we currently support, through the canonical error format.

MSBuild should consider extending the tool-output-scraping support to include SARIF output, and map the SARIF format to our error/warning structured events.

This might also help with issues like #8019 that primarily arise because an error cannot today have supplemental information.

rainersigwald avatar Oct 06 '22 13:10 rainersigwald

One thing that might be important: today's output scanners are line-at-a-time, so this might require tool output SARIF to be in JSON lines.

rainersigwald avatar Oct 06 '22 13:10 rainersigwald

@yuehuang010 is working on this.

rainersigwald avatar Feb 22 '23 17:02 rainersigwald

Any updates on this?

When using the RunCodeAnalysis property when building C/C++ Projects (.vcxproj), MSBuild forwards the /analyze flag to MSVC. This causes MSVC to output a bunch of XML files for each source file. These files are then merged by the RunMergeNativeCodeAnalysis task (from FxCopTask.dll). There is no easy way to pass /analyze:log:format:sarif as well without breaking the build completely.

Unfortunately, these tasks are provided by the Visual Studio installation and are not open-source. So there's no way to contribute this feature.

Thinking of it, the Microsoft Developer Community > VisualStudio might be a better place to request this feature.

jdoubleu avatar Mar 28 '25 14:03 jdoubleu

Thinking of it, the Microsoft Developer Community > VisualStudio might be a better place to request this feature.

Yes, that's best for the VC++ tasks and targets.

However for this one, I think you might need to set <UseStructuredOutput>true</UseStructuredOutput>--have you tried that?

(We haven't done this in MSBuild proper because vcxproj was able to bypass us to get SARIF from the compiler to the VS process, reducing the priority on our side.)

rainersigwald avatar Mar 28 '25 14:03 rainersigwald

This didn't work: When running msbuild My.sln /p:RunCodeAnalysis=true with the UseStructuredOutput option set in the projects, no SARIF output was generated whatsoever. I assume it either only affects the "log" output or when I manually run Code Analysis in Visual Studio.

jdoubleu avatar Mar 28 '25 15:03 jdoubleu

@olgaark ☝️

rainersigwald avatar Mar 28 '25 15:03 rainersigwald

UseStructureOutput property controls how cl.exe communicates with the build, not what it logs. To get the sarif log file, use /experimental:log switch https://learn.microsoft.com/en-us/cpp/build/reference/sarif-output?view=msvc-170 https://learn.microsoft.com/en-us/cpp/build/reference/experimental-log?view=msvc-170

olgaark avatar Mar 28 '25 18:03 olgaark