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 evaluation results are included in binlog #70472

Merged
merged 4 commits into from
Oct 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ private ImmutableDictionary<string, string> AllGlobalProperties
}
else
{
var projectCollection = new MSB.Evaluation.ProjectCollection(AllGlobalProperties);
var projectCollection = new MSB.Evaluation.ProjectCollection(
jasonmalinowski marked this conversation as resolved.
Show resolved Hide resolved
AllGlobalProperties,
_msbuildLogger != null ? ImmutableArray.Create(_msbuildLogger) : ImmutableArray<MSB.Framework.ILogger>.Empty,
MSB.Evaluation.ToolsetDefinitionLocations.Default);
try
{
return LoadProjectAsync(path, projectCollection, cancellationToken);
Expand Down Expand Up @@ -158,18 +161,27 @@ public void StartBatchBuild(IDictionary<string, string>? globalProperties = null

globalProperties ??= ImmutableDictionary<string, string>.Empty;
var allProperties = s_defaultGlobalProperties.RemoveRange(globalProperties.Keys).AddRange(globalProperties);
_batchBuildProjectCollection = new MSB.Evaluation.ProjectCollection(allProperties);

_batchBuildLogger = new MSBuildDiagnosticLogger()
{
Verbosity = MSB.Framework.LoggerVerbosity.Normal
};

// Pass in the binlog (if any) to the ProjectCollection to ensure evaluation results are included in it.
//
// We do not need to include the _batchBuildLogger in the ProjectCollection - it just collects the
// DiagnosticLog from the build steps, but evaluation already separately reports the DiagnosticLog.
var loggers = _msbuildLogger is not null
? ImmutableArray.Create(_msbuildLogger)
: ImmutableArray<MSB.Framework.ILogger>.Empty;

_batchBuildProjectCollection = new MSB.Evaluation.ProjectCollection(allProperties, loggers, MSB.Evaluation.ToolsetDefinitionLocations.Default);
dibarbet marked this conversation as resolved.
Show resolved Hide resolved

var buildParameters = new MSB.Execution.BuildParameters(_batchBuildProjectCollection)
{
Loggers = _msbuildLogger is null
? (new MSB.Framework.ILogger[] { _batchBuildLogger })
: (new MSB.Framework.ILogger[] { _batchBuildLogger, _msbuildLogger }),

// The loggers are not inherited from the project collection, so specify both the
// binlog logger and the _batchBuildLogger for the build steps.
Loggers = loggers.Add(_batchBuildLogger),
// If we have an additional logger and it's diagnostic, then we need to opt into task inputs globally, or otherwise
// it won't get any log events. This logic matches https://github.com/dotnet/msbuild/blob/fa6710d2720dcf1230a732a8858ffe71bcdbe110/src/Build/Instance/ProjectInstance.cs#L2365-L2371
LogTaskInputs = _msbuildLogger is not null && _msbuildLogger.Verbosity == LoggerVerbosity.Diagnostic
Expand Down
63 changes: 63 additions & 0 deletions src/Workspaces/MSBuildTest/NetCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.UnitTests;
using Microsoft.CodeAnalysis.UnitTests.TestFiles;
Expand Down Expand Up @@ -495,5 +498,65 @@ static void AssertSingleProjectReference(Project project, string projectRefFileP
Assert.Equal(projectRefFilePath, project.Solution.GetProject(projectRefId).FilePath);
}
}

[ConditionalFact(typeof(DotNetSdkMSBuildInstalled))]
[Trait(Traits.Feature, Traits.Features.MSBuildWorkspace)]
[Trait(Traits.Feature, Traits.Features.NetCore)]
public async Task TestOpenProject_LogsEvaluationAndBuild()
{
CreateFiles(GetNetCoreAppFiles());

var projectFilePath = GetSolutionFileName("Project.csproj");

DotNetRestore("Project.csproj");

using var workspace = CreateMSBuildWorkspace();

var loader = workspace.Services
.GetLanguageServices(LanguageNames.CSharp)
.GetRequiredService<IProjectFileLoader>();

var logger = new TestMSBuildLogger();
var buildManager = new ProjectBuildManager(ImmutableDictionary<string, string>.Empty, logger);
buildManager.StartBatchBuild();

var projectFile = await loader.LoadProjectFileAsync(projectFilePath, buildManager, CancellationToken.None);
var projectFileInfo = (await projectFile.GetProjectFileInfosAsync(CancellationToken.None)).Single();
buildManager.EndBatchBuild();

Assert.True(logger.WasInitialized);
var logLines = logger.GetLogLines();
Assert.StartsWith("Build started", logLines.First());
Assert.StartsWith("Evaluation started", logLines[1]);
Assert.Single(logLines.Where(line => line.StartsWith("Evaluation finished")));
Assert.StartsWith("Build succeeded", logLines.Last());
}

[ConditionalFact(typeof(DotNetSdkMSBuildInstalled))]
[Trait(Traits.Feature, Traits.Features.MSBuildWorkspace)]
[Trait(Traits.Feature, Traits.Features.NetCore)]
public async Task TestOpenProject_LogsEvaluationOnly()
{
CreateFiles(GetNetCoreAppFiles());

var projectFilePath = GetSolutionFileName("Project.csproj");

DotNetRestore("Project.csproj");

using var workspace = CreateMSBuildWorkspace();

var loader = workspace.Services
.GetLanguageServices(LanguageNames.CSharp)
.GetRequiredService<IProjectFileLoader>();

var logger = new TestMSBuildLogger();
var buildManager = new ProjectBuildManager(ImmutableDictionary<string, string>.Empty, logger);
var projectFile = await loader.LoadProjectFileAsync(projectFilePath, buildManager, CancellationToken.None);

Assert.True(logger.WasInitialized);
var logLines = logger.GetLogLines();
Assert.StartsWith("Evaluation started", logLines.First());
Assert.StartsWith("Evaluation finished", logLines.Last());
}
}
}
38 changes: 38 additions & 0 deletions src/Workspaces/MSBuildTest/Utilities/TestMSBuildLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Roslyn.Utilities;
internal class TestMSBuildLogger : ILogger
{
public LoggerVerbosity Verbosity { get; set; }
public string Parameters { get; set; } = string.Empty;

public bool WasInitialized = false;

private readonly List<string> _logLines = new List<string>();

public void Initialize(IEventSource eventSource)
{
WasInitialized = true;
eventSource.AnyEventRaised += EventSource_AnyEventRaised;
}

public void Shutdown()
{
}

public List<string> GetLogLines()
{
Contract.ThrowIfFalse(WasInitialized);
return _logLines;
}

private void EventSource_AnyEventRaised(object sender, BuildEventArgs e)
{
_logLines.Add(e.Message);
}
}
50 changes: 50 additions & 0 deletions src/Workspaces/MSBuildTest/VisualStudioMSBuildWorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3073,6 +3073,56 @@ public async Task TestOpenProject_CommandLineArgsHaveNoErrors()
Assert.Empty(commandLineArgs.Errors);
}

[ConditionalFact(typeof(VisualStudioMSBuildInstalled))]
public async Task TestOpenProject_LogsEvaluationAndBuild()
{
CreateFiles(GetSimpleCSharpSolutionFiles());

using var workspace = CreateMSBuildWorkspace();
var loader = workspace.Services
.GetLanguageServices(LanguageNames.CSharp)
.GetRequiredService<IProjectFileLoader>();

var projectFilePath = GetSolutionFileName(@"CSharpProject\CSharpProject.csproj");

var logger = new TestMSBuildLogger();
var buildManager = new ProjectBuildManager(ImmutableDictionary<string, string>.Empty, logger);
buildManager.StartBatchBuild();

var projectFile = await loader.LoadProjectFileAsync(projectFilePath, buildManager, CancellationToken.None);
var projectFileInfo = (await projectFile.GetProjectFileInfosAsync(CancellationToken.None)).Single();
buildManager.EndBatchBuild();

Assert.True(logger.WasInitialized);
var logLines = logger.GetLogLines();
Assert.StartsWith("Build started", logLines.First());
Assert.StartsWith("Evaluation started", logLines[1]);
Assert.Single(logLines.Where(line => line.StartsWith("Evaluation finished")));
Assert.StartsWith("Build succeeded", logLines.Last());
}

[ConditionalFact(typeof(VisualStudioMSBuildInstalled))]
public async Task TestOpenProject_LogsEvaluationOnly()
{
CreateFiles(GetSimpleCSharpSolutionFiles());

using var workspace = CreateMSBuildWorkspace();
var loader = workspace.Services
.GetLanguageServices(LanguageNames.CSharp)
.GetRequiredService<IProjectFileLoader>();

var projectFilePath = GetSolutionFileName(@"CSharpProject\CSharpProject.csproj");

var logger = new TestMSBuildLogger();
var buildManager = new ProjectBuildManager(ImmutableDictionary<string, string>.Empty, logger);
var projectFile = await loader.LoadProjectFileAsync(projectFilePath, buildManager, CancellationToken.None);

Assert.True(logger.WasInitialized);
var logLines = logger.GetLogLines();
Assert.StartsWith("Evaluation started", logLines.First());
Assert.StartsWith("Evaluation finished", logLines.Last());
}

[ConditionalFact(typeof(VisualStudioMSBuildInstalled))]
[WorkItem("https://github.com/dotnet/roslyn/issues/29122")]
public async Task TestOpenSolution_ProjectReferencesWithUnconventionalOutputPaths()
Expand Down