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

Ensure context is not cached before logging info #6437

Merged
merged 3 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the MIT license. See License.txt in the project root for full license information. -->
<Project>
<PropertyGroup>
<VersionPrefix>16.9.0</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<VersionPrefix>16.9.2</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<AssemblyVersion>15.1.0.0</AssemblyVersion>
<PreReleaseVersionLabel>preview</PreReleaseVersionLabel>
<DotNetUseShippingVersions>true</DotNetUseShippingVersions>
Expand Down
51 changes: 51 additions & 0 deletions src/Build.UnitTests/BinaryLogger_Tests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -92,6 +100,49 @@ public void BinaryLoggerShouldNotThrowWhenMetadataCannotBeExpanded()
ObjectModelHelpers.BuildProjectExpectSuccess(project, binaryLogger);
}

/// <summary>
/// Regression test for https://github.com/dotnet/msbuild/issues/6323.
/// </summary>
/// <remarks>
/// This isn't strictly a binlog test, but it fits here because
/// all log event types will be used when the binlog is attached.
/// </remarks>
[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", @"
<Project>
<Target Name='Target2'>
<Exec Command='echo a'/>
</Target>
</Project>");

var entryProject = _env.CreateTestProjectWithFiles("entry.proj", $@"
<Project>
<Target Name='BuildSelf'>
<Message Text='MessageOutputText'/>
<MSBuild Projects='{referenceProject.ProjectFile}' Targets='Target2' />
<MSBuild Projects='{referenceProject.ProjectFile}' Targets='Target2' /><!-- yes, again. That way it's a cached result -->
</Target>
</Project>");

buildManager.Build(new BuildParameters() { Loggers = new ILogger[] { binaryLogger } },
new BuildRequestData(entryProject.ProjectFile, new Dictionary<string, string>(), null, new string[] { "BuildSelf" }, null))
.OverallResult.ShouldBe(BuildResultCode.Success);
}


public void Dispose()
{
Expand Down
8 changes: 7 additions & 1 deletion src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down