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 4 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 AnalysisLoggingContext(_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 AnalysisLoggingContext(_nodeLoggingContext.LoggingService, _requestEntry.Request.BuildEventContext),
_requestEntry.RequestConfiguration.ProjectFullPath);
Expand Down
26 changes: 26 additions & 0 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 @@ -20,6 +21,8 @@ internal class BuildCheckBuildEventHandler

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

private bool isRestoring = false;
maridematte marked this conversation as resolved.
Show resolved Hide resolved

internal BuildCheckBuildEventHandler(
IAnalysisContextFactory analyzerContextFactory,
IBuildCheckManager buildCheckManager)
Expand All @@ -29,6 +32,7 @@ internal BuildCheckBuildEventHandler(

_eventHandlers = 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 @@ -45,12 +49,34 @@ internal BuildCheckBuildEventHandler(

public void HandleBuildEvent(BuildEventArgs e)
{
// Skip event handling during restore phase
if (
maridematte marked this conversation as resolved.
Show resolved Hide resolved
isRestoring &&
e.GetType() != typeof(BuildSubmissionStartedEventArgs) &&
e.BuildEventContext is not null)
maridematte marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

if (_eventHandlers.TryGetValue(e.GetType(), out Action<BuildEventArgs>? handler))
{
handler(e);
}
}

private void HandleBuildSubmissionStartedEvent(BuildSubmissionStartedEventArgs eventArgs)
{
if (isRestoring)
{
isRestoring = false;
maridematte marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
eventArgs.GlobalProperties.TryGetValue(MSBuildConstants.MSBuildIsRestoring, out string? restoreProperty);
isRestoring = restoreProperty is not null ? Convert.ToBoolean(restoreProperty) : false;
}
}

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 @@ -274,6 +274,23 @@ public void CustomAnalyzerTest_WithEditorConfig(string analysisCandidate, string
}
}

[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: -nr:False -t:restore -analyze",
out bool success);

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

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