diff --git a/src/main/java/se/bjurr/violations/lib/parsers/MSBuildLogParser.java b/src/main/java/se/bjurr/violations/lib/parsers/MSBuildLogParser.java index c1edbed..9fe7283 100644 --- a/src/main/java/se/bjurr/violations/lib/parsers/MSBuildLogParser.java +++ b/src/main/java/se/bjurr/violations/lib/parsers/MSBuildLogParser.java @@ -9,43 +9,47 @@ import static se.bjurr.violations.lib.util.Utils.isNullOrEmpty; import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines; -import java.util.List; -import java.util.Locale; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; import se.bjurr.violations.lib.ViolationsLogger; import se.bjurr.violations.lib.model.SEVERITY; import se.bjurr.violations.lib.model.Violation; public class MSBuildLogParser implements ViolationsParser { + private static final String ParsingRegex = + "^\\s*(.*)(\\(([0-9]*),([0-9]*)\\)|\\s):\\s([^\\s]*)\\s([^:]*):([^\\[]*)((\\[(.*)\\])?)$"; + + private static final String[] MsBuildTargetFiles = + new String[] {"Microsoft.Common.CurrentVersion.targets"}; + @Override public Set parseReportOutput( final String reportContent, final ViolationsLogger violationsLogger) throws Exception { final Set violations = new TreeSet<>(); - final List> partsPerLine = - getLines( - reportContent, - "^\\s*([^\\(]*)\\(([^,]*),([^\\)]*)\\):\\s([^\\s]*)\\s([^:]*):([^\\[]*).*$"); + final List> partsPerLine = getLines(reportContent, ParsingRegex); for (final List parts : partsPerLine) { - final String fileName = parts.get(1); + final String projectFile = parts.get(10); + final String sourceFile = parts.get(1); + + final boolean isMsBuildTarget = isMsBuildTarget(sourceFile); + Integer lineNumber = 0; - if (!parts.get(2).isEmpty()) { - lineNumber = parseInt(parts.get(2)); + if (parts.get(3) != null && !parts.get(3).isEmpty() && !isMsBuildTarget) { + lineNumber = parseInt(parts.get(3)); } Integer columnNumber = 0; - if (parts.get(3) != null && !parts.get(3).isEmpty()) { - columnNumber = parseInt(parts.get(3)); + if (parts.get(4) != null && !parts.get(4).isEmpty() && !isMsBuildTarget) { + columnNumber = parseInt(parts.get(4)); } - final String severity = parts.get(4); - final String rule = parts.get(5); - final String message = parts.get(6).trim(); + final String severity = parts.get(5); + final String rule = parts.get(6); + final String message = parts.get(7).trim(); violations.add( // violationBuilder() // .setParser(MSBULDLOG) // .setStartLine(lineNumber) // .setColumn(columnNumber) // - .setFile(fileName) // + .setFile(isMsBuildTarget ? projectFile : sourceFile) // .setSeverity(this.toSeverity(severity)) // .setRule(rule) // .setMessage(message) // @@ -71,4 +75,11 @@ public SEVERITY toSeverity(final String severity) { } return INFO; } + + private static boolean isMsBuildTarget(String sourceName) { + for (final String target : MsBuildTargetFiles) { + if (sourceName.endsWith(target)) return true; + } + return false; + } } diff --git a/src/test/java/se/bjurr/violations/lib/MSBuildLogTest.java b/src/test/java/se/bjurr/violations/lib/MSBuildLogTest.java index 4609a39..270da48 100644 --- a/src/test/java/se/bjurr/violations/lib/MSBuildLogTest.java +++ b/src/test/java/se/bjurr/violations/lib/MSBuildLogTest.java @@ -25,7 +25,7 @@ public void testThatViolationsCanBeParsed() { .violations(); assertThat(actual) // - .hasSize(15); + .hasSize(16); final Violation violation0 = new ArrayList<>(actual).get(0); assertThat(violation0.getMessage()) // @@ -51,4 +51,41 @@ public void testThatViolationsCanBeParsed() { assertThat(violation3.getStartLine()) // .isEqualTo(7); } + + @Test + public void testThatDotnetCoreViolationsCanBeParsed() { + final String rootFolder = getRootFolder(); + + final Set actual = + violationsApi() // + .withPattern(".*/msbuildlog/msbuild\\.sdk-style\\.log$") // + .inFolder(rootFolder) // + .findAll(MSBULDLOG) // + .violations(); + + assertThat(actual) // + .hasSize(2); + + final Violation violation1 = new ArrayList<>(actual).get(0); + assertThat(violation1.getMessage()) // + .isEqualTo( + "Package 'BouncyCastle 1.8.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project."); + assertThat(violation1.getFile()) // + .isEqualTo("C:/Users/aront/RiderProjects/Warning/Warning/Warning.csproj"); + assertThat(violation1.getSeverity()) // + .isEqualTo(WARN); + assertThat(violation1.getRule()) // + .isEqualTo("NU1701"); + + final Violation violation2 = new ArrayList<>(actual).get(1); + assertThat(violation2.getMessage()) // + .isEqualTo( + "There was a mismatch between the processor architecture of the project being built \"AMD64\" and the processor architecture of the reference \"C:\\Users\\aront\\RiderProjects\\Warning\\Dependency\\bin\\Debug\\net7.0\\Dependency.dll\", \"x86\". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project."); + assertThat(violation2.getFile()) // + .isEqualTo("C:/Users/aront/RiderProjects/Warning/Warning/Warning.csproj"); + assertThat(violation2.getSeverity()) // + .isEqualTo(WARN); + assertThat(violation2.getRule()) // + .isEqualTo("MSB3270"); + } } diff --git a/src/test/resources/msbuildlog/msbuild.sdk-style.log b/src/test/resources/msbuildlog/msbuild.sdk-style.log new file mode 100644 index 0000000..e643f4d --- /dev/null +++ b/src/test/resources/msbuildlog/msbuild.sdk-style.log @@ -0,0 +1,18 @@ +MSBuild version 17.6.8+c70978d4d for .NET + Determining projects to restore... +C:\Users\aront\RiderProjects\Warning\Warning\Warning.csproj : warning NU1701: Package 'BouncyCastle 1.8.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project. + All projects are up-to-date for restore. +C:\Users\aront\RiderProjects\Warning\Warning\Warning.csproj : warning NU1701: Package 'BouncyCastle 1.8.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project. + Dependency -> C:\Users\aront\RiderProjects\Warning\Dependency\bin\Debug\net7.0\Dependency.dll +C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2352,5): warning MSB3270: There was a mismatch between the processor architecture of the project being built "AMD64" and the processor architecture of the reference "C:\Users\aront\RiderProjects\Warning\Dependency\bin\Debug\net7.0\Dependency.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. [C:\Users\aront\RiderProjects\Warning\Warning\Warning.csproj] + Warning -> C:\Users\aront\RiderProjects\Warning\Warning\bin\Debug\net7.0\Warning.dll + +Build succeeded. + +C:\Users\aront\RiderProjects\Warning\Warning\Warning.csproj : warning NU1701: Package 'BouncyCastle 1.8.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project. +C:\Users\aront\RiderProjects\Warning\Warning\Warning.csproj : warning NU1701: Package 'BouncyCastle 1.8.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project. +C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2352,5): warning MSB3270: There was a mismatch between the processor architecture of the project being built "AMD64" and the processor architecture of the reference "C:\Users\aront\RiderProjects\Warning\Dependency\bin\Debug\net7.0\Dependency.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. [C:\Users\aront\RiderProjects\Warning\Warning\Warning.csproj] + 3 Warning(s) + 0 Error(s) + +Time Elapsed 00:00:00.85