diff --git a/src/Buildalyzer/Logging/EventProcessor.cs b/src/Buildalyzer/Logging/EventProcessor.cs index 0a63c4c6..6d16c264 100644 --- a/src/Buildalyzer/Logging/EventProcessor.cs +++ b/src/Buildalyzer/Logging/EventProcessor.cs @@ -63,6 +63,8 @@ public EventProcessor(AnalyzerManager manager, ProjectAnalyzer analyzer, IEnumer } // In binlog 14 we need to gather properties and items during evaluation and "glue" them with the project event args + // But can never remove ProjectStarted: "even v14 will log them on ProjectStarted if any legacy loggers are present (for compat)" + // See https://twitter.com/KirillOsenkov/status/1427686459713019904 private void StatusEventRaised(object sender, BuildStatusEventArgs e) { // Needed to add an extern alias, see https://github.com/KirillOsenkov/MSBuildStructuredLog/issues/521 @@ -160,19 +162,24 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e) private void MessageRaised(object sender, BuildMessageEventArgs e) { - // Process the command line arguments for the Fsc task AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek(); - if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true && !string.IsNullOrWhiteSpace(e.Message) && _targetStack.Any(x => x.TargetName == "CoreCompile") && _currentResult.Count != 0 && !result.HasFscArguments()) + if (result is object) { - result.ProcessFscCommandLine(e.Message); - } + // Process the command line arguments for the Fsc task + if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true + && !string.IsNullOrWhiteSpace(e.Message) + && _targetStack.Any(x => x.TargetName == "CoreCompile") + && !result.HasFscArguments()) + { + result.ProcessFscCommandLine(e.Message); + } - // Process the command line arguments for the Csc task - if (result != null - && e is TaskCommandLineEventArgs cmd - && string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase)) - { - result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile")); + // Process the command line arguments for the Csc task + if (e is TaskCommandLineEventArgs cmd + && string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase)) + { + result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile")); + } } } diff --git a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs index 44845a25..69505fc9 100644 --- a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs +++ b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs @@ -560,55 +560,57 @@ public void BuildsLotsOfProjects() analyzerResults.Count.ShouldBe(50); } - [Test] - public void GetsSourceFilesFromVersion14BinLog() + // To produce different versions, create a global.json and then run `dotnet clean` and `dotnet build -bl:SdkNetCore31Project-vX.binlog` from the source project folder + [TestCase("SdkNetCore31Project-v9.binlog", 9)] + [TestCase("SdkNetCore31Project-v14.binlog", 14)] + public void GetsSourceFilesFromBinLogFile(string path, int expectedVersion) { - // Given - StringWriter log = new StringWriter(); - IProjectAnalyzer analyzer = GetProjectAnalyzer( - @"SdkNetCore31Project\SdkNetCore31Project.csproj", - log); - string binLogPath = Path.ChangeExtension(Path.GetTempFileName(), ".binlog"); + // Verify this is the expected version + path = Path.GetFullPath( + Path.Combine( + Path.GetDirectoryName(typeof(SimpleProjectsFixture).Assembly.Location), + "..", + "..", + "..", + "..", + "binlogs", + path)) + .Replace('\\', Path.DirectorySeparatorChar); EnvironmentOptions options = new EnvironmentOptions(); - options.Arguments.Add("/bl:" + binLogPath); // Tell MSBuild to produce the binlog so we use the latest internal logger - - try + using (Stream stream = File.OpenRead(path)) { - // When - analyzer.Build(options); - using (Stream stream = File.OpenRead(binLogPath)) + using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress)) { - using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress)) + using (BinaryReader reader = new BinaryReader(gzip)) { - using (BinaryReader reader = new BinaryReader(gzip)) - { - // Verify this produced a version 14 binlog - reader.ReadInt32().ShouldBe(14); - } + reader.ReadInt32().ShouldBe(expectedVersion); } } - IAnalyzerResults analyzerResults = analyzer.Manager.Analyze(binLogPath); - IReadOnlyList sourceFiles = analyzerResults.First().SourceFiles; + } - // Then - sourceFiles.ShouldNotBeNull(log.ToString()); - new[] + // Given + StringWriter log = new StringWriter(); + AnalyzerManager analyzerManager = new AnalyzerManager( + new AnalyzerManagerOptions { + LogWriter = log + }); + + // When + IAnalyzerResults analyzerResults = analyzerManager.Analyze(path); + IReadOnlyList sourceFiles = analyzerResults.First().SourceFiles; + + // Then + sourceFiles.ShouldNotBeNull(log.ToString()); + new[] + { #if Is_Windows - // Linux and Mac builds appear to omit the AssemblyAttributes.cs file - "AssemblyAttributes", + // Linux and Mac builds appear to omit the AssemblyAttributes.cs file + "AssemblyAttributes", #endif - "Class1", - "AssemblyInfo" - }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString()); - } - finally - { - if (File.Exists(binLogPath)) - { - File.Delete(binLogPath); - } - } + "Class1", + "AssemblyInfo" + }.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString()); } private static IProjectAnalyzer GetProjectAnalyzer(string projectFile, StringWriter log) diff --git a/tests/binlogs/SdkNetCore31Project-v14.binlog b/tests/binlogs/SdkNetCore31Project-v14.binlog new file mode 100644 index 00000000..606ebcba Binary files /dev/null and b/tests/binlogs/SdkNetCore31Project-v14.binlog differ diff --git a/tests/binlogs/SdkNetCore31Project-v9.binlog b/tests/binlogs/SdkNetCore31Project-v9.binlog new file mode 100644 index 00000000..e63dfd6e Binary files /dev/null and b/tests/binlogs/SdkNetCore31Project-v9.binlog differ