Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 53 additions & 12 deletions src/Build.UnitTests/Evaluation/Evaluator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4597,7 +4597,7 @@ public void VerifyMSBuildLogsAMessageWhenLocalPropertyCannotOverrideValueOfGloba
public void VerifyPropertyTrackingLoggingDefault()
{
// Having just environment variables defined should default to nothing being logged except one environment variable read.
this.VerifyPropertyTrackingLoggingScenario(
VerifyPropertyTrackingLoggingScenario(
null,
logger =>
{
Expand All @@ -4613,11 +4613,6 @@ public void VerifyPropertyTrackingLoggingDefault()
.EnvironmentVariableName
.ShouldBe("DEFINED_ENVIRONMENT_VARIABLE2");

logger
.AllBuildEvents
.OfType<PropertyReassignmentEventArgs>()
.ShouldBeEmpty();

logger
.AllBuildEvents
.OfType<PropertyInitialValueSetEventArgs>()
Expand All @@ -4628,7 +4623,7 @@ public void VerifyPropertyTrackingLoggingDefault()
[Fact]
public void VerifyPropertyTrackingLoggingPropertyReassignment()
{
this.VerifyPropertyTrackingLoggingScenario(
VerifyPropertyTrackingLoggingScenario(
"1",
logger =>
{
Expand Down Expand Up @@ -4675,11 +4670,6 @@ public void VerifyPropertyTrackingLoggingNone()
.EnvironmentVariableName
.ShouldBe("DEFINED_ENVIRONMENT_VARIABLE2");

logger
.AllBuildEvents
.OfType<PropertyReassignmentEventArgs>()
.ShouldBeEmpty();

logger
.AllBuildEvents
.OfType<PropertyInitialValueSetEventArgs>()
Expand Down Expand Up @@ -4919,6 +4909,57 @@ private void VerifyPropertyTrackingLoggingScenario(string envVarValue, Action<Mo
}
}

/// <summary>
/// Log when a property is being assigned a new value.
/// </summary>
[Fact]
public void VerifyLogPropertyReassignment()
{
string testtargets = ObjectModelHelpers.CleanupFileContents(@"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: using """ literals allows you to use quotations as is without need to escape

<Project xmlns='msbuildnamespace'>
<PropertyGroup>
<Prop>OldValue</Prop>
<Prop>NewValue</Prop>
</PropertyGroup>
<Target Name=""Test""/>
</Project>");

string tempPath = Path.GetTempPath();
string targetDirectory = Path.Combine(tempPath, "LogPropertyAssignments");
string testTargetPath = Path.Combine(targetDirectory, "test.proj");

try
{
Directory.CreateDirectory(targetDirectory);
File.WriteAllText(testTargetPath, testtargets);

MockLogger logger = new()
{
Verbosity = LoggerVerbosity.Diagnostic,
};
ProjectCollection pc = new();
pc.RegisterLogger(logger);
Project project = pc.LoadProject(testTargetPath);

bool result = project.Build();
result.ShouldBeTrue();
logger.BuildMessageEvents
.OfType<PropertyReassignmentEventArgs>()
.ShouldContain(r => r.PropertyName == "Prop"
&& r.PreviousValue == "OldValue"
&& r.NewValue == "NewValue"
&& r.Message.StartsWith("Property reassignment: $(Prop)=\"NewValue\" (previous value: \"OldValue\")"));
logger.BuildMessageEvents.ShouldBeOfTypes(new[] { typeof(PropertyReassignmentEventArgs) });
}
finally
{
if (Directory.Exists(targetDirectory))
{
FileUtilities.DeleteWithoutTrailingBackslash(targetDirectory, true /* recursive delete */);
}
}
}

#if FEATURE_HTTP_LISTENER
/// <summary>
/// HTTP server code running on a separate thread that expects a connection request
Expand Down
14 changes: 9 additions & 5 deletions src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,13 +1348,17 @@ private void LogPropertyReassignment(P predecessor, P property, string location)

if (newValue != oldValue)
{
_evaluationLoggingContext.LogComment(
MessageImportance.Low,
"PropertyReassignment",
var args = new PropertyReassignmentEventArgs(
property.Name,
newValue,
oldValue,
location);
newValue,
location,
message: null)
{
BuildEventContext = _evaluationLoggingContext.BuildEventContext,
};

_evaluationLoggingContext.LogBuildEvent(args);
}
}

Expand Down