Skip to content

Commit

Permalink
Always send project evaluation finished Fixes #10113 (#10365)
Browse files Browse the repository at this point in the history
Fixes #10113

Context
Evaluating a project is supposed to send a ProjectEvaluationFinished event. If it fails, however, it doesn't.

Changes Made
Wrap Evaluate in a try/finally block to ensure ProjectEvaluationFinished is always sent

Testing
Created a unit test
  • Loading branch information
Forgind authored Jul 18, 2024
1 parent 47f364c commit 7b1e619
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/Build.UnitTests/Evaluation/Evaluator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ public void Dispose()
GC.Collect();
}

[Fact]
public void EnsureProjectEvaluationFinishedIsLogged()
{
using TestEnvironment env = TestEnvironment.Create();
TransientTestFile projectFile = env.CreateFile("project.proj", $@"
<Project Sdk=""Microsoft.NETT.Sdk"">
<Target Name=""DefaultTarget"">
</Target>
</Project>
");

MockLogger logger = new();
using ProjectCollection collection = new(new Dictionary<string, string>(), [logger], ToolsetDefinitionLocations.Default);
Assert.Throws<InvalidProjectFileException>(() => collection.LoadProject(projectFile.Path));
logger.EvaluationFinishedEvents.ShouldNotBeEmpty();
}

[Theory]
[MemberData(nameof(ImportLoadingScenarioTestData))]
public void VerifyLoadingImportScenarios(string importParameter, bool shouldSucceed)
Expand Down
34 changes: 20 additions & 14 deletions src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,26 @@ internal static void Evaluate(
loggingService,
buildEventContext);

evaluator.Evaluate();
try
{
evaluator.Evaluate();
}
finally
{
IEnumerable globalProperties = null;
IEnumerable properties = null;
IEnumerable items = null;

if (evaluator._evaluationLoggingContext.LoggingService.IncludeEvaluationPropertiesAndItems)
{
globalProperties = evaluator._data.GlobalPropertiesDictionary;
properties = Traits.LogAllEnvironmentVariables ? evaluator._data.Properties : evaluator.FilterOutEnvironmentDerivedProperties(evaluator._data.Properties);
items = evaluator._data.Items;
}

evaluator._evaluationLoggingContext.LogProjectEvaluationFinished(globalProperties, properties, items, evaluator._evaluationProfiler.ProfiledResult);
}

MSBuildEventSource.Log.EvaluateStop(root.ProjectFileLocation.File);
}

Expand Down Expand Up @@ -798,19 +817,6 @@ private void Evaluate()
}

ErrorUtilities.VerifyThrow(_evaluationProfiler.IsEmpty(), "Evaluation profiler stack is not empty.");

IEnumerable globalProperties = null;
IEnumerable properties = null;
IEnumerable items = null;

if (this._evaluationLoggingContext.LoggingService.IncludeEvaluationPropertiesAndItems)
{
globalProperties = _data.GlobalPropertiesDictionary;
properties = Traits.LogAllEnvironmentVariables ? _data.Properties : FilterOutEnvironmentDerivedProperties(_data.Properties);
items = _data.Items;
}

_evaluationLoggingContext.LogProjectEvaluationFinished(globalProperties, properties, items, _evaluationProfiler.ProfiledResult);
}

private IEnumerable FilterOutEnvironmentDerivedProperties(PropertyDictionary<P> dictionary)
Expand Down

0 comments on commit 7b1e619

Please sign in to comment.