Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MSBuild Converter regex to support more MSBuild log variants #2579

Merged
merged 4 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* BUGFIX: Update `merge` command to properly produce runs by tool and version when passed the `--merge-runs` argument. [#2488](https://github.com/microsoft/sarif-sdk/pull/2488)
* BUGFIX: Eliminate `IOException` and `DirectoryNotFoundException` exceptions thrown by `merge` command when splitting by rule (due to invalid file characters in rule ids). [#2513](https://github.com/microsoft/sarif-sdk/pull/2513)
* BUGFIX: Fix classes inside NotYetAutoGenerated folder missing `virtual` keyword for public methods and properties, by regenerate and manually sync the changes. [#2537](https://github.com/microsoft/sarif-sdk/pull/2537)
* BUGFIX: MSBuild Converter accepts case insensitive keywords and supports PackageValidator msbuild log. [#2579](https://github.com/microsoft/sarif-sdk/pull/2579)
Copy link
Collaborator

@shaopeng-gh shaopeng-gh Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUGFIX: MSBuild Converter accepts case insensitive keywords and supports PackageValidator msbuild log

may need some lang update make it more clear that accepts case insensitive keywords is the the bug fixed but after fix, e.g. now accepts #Resolved

Copy link
Member

@michaelcfanning michaelcfanning Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSBuild Converter accepts case insensitive keywords and supports PackageValidator msbuild log

It's super close! Use this:

MSBuild Converter now accepts case insensitive keywords and supports PackageValidator msbuild log output.
#Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion. updated.


## **v3.1.0** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/3.1.0) | [Driver](https://www.nuget.org/packages/Sarif.Driver/3.1.0) | [Converters](https://www.nuget.org/packages/Sarif.Converters/3.1.0) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/3.1.0) | [Multitool Library](https://www.nuget.org/packages/Sarif.Multitool.Library/3.1.0)

Expand Down
17 changes: 12 additions & 5 deletions src/Sarif.Converters/MSBuildConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private IList<Result> GetResults(TextReader reader)
}

private const string ErrorLinePattern = @"
(?i) # Case insensitive
^
\s*
( # EITHER
Expand All @@ -83,9 +84,9 @@ private IList<Result> GetResults(TextReader reader)
\s*:\s*
(?<qualifiedLevel>
(?<levelQualification>.*) # For example, 'fatal'.
(?<level>error|warning|note|info|pass|review|open|notapplicable)
(?<level>error|err|warning|wrn|note|info|pass|review|open|notapplicable)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrn

just to get info, the reason we need wrn and err ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see a definition of what the level should be in .ERR file. Have seen "Error"/"Warning" also the abbreviation forms "ERR"/"WRN" in build logs. Here just try to match the keyword as much as possible

)
\s+
\s*:?\s*
(?<ruleId>[^\s:]+)
\s*:\s*
(?<message>.*)
Expand Down Expand Up @@ -154,11 +155,17 @@ private static FailureLevel GetFailureLevelFrom(string level, out ResultKind res
resultKind = ResultKind.Fail;
FailureLevel failureLevel = FailureLevel.None;

switch (level)
switch (level.ToLower())
{
// Failure cases. ResultKind.Fail + specific failure level
case "warning": { failureLevel = FailureLevel.Warning; break; }
case "error": { failureLevel = FailureLevel.Error; break; }
case "warning":
case "wrn":
failureLevel = FailureLevel.Warning;
break;
case "error":
case "err":
failureLevel = FailureLevel.Error;
break;
case "note": { failureLevel = FailureLevel.Note; break; }

// Non-failure cases. FailureLevel.None + specific result kind
Expand Down
59 changes: 59 additions & 0 deletions src/Test.UnitTests.Sarif.Converters/MSBuildConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

using FluentAssertions;

using Microsoft.CodeAnalysis.Sarif.Writers;

using Xunit;

namespace Microsoft.CodeAnalysis.Sarif.Converters
{
public class MSBuildConverterTests : ConverterTestsBase<MSBuildConverter>
{
[Fact]
public void Converter_RequiresInputStream()
{
var converter = new MSBuildConverter();
Action action = () => converter.Convert(input: null, output: new ResultLogObjectWriter(), dataToInsert: OptionallyEmittedData.None);
action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void Converter_RequiresResultLogWriter()
{
var converter = new MSBuildConverter();
Action action = () => converter.Convert(input: new MemoryStream(), output: null, dataToInsert: OptionallyEmittedData.None);
action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void Converter_WhenInputIsEmpty_ReturnsNoResults()
{
string input = s_extractor.GetResourceInputText("Empty.ERR");
string expectedOutput = s_extractor.GetResourceExpectedOutputsText("NoResults.sarif");
RunTestCase(input, expectedOutput);
}

[Fact]
public void Converter_WhenResultRowIsInvalid_ReturnsNoResults()
{
string input = s_extractor.GetResourceInputText("InvalidResult.ERR");
string expectedOutput = s_extractor.GetResourceExpectedOutputsText("NoResults.sarif");
RunTestCase(input, expectedOutput);
}

[Fact]
public void Converter_WhenInputContainsValidResults_ReturnsExpectedOutput()
{
string input = s_extractor.GetResourceInputText("ValidResults.ERR");
string expectedOutput = s_extractor.GetResourceExpectedOutputsText("ValidResults.sarif");
RunTestCase(input, expectedOutput);
}

private static readonly TestAssetResourceExtractor s_extractor = new TestAssetResourceExtractor(typeof(MSBuildConverterTests));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,6 @@
<None Remove="TestData\Hdf\**" />
</ItemGroup>

<ItemGroup>
<None Remove="TestData\ClangTidyConverter\ExpectedOutputs\NoResults.sarif" />
<None Remove="TestData\ClangTidyConverter\ExpectedOutputs\ValidResults.sarif" />
<None Remove="TestData\ClangTidyConverter\ExpectedOutputs\ValidResultsWithLog.sarif" />
<None Remove="TestData\ClangTidyConverter\Inputs\Empty.yaml" />
<None Remove="TestData\ClangTidyConverter\Inputs\InvalidResult.yaml" />
<None Remove="TestData\ClangTidyConverter\Inputs\ValidResults.yaml" />
<None Remove="TestData\ClangTidyConverter\Inputs\ValidResultsWithLog.yaml" />
<None Remove="TestData\ClangTidyConverter\Inputs\ValidResultsWithLog.yaml.log" />
<None Remove="TestData\FlawFinderConverter\ExpectedOutputs\NoResults.sarif" />
<None Remove="TestData\FlawFinderConverter\ExpectedOutputs\ValidResults.sarif" />
<None Remove="TestData\FlawFinderConverter\Inputs\Empty.csv" />
<None Remove="TestData\FlawFinderConverter\Inputs\InvalidHeader.csv" />
<None Remove="TestData\FlawFinderConverter\Inputs\InvalidResult.csv" />
<None Remove="TestData\FlawFinderConverter\Inputs\OldVersionResult.csv" />
<None Remove="TestData\FlawFinderConverter\Inputs\OnlyHeaderLine.csv" />
<None Remove="TestData\FlawFinderConverter\Inputs\ValidResults.csv" />
<None Remove="TestData\HdfConverter\ExpectedOutputs\NoResults.sarif" />
<None Remove="TestData\HdfConverter\ExpectedOutputs\ValidResults.sarif" />
<None Remove="TestData\HdfConverter\Inputs\Empty.json" />
<None Remove="TestData\HdfConverter\Inputs\InvalidResult.json" />
<None Remove="TestData\HdfConverter\Inputs\ValidResults.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="TestData\ClangTidyConverter\ExpectedOutputs\NoResults.sarif" />
<EmbeddedResource Include="TestData\ClangTidyConverter\ExpectedOutputs\ValidResults.sarif" />
Expand All @@ -69,6 +45,11 @@
<EmbeddedResource Include="TestData\HdfConverter\Inputs\Empty.json" />
<EmbeddedResource Include="TestData\HdfConverter\Inputs\InvalidResult.json" />
<EmbeddedResource Include="TestData\HdfConverter\Inputs\ValidResults.json" />
<EmbeddedResource Include="TestData\MSBuildConverter\ExpectedOutputs\NoResults.sarif" />
<EmbeddedResource Include="TestData\MSBuildConverter\ExpectedOutputs\ValidResults.sarif" />
<EmbeddedResource Include="TestData\MSBuildConverter\Inputs\Empty.ERR" />
<EmbeddedResource Include="TestData\MSBuildConverter\Inputs\InvalidResult.ERR" />
<EmbeddedResource Include="TestData\MSBuildConverter\Inputs\ValidResults.ERR" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json",
"version": "2.1.0",
"runs": [
{
"results": [],
"tool": {
"driver": {
"name": "MSBuild"
}
},
"columnKind": "utf16CodeUnits"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json",
"version": "2.1.0",
"runs": [
{
"results": [
{
"ruleId": "MSB1003",
"level": "error",
"message": {
"text": "Specify a project or solution file. The current working directory does not contain a project or solution file."
}
},
{
"ruleId": "MSB1001",
"level": "error",
"message": {
"text": "Unknown switch, Switch: /maxcpucount."
}
},
{
"ruleId": "PV1002",
Copy link
Collaborator

@shaopeng-gh shaopeng-gh Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PV1002

should this also be "level": "error", ?
ERROR: PV1002 #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug fixed

"level": "error",
"message": {
"text": "Package sense1ds[1.1000.1] is missing the content manifest. For more information, see https://osgwiki.com/wiki/PackageValidator#PV1002"
}
},
{
"ruleId": "PV1000",
"level": "note",
"message": {
"text": "Package diagtrackservice.vb_release.x86[0.0.0] is missing provenance data. For more information, see https://osgwiki.com/wiki/PackageValidator#PV1000"
}
},
{
"ruleId": "PV1004",
"message": {
"text": "Package microsoft.timetraveldebugging.test.x86[0.0.25] is missing the manifest catalog. For more information, see https://osgwiki.com/wiki/PackageValidator#PV1004"
}
}
],
"tool": {
"driver": {
"name": "MSBuild"
}
},
"columnKind": "utf16CodeUnits"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
----- Catalog construction errors -----
Error #1
Microsoft.VisualStudio.Composition.PartDiscoveryException: ReflectionTypeLoadException while enumerating types in assembly "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\EXTENSIONS\MICROSOFT\GRAPHICS\DEBUGGER\VsGraphicsDebuggerPkg.dll". Results will be incomplete. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at Microsoft.VisualStudio.Composition.PartDiscovery.CombinedPartDiscovery.GetTypes(Assembly assembly)
at Microsoft.VisualStudio.Composition.PartDiscovery.<>c__DisplayClass32_0.<CreateAssemblyDiscoveryBlockChain>b__0(Assembly a)
--- End of inner exception stack trace ---
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
----- non msbuild error -----
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.UniversalApps.Deployment, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.UniversalApps.Deployment, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

----- VS msbuild error -----
msbuild : err MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
msbuild : error MSB1001: Unknown switch, Switch: /maxcpucount.

----- Package Validator build error -----
%object_root%\amcore\wcd\source\communicator\sense1ds\dll\$(o)\vpack : ERROR: PV1002: Package sense1ds[1.1000.1] is missing the content manifest. For more information, see https://osgwiki.com/wiki/PackageValidator#PV1002
%object_root%\onecore\base\telemetry\utc\tests\functionaltests\vpacks\diagtrackservice.vb_release\$(o)\vpack : NOTE: PV1000: Package diagtrackservice.vb_release.x86[0.0.0] is missing provenance data. For more information, see https://osgwiki.com/wiki/PackageValidator#PV1000
%object_root%\onecore\sdktools\debuggers\ttd\undocked\testbinaries\$(o)\vpack : WRN: PV1004: Package microsoft.timetraveldebugging.test.x86[0.0.25] is missing the manifest catalog. For more information, see https://osgwiki.com/wiki/PackageValidator#PV1004