diff --git a/eng/Versions.props b/eng/Versions.props index 041ae15c3e2..0b1046359b4 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -2,7 +2,7 @@ - 16.9.0release + 16.9.2release 15.1.0.0 preview true diff --git a/src/Build.UnitTests/BinaryLogger_Tests.cs b/src/Build.UnitTests/BinaryLogger_Tests.cs index ec95009bec1..bc2c161d226 100644 --- a/src/Build.UnitTests/BinaryLogger_Tests.cs +++ b/src/Build.UnitTests/BinaryLogger_Tests.cs @@ -1,5 +1,13 @@ using System; +using System.Collections.Generic; + +using Microsoft.Build.Evaluation; +using Microsoft.Build.Execution; +using Microsoft.Build.Framework; using Microsoft.Build.Logging; + +using Shouldly; + using Xunit; using Xunit.Abstractions; @@ -92,6 +100,49 @@ public void BinaryLoggerShouldNotThrowWhenMetadataCannotBeExpanded() ObjectModelHelpers.BuildProjectExpectSuccess(project, binaryLogger); } + /// + /// Regression test for https://github.com/dotnet/msbuild/issues/6323. + /// + /// + /// This isn't strictly a binlog test, but it fits here because + /// all log event types will be used when the binlog is attached. + /// + [Fact] + public void MessagesCanBeLoggedWhenProjectsAreCached() + { + using var env = TestEnvironment.Create(); + + env.SetEnvironmentVariable("MSBUILDDEBUGFORCECACHING", "1"); + + using var buildManager = new BuildManager(); + + var binaryLogger = new BinaryLogger + { + Parameters = $"LogFile={_logFile}" + }; + + // To trigger #6323, there must be at least two project instances. + var referenceProject = _env.CreateTestProjectWithFiles("reference.proj", @" + + + + + "); + + var entryProject = _env.CreateTestProjectWithFiles("entry.proj", $@" + + + + + + + "); + + buildManager.Build(new BuildParameters() { Loggers = new ILogger[] { binaryLogger } }, + new BuildRequestData(entryProject.ProjectFile, new Dictionary(), null, new string[] { "BuildSelf" }, null)) + .OverallResult.ShouldBe(BuildResultCode.Success); + } + public void Dispose() { diff --git a/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs b/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs index 05c5ae53708..0b55b80359a 100644 --- a/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs +++ b/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs @@ -69,7 +69,13 @@ internal ProjectLoggingContext LogProjectStarted(BuildRequestEntry requestEntry) internal ProjectLoggingContext LogProjectStarted(BuildRequest request, BuildRequestConfiguration configuration) { ErrorUtilities.VerifyThrow(this.IsValid, "Build not started."); - return new ProjectLoggingContext(this, request, configuration.ProjectFullPath, configuration.ToolsVersion, request.ParentBuildEventContext, configuration.Project?.EvaluationId ?? BuildEventContext.InvalidEvaluationId); + + // If we can retrieve the evaluationId from the project, do so. Don't if it's not available or + // if we'd have to retrieve it from the cache in order to access it. + // Order is important here because the Project getter will throw if IsCached. + int evaluationId = (configuration != null && !configuration.IsCached && configuration.Project != null) ? configuration.Project.EvaluationId : BuildEventContext.InvalidEvaluationId; + + return new ProjectLoggingContext(this, request, configuration.ProjectFullPath, configuration.ToolsVersion, request.ParentBuildEventContext, evaluationId); } ///