Skip to content

Commit

Permalink
use AnalyzerLoggingContext instead of LoggingService in BuildCheckMan…
Browse files Browse the repository at this point in the history
…ager
  • Loading branch information
surayya-MS committed Jun 11, 2024
1 parent bc63e18 commit e47a8ff
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/Build/BackEnd/Components/RequestBuilder/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Experimental.BuildCheck.Infrastructure;
using Microsoft.Build.Collections;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Eventing;
using Microsoft.Build.Exceptions;
using Microsoft.Build.Execution;
using Microsoft.Build.Experimental.BuildCheck;
using Microsoft.Build.Experimental.BuildCheck.Infrastructure;
using Microsoft.Build.Experimental.BuildCheck.Logging;
using Microsoft.Build.Framework;
using Microsoft.Build.Internal;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -1123,7 +1124,7 @@ private async Task<BuildResult> BuildProject()
{
buildCheckManager.StartProjectEvaluation(
BuildCheckDataSource.BuildExecution,
_requestEntry.Request.ParentBuildEventContext,
new AnalyzerLoggingContext(_nodeLoggingContext.LoggingService, _requestEntry.Request.ParentBuildEventContext),
_requestEntry.RequestConfiguration.ProjectFullPath);

_requestEntry.RequestConfiguration.LoadProjectIntoConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private void HandleProjectEvaluationStartedEvent(ProjectEvaluationStartedEventAr
{
if (!IsMetaProjFile(eventArgs.ProjectFile))
{
_buildCheckManager.StartProjectEvaluation(BuildCheckDataSource.EventArgs, eventArgs.BuildEventContext!, eventArgs.ProjectFile!);
_buildCheckManager.StartProjectEvaluation(
BuildCheckDataSource.EventArgs,
_loggingContextFactory.CreateLoggingContext(eventArgs.BuildEventContext!),
eventArgs.ProjectFile!);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/Build/BuildCheck/Infrastructure/BuildCheckManagerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
using System.Threading;
using Microsoft.Build.BackEnd;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Experimental.BuildCheck;
using Microsoft.Build.Experimental.BuildCheck.Acquisition;
using Microsoft.Build.Experimental.BuildCheck.Analyzers;
using Microsoft.Build.Experimental.BuildCheck.Logging;
using Microsoft.Build.Experimental.BuildCheck;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void InitializeComponent(IBuildComponentHost host)
IBuildCheckManager instance;
if (host!.BuildParameters.IsBuildCheckEnabled)
{
instance = new BuildCheckManager(host.LoggingService);
instance = new BuildCheckManager();
}
else
{
Expand All @@ -66,17 +66,15 @@ internal sealed class BuildCheckManager : IBuildCheckManager
private readonly TracingReporter _tracingReporter = new TracingReporter();
private readonly ConfigurationProvider _configurationProvider = new ConfigurationProvider();
private readonly BuildCheckCentralContext _buildCheckCentralContext;
private readonly ILoggingService _loggingService;
private readonly List<BuildAnalyzerFactoryContext> _analyzersRegistry;
private readonly bool[] _enabledDataSources = new bool[(int)BuildCheckDataSource.ValuesCount];
private readonly BuildEventsProcessor _buildEventsProcessor;
private readonly IBuildCheckAcquisitionModule _acquisitionModule;

internal BuildCheckManager(ILoggingService loggingService)
internal BuildCheckManager()
{
_analyzersRegistry = new List<BuildAnalyzerFactoryContext>();
_acquisitionModule = new BuildCheckAcquisitionModule();
_loggingService = loggingService;
_buildCheckCentralContext = new(_configurationProvider);
_buildEventsProcessor = new(_buildCheckCentralContext);
}
Expand Down Expand Up @@ -205,7 +203,7 @@ internal void RegisterCustomAnalyzer(
}
}

private void SetupSingleAnalyzer(BuildAnalyzerFactoryContext analyzerFactoryContext, string projectFullPath, BuildEventContext buildEventContext)
private void SetupSingleAnalyzer(BuildAnalyzerFactoryContext analyzerFactoryContext, string projectFullPath)
{
// For custom analyzers - it should run only on projects where referenced
// (otherwise error out - https://github.com/orgs/dotnet/projects/373/views/1?pane=issue&itemId=57849480)
Expand Down Expand Up @@ -283,7 +281,7 @@ private void SetupSingleAnalyzer(BuildAnalyzerFactoryContext analyzerFactoryCont
}
}

private void SetupAnalyzersForNewProject(string projectFullPath, BuildEventContext buildEventContext)
private void SetupAnalyzersForNewProject(string projectFullPath, AnalyzerLoggingContext loggingContext)
{
// Only add analyzers here
// On an execution node - we might remove and dispose the analyzers once project is done
Expand All @@ -295,11 +293,11 @@ private void SetupAnalyzersForNewProject(string projectFullPath, BuildEventConte
{
try
{
SetupSingleAnalyzer(analyzerFactoryContext, projectFullPath, buildEventContext);
SetupSingleAnalyzer(analyzerFactoryContext, projectFullPath);
}
catch (BuildCheckConfigurationException e)
{
_loggingService.LogErrorFromText(buildEventContext, null, null, null,
loggingContext.LogErrorFromText(null, null, null,
new BuildEventFileInfo(projectFullPath),
e.Message);
analyzersToRemove.Add(analyzerFactoryContext);
Expand All @@ -309,7 +307,7 @@ private void SetupAnalyzersForNewProject(string projectFullPath, BuildEventConte
analyzersToRemove.ForEach(c =>
{
_analyzersRegistry.Remove(c);
_loggingService.LogCommentFromText(buildEventContext, MessageImportance.High, $"Dismounting analyzer '{c.FriendlyName}'");
loggingContext.LogCommentFromText(MessageImportance.High, $"Dismounting analyzer '{c.FriendlyName}'");
});
foreach (var analyzerToRemove in analyzersToRemove.Select(a => a.MaterializedAnalyzer).Where(a => a != null))
{
Expand Down Expand Up @@ -377,7 +375,9 @@ public void FinalizeProcessing(LoggingContext loggingContext)
loggingContext.LogBuildEvent(analyzerEventArg);
}

public void StartProjectEvaluation(BuildCheckDataSource buildCheckDataSource, BuildEventContext buildEventContext,
public void StartProjectEvaluation(
BuildCheckDataSource buildCheckDataSource,
AnalyzerLoggingContext loggingContext,
string fullPath)
{
if (buildCheckDataSource == BuildCheckDataSource.EventArgs && IsInProcNode)
Expand All @@ -388,7 +388,7 @@ public void StartProjectEvaluation(BuildCheckDataSource buildCheckDataSource, Bu
return;
}

SetupAnalyzersForNewProject(fullPath, buildEventContext);
SetupAnalyzersForNewProject(fullPath, loggingContext);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/Build/BuildCheck/Infrastructure/IBuildCheckManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void ProcessTaskParameterEventArgs(
// but as well from the ConnectorLogger - as even if interleaved, it gives the info
// to manager about what analyzers need to be materialized and configuration fetched.
// No unloading of analyzers is yet considered - once loaded it stays for whole build.
void StartProjectEvaluation(BuildCheckDataSource buildCheckDataSource, BuildEventContext buildEventContext, string fullPath);
void StartProjectEvaluation(BuildCheckDataSource buildCheckDataSource, AnalyzerLoggingContext loggingContext, string fullPath);

void EndProjectEvaluation(BuildCheckDataSource buildCheckDataSource, BuildEventContext buildEventContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void FinalizeProcessing(LoggingContext loggingContext)
{
}

public void StartProjectEvaluation(BuildCheckDataSource buildCheckDataSource, BuildEventContext buildEventContext, string fullPath)
public void StartProjectEvaluation(BuildCheckDataSource buildCheckDataSource, AnalyzerLoggingContext loggingContext, string fullPath)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/BuildCheck.UnitTests/BuildCheckManagerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BuildCheckManagerTests(ITestOutputHelper output)
_loggingService = LoggingService.CreateLoggingService(LoggerMode.Synchronous, 1);
_logger = new MockLogger();
_loggingService.RegisterLogger(_logger);
_testedInstance = new BuildCheckManager(_loggingService);
_testedInstance = new BuildCheckManager();
}

[Theory]
Expand Down

0 comments on commit e47a8ff

Please sign in to comment.