Skip to content

Commit

Permalink
fix: allow MSBuild in Program Files (x86) and support NU1701 (refs #182)
Browse files Browse the repository at this point in the history
  • Loading branch information
arontsang authored and tomasbjerre committed Sep 17, 2023
1 parent b4bb021 commit 7b19453
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
45 changes: 28 additions & 17 deletions src/main/java/se/bjurr/violations/lib/parsers/MSBuildLogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Violation> parseReportOutput(
final String reportContent, final ViolationsLogger violationsLogger) throws Exception {
final Set<Violation> violations = new TreeSet<>();
final List<List<String>> partsPerLine =
getLines(
reportContent,
"^\\s*([^\\(]*)\\(([^,]*),([^\\)]*)\\):\\s([^\\s]*)\\s([^:]*):([^\\[]*).*$");
final List<List<String>> partsPerLine = getLines(reportContent, ParsingRegex);
for (final List<String> 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) //
Expand All @@ -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;
}
}
39 changes: 38 additions & 1 deletion src/test/java/se/bjurr/violations/lib/MSBuildLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) //
Expand All @@ -51,4 +51,41 @@ public void testThatViolationsCanBeParsed() {
assertThat(violation3.getStartLine()) //
.isEqualTo(7);
}

@Test
public void testThatDotnetCoreViolationsCanBeParsed() {
final String rootFolder = getRootFolder();

final Set<Violation> 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");
}
}
18 changes: 18 additions & 0 deletions src/test/resources/msbuildlog/msbuild.sdk-style.log
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7b19453

Please sign in to comment.