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 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
26 changes: 17 additions & 9 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,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 +903,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 +919,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");
Copy link
Member

Choose a reason for hiding this comment

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

still validate the source? or does it not make sense now?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't pass the source if File path is known. It seems to be excessive now.

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;
Copy link
Member

Choose a reason for hiding this comment

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

Ah, I think this isn't quite what we want, instead we want "global properties set for the whole build", independent of whether that's through the API or command line args.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

could you please clarify if we need this additional classification or not?


/// <summary>
/// Interface allowing the host to provide additional control over the build process.
/// </summary>
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal class PropertyGroupIntrinsicTask : IntrinsicTask
/// </summary>
private ProjectPropertyGroupTaskInstance _taskInstance;

private readonly PropertyTrackingSetting _propertyTrackingSettings;

/// <summary>
/// Create a new PropertyGroup task.
/// </summary>
Expand All @@ -36,6 +38,7 @@ public PropertyGroupIntrinsicTask(ProjectPropertyGroupTaskInstance taskInstance,
: base(loggingContext, projectInstance, logTaskInputs)
{
_taskInstance = taskInstance;
_propertyTrackingSettings = (PropertyTrackingSetting)Traits.Instance.LogPropertyTracking;
}

/// <summary>
Expand Down Expand Up @@ -85,6 +88,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);

PropertyTrackingUtils.LogPropertyAssignment(_propertyTrackingSettings, property.Name, evaluatedValue, property.Location, Project.GetProperty(property.Name)?.EvaluatedValue ?? null, LoggingContext);

if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents)
{
LoggingContext.LogComment(MessageImportance.Low, "PropertyGroupLogMessage", property.Name, evaluatedValue);
Expand Down
52 changes: 52 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,60 @@ private void GatherArrayStringAndValueOutputs(bool outputTargetIsItem, string ou
}
}

PropertyTrackingUtils.LogPropertyAssignment(_propertyTrackingSettings, outputTargetName, outputString, parameterLocation, _projectInstance.GetProperty(outputTargetName)?.EvaluatedValue ?? null, _targetLoggingContext);

_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 == PropertyTrackingSetting.None)
{
return;
}

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

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

_targetLoggingContext.LogBuildEvent(args);
}
else if (PropertyTrackingUtils.IsPropertyTrackingEnabled(_propertyTrackingSettings, PropertyTrackingSetting.PropertyReassignment))
{
var args = new PropertyReassignmentEventArgs(
propertyName,
previousPropertyValue,
propertyValue,
location: null,
location.File,
location.Line,
location.Column,
message: null)
{ 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
8 changes: 8 additions & 0 deletions src/Build/Definition/ProjectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ public ProjectCollection(IDictionary<string, string> globalProperties, IEnumerab
{
_globalProperties = new PropertyDictionary<ProjectPropertyInstance>(globalProperties.Count);

// at this stage globalProperties collection contains entries passed from command line (e.g. /p:foo=bar).
PropertiesFromCommandLine = [.. globalProperties.Keys];

foreach (KeyValuePair<string, string> pair in globalProperties)
{
try
Expand Down Expand Up @@ -497,6 +500,11 @@ public static string DisplayVersion
}
}

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

/// <summary>
/// The default tools version of this project collection. Projects use this tools version if they
/// aren't otherwise told what tools version to use.
Expand Down
Loading
Loading