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

Skip BuildCheck on restore #10500

Merged
merged 9 commits into from
Aug 9, 2024
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
16 changes: 10 additions & 6 deletions src/Build/BackEnd/Components/RequestBuilder/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,9 +1105,13 @@ private async Task<BuildResult> BuildProject()
ErrorUtilities.VerifyThrow(_targetBuilder != null, "Target builder is null");

// We consider this the entrypoint for the project build for purposes of BuildCheck processing
bool isRestoring = _requestEntry.RequestConfiguration.GlobalProperties[MSBuildConstants.MSBuildIsRestoring] is null;

var buildCheckManager = (_componentHost.GetComponent(BuildComponentType.BuildCheckManagerProvider) as IBuildCheckManagerProvider)!.Instance;
buildCheckManager.SetDataSource(BuildCheckDataSource.BuildExecution);
var buildCheckManager = isRestoring
? (_componentHost.GetComponent(BuildComponentType.BuildCheckManagerProvider) as IBuildCheckManagerProvider)!.Instance
: null;

buildCheckManager?.SetDataSource(BuildCheckDataSource.BuildExecution);

// Make sure it is null before loading the configuration into the request, because if there is a problem
// we do not wand to have an invalid projectLoggingContext floating around. Also if this is null the error will be
Expand All @@ -1121,7 +1125,7 @@ private async Task<BuildResult> BuildProject()
// Load the project
if (!_requestEntry.RequestConfiguration.IsLoaded)
{
buildCheckManager.StartProjectEvaluation(
buildCheckManager?.StartProjectEvaluation(
BuildCheckDataSource.BuildExecution,
new CheckLoggingContext(_nodeLoggingContext.LoggingService, _requestEntry.Request.BuildEventContext),
_requestEntry.RequestConfiguration.ProjectFullPath);
Expand All @@ -1146,13 +1150,13 @@ private async Task<BuildResult> BuildProject()
}
finally
{
buildCheckManager.EndProjectEvaluation(
buildCheckManager?.EndProjectEvaluation(
BuildCheckDataSource.BuildExecution,
_requestEntry.Request.BuildEventContext);
}

_projectLoggingContext = _nodeLoggingContext.LogProjectStarted(_requestEntry);
buildCheckManager.StartProjectRequest(
buildCheckManager?.StartProjectRequest(
BuildCheckDataSource.BuildExecution,
_requestEntry.Request.BuildEventContext,
_requestEntry.RequestConfiguration.ProjectFullPath);
Expand Down Expand Up @@ -1224,7 +1228,7 @@ private async Task<BuildResult> BuildProject()
}
finally
{
buildCheckManager.EndProjectRequest(
buildCheckManager?.EndProjectRequest(
BuildCheckDataSource.BuildExecution,
new CheckLoggingContext(_nodeLoggingContext.LoggingService, _requestEntry.Request.BuildEventContext),
_requestEntry.RequestConfiguration.ProjectFullPath);
Expand Down
24 changes: 22 additions & 2 deletions src/Build/BuildCheck/Infrastructure/BuildCheckBuildEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Build.Experimental.BuildCheck.Acquisition;
using Microsoft.Build.Experimental.BuildCheck.Utilities;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;

namespace Microsoft.Build.Experimental.BuildCheck.Infrastructure;

Expand All @@ -18,7 +19,9 @@ internal class BuildCheckBuildEventHandler
private readonly IBuildCheckManager _buildCheckManager;
private readonly ICheckContextFactory _checkContextFactory;

private readonly Dictionary<Type, Action<BuildEventArgs>> _eventHandlers;
private Dictionary<Type, Action<BuildEventArgs>> _eventHandlers;
private readonly Dictionary<Type, Action<BuildEventArgs>> _eventHandlersFull;
private readonly Dictionary<Type, Action<BuildEventArgs>> _eventHandlersRestore;

internal BuildCheckBuildEventHandler(
ICheckContextFactory checkContextFactory,
Expand All @@ -27,8 +30,9 @@ internal BuildCheckBuildEventHandler(
_buildCheckManager = buildCheckManager;
_checkContextFactory = checkContextFactory;

_eventHandlers = new()
_eventHandlersFull = new()
{
{ typeof(BuildSubmissionStartedEventArgs), (BuildEventArgs e) => HandleBuildSubmissionStartedEvent((BuildSubmissionStartedEventArgs)e) },
{ typeof(ProjectEvaluationFinishedEventArgs), (BuildEventArgs e) => HandleProjectEvaluationFinishedEvent((ProjectEvaluationFinishedEventArgs)e) },
{ typeof(ProjectEvaluationStartedEventArgs), (BuildEventArgs e) => HandleProjectEvaluationStartedEvent((ProjectEvaluationStartedEventArgs)e) },
{ typeof(EnvironmentVariableReadEventArgs), (BuildEventArgs e) => HandleEnvironmentVariableReadEvent((EnvironmentVariableReadEventArgs)e) },
Expand All @@ -41,6 +45,14 @@ internal BuildCheckBuildEventHandler(
{ typeof(TaskParameterEventArgs), (BuildEventArgs e) => HandleTaskParameterEvent((TaskParameterEventArgs)e) },
{ typeof(BuildFinishedEventArgs), (BuildEventArgs e) => HandleBuildFinishedEvent((BuildFinishedEventArgs)e) },
};

// During restore we'll wait only for restore to be done.
_eventHandlersRestore = new()
{
{ typeof(BuildSubmissionStartedEventArgs), (BuildEventArgs e) => HandleBuildSubmissionStartedEvent((BuildSubmissionStartedEventArgs)e) },
};

_eventHandlers = _eventHandlersFull;
}

public void HandleBuildEvent(BuildEventArgs e)
Expand All @@ -51,6 +63,14 @@ public void HandleBuildEvent(BuildEventArgs e)
}
}

private void HandleBuildSubmissionStartedEvent(BuildSubmissionStartedEventArgs eventArgs)
{
eventArgs.GlobalProperties.TryGetValue(MSBuildConstants.MSBuildIsRestoring, out string? restoreProperty);
bool isRestoring = restoreProperty is not null && Convert.ToBoolean(restoreProperty);

_eventHandlers = isRestoring ? _eventHandlersRestore : _eventHandlersFull;
}

private void HandleProjectEvaluationFinishedEvent(ProjectEvaluationFinishedEventArgs eventArgs)
{
if (!IsMetaProjFile(eventArgs.ProjectFile))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal class BuildCheckForwardingLogger : IForwardingLogger
private HashSet<Type> _eventsToForward = new HashSet<Type>
{
typeof(EnvironmentVariableReadEventArgs),
typeof(BuildSubmissionStartedEventArgs),
typeof(ProjectEvaluationFinishedEventArgs),
typeof(ProjectEvaluationStartedEventArgs),
typeof(ProjectStartedEventArgs),
Expand Down
17 changes: 17 additions & 0 deletions src/BuildCheck.UnitTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,23 @@ public void CustomCheckTest_WithEditorConfig(string checkCandidate, string ruleI
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void DoesNotRunOnRestore(bool buildInOutOfProcessNode)
{
PrepareSampleProjectsAndConfig(buildInOutOfProcessNode, out TransientTestFile projectFile, new List<(string, string)>() { ("BC0101", "warning") });

string output = RunnerUtilities.ExecBootstrapedMSBuild(
$"{Path.GetFileName(projectFile.Path)} /m:1 -nr:False -t:restore -check",
out bool success);

success.ShouldBeTrue();
output.ShouldNotContain("BC0101");
output.ShouldNotContain("BC0102");
output.ShouldNotContain("BC0103");
}

private void AddCustomDataSourceToNugetConfig(string checkCandidatePath)
{
var nugetTemplatePath = Path.Combine(checkCandidatePath, "nugetTemplate.config");
Expand Down
Loading