Skip to content

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

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

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d9f7791
add location to the initial property assignment event and tracking pr…
YuliiaKovalova Dec 6, 2024
1988391
extend condition
YuliiaKovalova Dec 6, 2024
c034187
fix typo
YuliiaKovalova Dec 6, 2024
9de0ce2
add separate categorization of properties specified in command line
YuliiaKovalova Dec 9, 2024
b1ed4a4
fix null ref exception
YuliiaKovalova Dec 9, 2024
9bd143e
fix invalid condition for property source
YuliiaKovalova Dec 9, 2024
923e833
change the event content to get dedupe functionality
YuliiaKovalova Dec 10, 2024
bf37922
add extra ctor to PropertyReassignmentEventArgs to improve parsing lo…
YuliiaKovalova Dec 10, 2024
7da96c3
change location formatting
YuliiaKovalova Dec 10, 2024
e7f4df1
adjust the tests to the code changes
YuliiaKovalova Dec 11, 2024
047bd55
remove extra using
YuliiaKovalova Dec 11, 2024
b81fac7
Merge branch 'dotnet:main' into dev/ykovalova/property_assignment
YuliiaKovalova Jan 2, 2025
6ed8628
Update src/Build/Evaluation/Evaluator.cs
YuliiaKovalova Jan 6, 2025
05dad6d
Merge branch 'dotnet:main' into dev/ykovalova/property_assignment
YuliiaKovalova Jan 21, 2025
9964cb9
remove changewave check and enumutils
YuliiaKovalova Jan 22, 2025
75fe784
fix review comments
YuliiaKovalova Jan 27, 2025
aa62e8c
fix the test
YuliiaKovalova Jan 27, 2025
ee58f18
apply message optimization for UninitializedPropertyRead
YuliiaKovalova Jan 28, 2025
9a5ad3c
fix review comments
YuliiaKovalova Jan 31, 2025
4810754
fix review comments
YuliiaKovalova Feb 7, 2025
f6f2b20
fix review comments
YuliiaKovalova Feb 10, 2025
d51cfc7
fix review comments
YuliiaKovalova Feb 10, 2025
80beaa6
fix review comments
YuliiaKovalova Feb 12, 2025
bdadaea
change PropertiesFromCommandLine population
YuliiaKovalova Feb 12, 2025
9a7cd55
fix the missed case with command line
YuliiaKovalova Feb 13, 2025
78eaf82
undo change of change wave
YuliiaKovalova Feb 13, 2025
152ceb2
add doc about property assignment
YuliiaKovalova Feb 13, 2025
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
27 changes: 18 additions & 9 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using FluentAssertions;
using FluentAssertions.Equivalency;
using Microsoft.Build.BackEnd;
using Microsoft.Build.Experimental.BuildCheck;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -881,8 +882,11 @@ public void RoundTripPropertyReassignmentEventArgs()
propertyName: "a",
previousValue: "b",
newValue: "c",
location: "d",
message: "Property reassignment: $(a)=\"c\" (previous value: \"b\") at d",
location: null,
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 All @@ -900,8 +904,8 @@ public void RoundTripPropertyReassignmentEventArgs()
public void UninitializedPropertyReadEventArgs()
{
var args = new UninitializedPropertyReadEventArgs(
propertyName: Guid.NewGuid().ToString(),
message: Guid.NewGuid().ToString(),
propertyName: "a",
message: "Read uninitialized property \"a\"",
helpKeyword: Guid.NewGuid().ToString(),
senderName: Guid.NewGuid().ToString());

Expand All @@ -916,17 +920,22 @@ public void UninitializedPropertyReadEventArgs()
public void PropertyInitialValueEventArgs()
{
var args = new PropertyInitialValueSetEventArgs(
propertyName: Guid.NewGuid().ToString(),
propertyValue: Guid.NewGuid().ToString(),
propertySource: Guid.NewGuid().ToString(),
message: Guid.NewGuid().ToString(),
propertyName: "a",
propertyValue: "b",
propertySource: null,
file: "file.cs",
line: 10,
column: 20,
message: "Property initial value: $(a)=\"b\" Source: file.cs (10,20)",
helpKeyword: Guid.NewGuid().ToString(),
senderName: Guid.NewGuid().ToString());

Roundtrip(args,
e => e.PropertyName,
e => e.PropertyValue,
e => e.PropertySource,
e => e.File,
e => e.LineNumber.ToString(),
e => e.ColumnNumber.ToString(),
e => e.Message,
e => e.HelpKeyword,
e => e.SenderName);
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,52 @@ 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)
{
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,
message: null)
{ BuildEventContext = LoggingContext.BuildEventContext };

LoggingContext.LogBuildEvent(args);
}
else if ((_propertyTrackingSettings & PropertyTrackingSetting.PropertyReassignment) == PropertyTrackingSetting.PropertyReassignment)
{
var args = new PropertyReassignmentEventArgs(
property.Name,
previousPropertyValue,
evaluatedValue,
location: null,
property.Location.File,
property.Location.Line,
property.Location.Column,
message: null)
{ 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
Loading