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

Extend property assignment event (The feature is opted-out by default) #11106

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
6 changes: 4 additions & 2 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,10 @@ public void RoundTripPropertyReassignmentEventArgs()
propertyName: "a",
previousValue: "b",
newValue: "c",
location: "d",
message: "Property reassignment: $(a)=\"c\" (previous value: \"b\") at d",
file: "file.cs",
line: 10,
column: 20,
message: "Property reassignment: $(a)=\"c\" (previous value: \"b\") at file.cs (10,20)",
helpKeyword: "e",
senderName: "f");

Expand Down
34 changes: 17 additions & 17 deletions src/Build.UnitTests/Evaluation/Evaluator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4708,7 +4708,7 @@ public void VerifyPropertyTrackingLoggingDefault()
// Having just environment variables defined should default to nothing being logged except one environment variable read.
VerifyPropertyTrackingLoggingScenario(
null,
logger =>
(logger, _) =>
{
logger
.AllBuildEvents
Expand Down Expand Up @@ -4740,7 +4740,7 @@ public void VerifyPropertyTrackingLoggingPropertyReassignment()
{
VerifyPropertyTrackingLoggingScenario(
"1",
logger =>
(logger, _) =>
{
logger
.AllBuildEvents
Expand Down Expand Up @@ -4771,7 +4771,7 @@ public void VerifyPropertyTrackingLoggingNone()
{
this.VerifyPropertyTrackingLoggingScenario(
"0",
logger =>
(logger, _) =>
{
logger
.AllBuildEvents
Expand Down Expand Up @@ -4803,7 +4803,7 @@ public void VerifyPropertyTrackingLoggingPropertyInitialValue()
{
this.VerifyPropertyTrackingLoggingScenario(
"2",
logger =>
(logger, projectPath) =>
{
logger
.AllBuildEvents
Expand All @@ -4829,23 +4829,23 @@ public void VerifyPropertyTrackingLoggingPropertyInitialValue()

// Verify logging of property initial values.
propertyInitialValueMap.ShouldContainKey("Prop");
propertyInitialValueMap["Prop"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["Prop"].File.ShouldBe(projectPath);
propertyInitialValueMap["Prop"].PropertyValue.ShouldBe(string.Empty);

propertyInitialValueMap.ShouldContainKey("EnvVar");
propertyInitialValueMap["EnvVar"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["EnvVar"].File.ShouldBe(projectPath);
propertyInitialValueMap["EnvVar"].PropertyValue.ShouldBe("It's also Defined!");

propertyInitialValueMap.ShouldContainKey("DEFINED_ENVIRONMENT_VARIABLE");
propertyInitialValueMap["DEFINED_ENVIRONMENT_VARIABLE"].PropertySource.ShouldBe("EnvironmentVariable");
propertyInitialValueMap["DEFINED_ENVIRONMENT_VARIABLE"].PropertyValue.ShouldBe("It's Defined!");

propertyInitialValueMap.ShouldContainKey("NotEnvVarRead");
propertyInitialValueMap["NotEnvVarRead"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["NotEnvVarRead"].File.ShouldBe(projectPath);
propertyInitialValueMap["NotEnvVarRead"].PropertyValue.ShouldBe("Overwritten!");

propertyInitialValueMap.ShouldContainKey("Prop2");
propertyInitialValueMap["Prop2"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["Prop2"].File.ShouldBe(projectPath);
propertyInitialValueMap["Prop2"].PropertyValue.ShouldBe("Value1");
});
}
Expand All @@ -4855,7 +4855,7 @@ public void VerifyPropertyTrackingLoggingEnvironmentVariableRead()
{
this.VerifyPropertyTrackingLoggingScenario(
"4",
logger =>
(logger, _) =>
{
logger
.AllBuildEvents
Expand Down Expand Up @@ -4889,7 +4889,7 @@ public void VerifyPropertyTrackingLoggingUninitializedPropertyRead()
{
this.VerifyPropertyTrackingLoggingScenario(
"8",
logger =>
(logger, _) =>
{
logger
.AllBuildEvents
Expand Down Expand Up @@ -4920,7 +4920,7 @@ public void VerifyPropertyTrackingLoggingAll()
{
this.VerifyPropertyTrackingLoggingScenario(
"15",
logger =>
(logger, projectPath) =>
{
logger
.AllBuildEvents
Expand Down Expand Up @@ -4949,23 +4949,23 @@ public void VerifyPropertyTrackingLoggingAll()

// Verify logging of property initial values.
propertyInitialValueMap.ShouldContainKey("Prop");
propertyInitialValueMap["Prop"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["Prop"].File.ShouldBe(projectPath);
propertyInitialValueMap["Prop"].PropertyValue.ShouldBe(string.Empty);

propertyInitialValueMap.ShouldContainKey("EnvVar");
propertyInitialValueMap["EnvVar"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["EnvVar"].File.ShouldBe(projectPath);
propertyInitialValueMap["EnvVar"].PropertyValue.ShouldBe("It's also Defined!");

propertyInitialValueMap.ShouldContainKey("DEFINED_ENVIRONMENT_VARIABLE");
propertyInitialValueMap["DEFINED_ENVIRONMENT_VARIABLE"].PropertySource.ShouldBe("EnvironmentVariable");
propertyInitialValueMap["DEFINED_ENVIRONMENT_VARIABLE"].PropertyValue.ShouldBe("It's Defined!");

propertyInitialValueMap.ShouldContainKey("NotEnvVarRead");
propertyInitialValueMap["NotEnvVarRead"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["NotEnvVarRead"].File.ShouldBe(projectPath);
propertyInitialValueMap["NotEnvVarRead"].PropertyValue.ShouldBe("Overwritten!");

propertyInitialValueMap.ShouldContainKey("Prop2");
propertyInitialValueMap["Prop2"].PropertySource.ShouldBe("Xml");
propertyInitialValueMap["Prop2"].File.ShouldBe(projectPath);
propertyInitialValueMap["Prop2"].PropertyValue.ShouldBe("Value1");
});
}
Expand All @@ -4987,7 +4987,7 @@ public void VerifyGetTypeEvaluationBlocked()
new Project(XmlReader.Create(new StringReader(projectContents)), null, "Fake", fakeProjectCollection));
}

private void VerifyPropertyTrackingLoggingScenario(string envVarValue, Action<MockLogger> loggerEvaluatorAction)
private void VerifyPropertyTrackingLoggingScenario(string envVarValue, Action<MockLogger, string> loggerEvaluatorAction)
{
// The default is that only reassignments are logged.

Expand Down Expand Up @@ -5026,7 +5026,7 @@ private void VerifyPropertyTrackingLoggingScenario(string envVarValue, Action<Mo

project.Build().ShouldBeTrue();

loggerEvaluatorAction?.Invoke(logger);
loggerEvaluatorAction?.Invoke(logger, tempPath.Path);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Build/BackEnd/BuildManager/BuildParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public class BuildParameters : ITranslatable
/// </summary>
private PropertyDictionary<ProjectPropertyInstance> _globalProperties = new PropertyDictionary<ProjectPropertyInstance>();

/// <summary>
/// Properties passed from the command line (e.g. by using /p:).
/// </summary>
private HashSet<string> _propertiesFromCommandLine;

/// <summary>
/// The loggers.
/// </summary>
Expand Down Expand Up @@ -250,6 +255,7 @@ public BuildParameters(ProjectCollection projectCollection)
_defaultToolsVersion = projectCollection.DefaultToolsVersion;

_globalProperties = new PropertyDictionary<ProjectPropertyInstance>(projectCollection.GlobalPropertiesCollection);
_propertiesFromCommandLine = projectCollection.PropertiesFromCommandLine;
}

/// <summary>
Expand Down Expand Up @@ -279,6 +285,7 @@ internal BuildParameters(BuildParameters other, bool resetEnvironment = false)
_environmentProperties = other._environmentProperties != null ? new PropertyDictionary<ProjectPropertyInstance>(other._environmentProperties) : null;
_forwardingLoggers = other._forwardingLoggers != null ? new List<ForwardingLoggerRecord>(other._forwardingLoggers) : null;
_globalProperties = other._globalProperties != null ? new PropertyDictionary<ProjectPropertyInstance>(other._globalProperties) : null;
_propertiesFromCommandLine = other._propertiesFromCommandLine != null ? new HashSet<string>(other._propertiesFromCommandLine) : null;
HostServices = other.HostServices;
_loggers = other._loggers != null ? new List<ILogger>(other._loggers) : null;
_maxNodeCount = other._maxNodeCount;
Expand Down Expand Up @@ -472,6 +479,11 @@ public IDictionary<string, string> GlobalProperties
}
}

/// <summary>
/// Properties passed from the command line (e.g. by using /p:).
/// </summary>
public HashSet<string> PropertiesFromCommandLine => _propertiesFromCommandLine;

/// <summary>
/// Interface allowing the host to provide additional control over the build process.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Build/BackEnd/Components/RequestBuilder/IntrinsicTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;

#nullable disable
Expand Down Expand Up @@ -69,7 +70,7 @@ internal static IntrinsicTask InstantiateTask(ProjectTargetInstanceChild taskIns
{
if (taskInstance is ProjectPropertyGroupTaskInstance propertyGroupTaskInstance)
{
return new PropertyGroupIntrinsicTask(propertyGroupTaskInstance, loggingContext, projectInstance, logTaskInputs);
return new PropertyGroupIntrinsicTask(propertyGroupTaskInstance, loggingContext, projectInstance, logTaskInputs, Traits.Instance.LogPropertyTracking);
}
else if (taskInstance is ProjectItemGroupTaskInstance itemGroupTaskInstance)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ internal class PropertyGroupIntrinsicTask : IntrinsicTask
/// </summary>
private ProjectPropertyGroupTaskInstance _taskInstance;

private readonly PropertyTrackingSetting _propertyTrackingSettings;

/// <summary>
/// Create a new PropertyGroup task.
/// </summary>
/// <param name="taskInstance">The task instance data</param>
/// <param name="loggingContext">The logging context</param>
/// <param name="projectInstance">The project instance</param>
/// <param name="logTaskInputs">Flag to determine whether or not to log task inputs.</param>
public PropertyGroupIntrinsicTask(ProjectPropertyGroupTaskInstance taskInstance, TargetLoggingContext loggingContext, ProjectInstance projectInstance, bool logTaskInputs)
/// <param name="settingValue">Flag to determine whether or not property tracking enabled.</param>
public PropertyGroupIntrinsicTask(ProjectPropertyGroupTaskInstance taskInstance, TargetLoggingContext loggingContext, ProjectInstance projectInstance, bool logTaskInputs, int settingValue)
: base(loggingContext, projectInstance, logTaskInputs)
{
_taskInstance = taskInstance;
_propertyTrackingSettings = (PropertyTrackingSetting)settingValue;
}

/// <summary>
Expand Down Expand Up @@ -85,6 +89,8 @@ internal override void ExecuteTask(Lookup lookup)
string evaluatedValue = bucket.Expander.ExpandIntoStringLeaveEscaped(property.Value, ExpanderOptions.ExpandAll, property.Location);
bucket.Expander.PropertiesUseTracker.CheckPreexistingUndefinedUsage(property, evaluatedValue, LoggingContext);

LogPropertyInTargetAssignment(property, evaluatedValue);

if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents)
{
LoggingContext.LogComment(MessageImportance.Low, "PropertyGroupLogMessage", property.Name, evaluatedValue);
Expand All @@ -111,6 +117,51 @@ internal override void ExecuteTask(Lookup lookup)
}
}

/// <summary>
/// Logs property assignment information during target execution, providing detailed tracking of property value changes.
/// </summary>
/// <param name="property">The property instance being assigned or modified.</param>
/// <param name="evaluatedValue">The new evaluated value of the property.</param>
private void LogPropertyInTargetAssignment(ProjectPropertyGroupTaskPropertyInstance property, string evaluatedValue)
{
if (_propertyTrackingSettings == 0 || !ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_10))
YuliiaKovalova marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

var previousPropertyValue = Project.GetProperty(property.Name)?.EvaluatedValue;

if (previousPropertyValue == null
&& (_propertyTrackingSettings & PropertyTrackingSetting.PropertyInitialValueSet) == PropertyTrackingSetting.PropertyInitialValueSet)
{
var args = new PropertyInitialValueSetEventArgs(
property.Name,
evaluatedValue,
propertySource: string.Empty,
property.Location.File,
property.Location.Line,
property.Location.Column,
ResourceUtilities.GetResourceString("PropertyAssignment"))
{ BuildEventContext = LoggingContext.BuildEventContext };

LoggingContext.LogBuildEvent(args);
}
else if ((_propertyTrackingSettings & PropertyTrackingSetting.PropertyReassignment) == PropertyTrackingSetting.PropertyReassignment)
{
var args = new PropertyReassignmentEventArgs(
property.Name,
previousPropertyValue,
evaluatedValue,
property.Location.File,
property.Location.Line,
property.Location.Column,
message: ResourceUtilities.GetResourceString("PropertyReassignment"))
{ BuildEventContext = LoggingContext.BuildEventContext, };

LoggingContext.LogBuildEvent(args);
}
}

/// <summary>
/// Adds batchable parameters from a property element into the list. If the property element was
/// a task, these would be its raw parameter values.
Expand Down
50 changes: 50 additions & 0 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ internal class TaskExecutionHost : IDisposable
/// </summary>
private readonly Dictionary<string, TaskFactoryWrapper> _intrinsicTasks = new Dictionary<string, TaskFactoryWrapper>(StringComparer.OrdinalIgnoreCase);

private readonly PropertyTrackingSetting _propertyTrackingSettings;

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -172,6 +174,8 @@ internal TaskExecutionHost(IBuildComponentHost host)
{
LogTaskInputs = Traits.Instance.EscapeHatches.LogTaskInputs;
}

_propertyTrackingSettings = (PropertyTrackingSetting)Traits.Instance.LogPropertyTracking;
}

/// <summary>
Expand Down Expand Up @@ -1582,12 +1586,58 @@ private void GatherArrayStringAndValueOutputs(bool outputTargetIsItem, string ou
}
}

LogPropertyInTaskAssignment(outputTargetName, outputString, parameterLocation);
_batchBucket.Lookup.SetProperty(ProjectPropertyInstance.Create(outputTargetName, outputString, parameterLocation, _projectInstance.IsImmutable));
}
}
}
}

/// <summary>
/// Logs property assignment information during task execution, based on configured property tracking settings.
/// </summary>
/// <param name="propertyName">The name of the property being assigned or reassigned.</param>
/// <param name="propertyValue">The new value being assigned to the property.</param>
/// <param name="location">The source location where the property assignment occurs.</param>
private void LogPropertyInTaskAssignment(string propertyName, string propertyValue, IElementLocation location)
{
if (_propertyTrackingSettings == 0 || !ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_10))
{
return;
}

var previousPropertyValue = _projectInstance.GetProperty(propertyName)?.EvaluatedValue;

if (previousPropertyValue == null && (_propertyTrackingSettings & PropertyTrackingSetting.PropertyInitialValueSet) == PropertyTrackingSetting.PropertyInitialValueSet)
{
var args = new PropertyInitialValueSetEventArgs(
propertyName,
propertyValue,
propertySource: string.Empty,
location.File,
location.Line,
location.Column,
ResourceUtilities.GetResourceString("PropertyAssignment"))
{ BuildEventContext = _targetLoggingContext.BuildEventContext };

_targetLoggingContext.LogBuildEvent(args);
}
else if ((_propertyTrackingSettings & PropertyTrackingSetting.PropertyReassignment) == PropertyTrackingSetting.PropertyReassignment)
{
var args = new PropertyReassignmentEventArgs(
propertyName,
previousPropertyValue,
propertyValue,
location.File,
location.Line,
location.Column,
message: ResourceUtilities.GetResourceString("PropertyReassignment"))
{ BuildEventContext = _targetLoggingContext.BuildEventContext };

_targetLoggingContext.LogBuildEvent(args);
}
}

/// <summary>
/// Finds all the task properties that are required.
/// Returns them as keys in a dictionary.
Expand Down
3 changes: 2 additions & 1 deletion src/Build/Definition/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3721,6 +3721,7 @@ private void Reevaluate(
loadSettings,
ProjectCollection.MaxNodeCount,
ProjectCollection.EnvironmentProperties,
ProjectCollection.PropertiesFromCommandLine,
loggingServiceForEvaluation,
new ProjectItemFactory(Owner),
ProjectCollection,
Expand Down Expand Up @@ -4437,7 +4438,7 @@ public IItemDefinition<ProjectMetadata> GetItemDefinition(string itemType)
/// <summary>
/// Sets a property which is not derived from Xml.
/// </summary>
public ProjectProperty SetProperty(string name, string evaluatedValueEscaped, bool isGlobalProperty, bool mayBeReserved, LoggingContext loggingContext, bool isEnvironmentVariable = false)
public ProjectProperty SetProperty(string name, string evaluatedValueEscaped, bool isGlobalProperty, bool mayBeReserved, LoggingContext loggingContext, bool isEnvironmentVariable = false, bool isCommandLineProperty = false)
{
ProjectProperty property = ProjectProperty.Create(Project, name, evaluatedValueEscaped, isGlobalProperty, mayBeReserved, loggingContext);
Properties.Set(property);
Expand Down
Loading