From f712a9f3c2e11c792f41c48e622217cb3b5ea86e Mon Sep 17 00:00:00 2001 From: Ladi Prosek Date: Wed, 15 May 2024 13:03:56 +0200 Subject: [PATCH 1/2] Add support for improved TaskParameterEventArgs --- src/StructuredLogger/BinaryLog.cs | 2 +- .../BinaryLogger/BinaryLogger.cs | 4 ++- .../BinaryLogger/BuildEventArgsReader.cs | 5 ++++ .../BinaryLogger/BuildEventArgsWriter.cs | 5 ++++ .../BinaryLogger/Utilities.cs | 25 ++++++------------- .../Construction/MessageProcessor.cs | 17 ++++++++++--- src/StructuredLogger/Reflector.cs | 14 +---------- 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/StructuredLogger/BinaryLog.cs b/src/StructuredLogger/BinaryLog.cs index e43aac50..35b41383 100644 --- a/src/StructuredLogger/BinaryLog.cs +++ b/src/StructuredLogger/BinaryLog.cs @@ -165,7 +165,7 @@ public static Build ReadBuild( if (errorByType.Any(i => i != 0)) { - string summary = string.Join(", ", errorByType.Where((count, index) => count > 0).Select((count, index) => $"{((ReaderErrorType)index)}: {count}")); + string summary = string.Join(", ", errorByType.Where((count, index) => count > 0).Select((count, index) => $"{((ReaderErrorType)index)}: {count} cases")); string message = $"Skipped some data unknown to this version of Viewer. {errorByType.Sum()} case{(errorByType.Sum() > 1 ? "s" : string.Empty)} encountered ({summary})."; TreeNode node = readerSettings.UnknownDataBehavior switch diff --git a/src/StructuredLogger/BinaryLogger/BinaryLogger.cs b/src/StructuredLogger/BinaryLogger/BinaryLogger.cs index eb7c959d..45f49a9c 100644 --- a/src/StructuredLogger/BinaryLogger/BinaryLogger.cs +++ b/src/StructuredLogger/BinaryLogger/BinaryLogger.cs @@ -69,6 +69,8 @@ public sealed class BinaryLogger : ILogger // - GeneratedFileUsedEventArgs exposed for brief period of time (so let's continue with 20) // version 20: // - TaskStartedEventArgs: Added TaskAssemblyLocation property + // version 21: + // - TaskParameterEventArgs: Added ParameterName and PropertyName properties // This should be never changed. // The minimum version of the binary log reader that can read log of above version. @@ -76,7 +78,7 @@ public sealed class BinaryLogger : ILogger // The current version of the binary log representation. // Changes with each update of the binary log format. - internal const int FileFormatVersion = 20; + internal const int FileFormatVersion = 21; // The minimum version of the binary log reader that can read log of above version. // This should be changed only when the binary log format is changed in a way that would prevent it from being // read by older readers. (changing of the individual BuildEventArgs or adding new is fine - as reader can diff --git a/src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs b/src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs index 1ce07781..e5a696e5 100644 --- a/src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs +++ b/src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs @@ -1149,10 +1149,15 @@ private BuildEventArgs ReadTaskParameterEventArgs() var kind = (TaskParameterMessageKind)ReadInt32(); var itemType = ReadDeduplicatedString() ?? "N/A"; var items = ReadTaskItemList() as IList ?? Array.Empty(); + var (parameterName, propertyName) = _fileFormatVersion >= 21 + ? (ReadDeduplicatedString(), ReadDeduplicatedString()) + : (null, null); var e = ItemGroupLoggingHelper.CreateTaskParameterEventArgs( fields.BuildEventContext, kind, + parameterName, + propertyName, itemType, items, logItemMetadata: true, diff --git a/src/StructuredLogger/BinaryLogger/BuildEventArgsWriter.cs b/src/StructuredLogger/BinaryLogger/BuildEventArgsWriter.cs index 79172aec..dc051c56 100644 --- a/src/StructuredLogger/BinaryLogger/BuildEventArgsWriter.cs +++ b/src/StructuredLogger/BinaryLogger/BuildEventArgsWriter.cs @@ -589,6 +589,11 @@ private BinaryLogRecordKind Write(TaskParameterEventArgs e) Write((int)e.Kind); WriteDeduplicatedString(e.ItemType); WriteTaskItemList(e.Items, e.LogItemMetadata); + + TaskParameterEventArgs2 taskParameters2 = e as TaskParameterEventArgs2; + WriteDeduplicatedString(taskParameters2?.ParameterName); + WriteDeduplicatedString(taskParameters2?.PropertyName); + if (e.Kind == TaskParameterMessageKind.AddItem || e.Kind == TaskParameterMessageKind.TaskOutput) { diff --git a/src/StructuredLogger/BinaryLogger/Utilities.cs b/src/StructuredLogger/BinaryLogger/Utilities.cs index 96d130a2..79e2679e 100644 --- a/src/StructuredLogger/BinaryLogger/Utilities.cs +++ b/src/StructuredLogger/BinaryLogger/Utilities.cs @@ -2,10 +2,8 @@ using System.Collections; using System.Collections.Generic; using System.IO; -using System.Reflection; using System.Runtime.CompilerServices; using Microsoft.Build.Framework; -using Microsoft.Build.Logging.StructuredLogger; namespace Microsoft.Build.BackEnd { @@ -14,6 +12,8 @@ internal class ItemGroupLoggingHelper internal static TaskParameterEventArgs CreateTaskParameterEventArgs( BuildEventContext buildEventContext, TaskParameterMessageKind messageKind, + string parameterName, + string propertyName, string itemType, IList items, bool logItemMetadata, @@ -21,27 +21,18 @@ internal static TaskParameterEventArgs CreateTaskParameterEventArgs( int line, int column) { - var args = new TaskParameterEventArgs( + var args = new TaskParameterEventArgs2( messageKind, + parameterName, + propertyName, itemType, items, logItemMetadata, timestamp); args.BuildEventContext = buildEventContext; - if (line != 0) - { - Reflector.SetLineNumber(args, line); - } - - if (column != 0) - { - Reflector.SetColumnNumber(args, column); - } - - // Should probably make these public - // args.LineNumber = line; - // args.ColumnNumber = column; + args.LineNumber = line; + args.ColumnNumber = column; return args; } } @@ -426,4 +417,4 @@ public static BuildEventContext ReadBuildEventContext(this BinaryReader reader) return buildEventContext; } } -} \ No newline at end of file +} diff --git a/src/StructuredLogger/Construction/MessageProcessor.cs b/src/StructuredLogger/Construction/MessageProcessor.cs index 092d75a3..da32fb96 100644 --- a/src/StructuredLogger/Construction/MessageProcessor.cs +++ b/src/StructuredLogger/Construction/MessageProcessor.cs @@ -174,6 +174,9 @@ private void ProcessProjectImported(ProjectImportedEventArgs args) private void ProcessTaskParameter(TaskParameterEventArgs args) { string itemType = args.ItemType; + var (parameterName, propertyName) = args is TaskParameterEventArgs2 taskParameterEventArgs2 + ? (taskParameterEventArgs2.ParameterName, taskParameterEventArgs2.PropertyName) + : (null, null); var items = args.Items; var kind = args.Kind; @@ -189,11 +192,17 @@ private void ProcessTaskParameter(TaskParameterEventArgs args) bool isOutput = kind == TaskParameterMessageKind.TaskOutput; - string folderName = isOutput ? Strings.OutputItems : Strings.Parameters; + string folderName = isOutput + ? (propertyName is null ? Strings.OutputItems : Strings.OutputProperties) + : Strings.Parameters; parent = task.GetOrCreateNodeWithName(folderName); parent.DisableChildrenCache = true; - node = CreateParameterNode(itemType, items, isOutput); + string itemName = isOutput && parameterName is not null + ? $"{propertyName ?? itemType} from parameter {parameterName}" + : propertyName ?? itemType; + + node = CreateParameterNode(itemName, items, isOutput); } else if ( kind == TaskParameterMessageKind.AddItem || @@ -208,14 +217,14 @@ private void ProcessTaskParameter(TaskParameterEventArgs args) { named = new AddItem { - LineNumber = args.LineNumber + LineNumber = (args as TaskParameterEventArgs2)?.LineNumber ?? args.LineNumber }; } else if (kind == TaskParameterMessageKind.RemoveItem) { named = new RemoveItem { - LineNumber = args.LineNumber + LineNumber = (args as TaskParameterEventArgs2)?.LineNumber ?? args.LineNumber }; } else diff --git a/src/StructuredLogger/Reflector.cs b/src/StructuredLogger/Reflector.cs index c749667b..c974f8bb 100644 --- a/src/StructuredLogger/Reflector.cs +++ b/src/StructuredLogger/Reflector.cs @@ -71,18 +71,6 @@ public static void SetTimestamp(BuildEventArgs args, DateTime timestamp) timeStampSetter(args, timestamp); } - private static Action lineNumberSetter = GetFieldSetter("lineNumber"); - public static void SetLineNumber(BuildMessageEventArgs args, int lineNumber) - { - lineNumberSetter(args, lineNumber); - } - - private static Action columnNumberSetter = GetFieldSetter("columnNumber"); - public static void SetColumnNumber(BuildMessageEventArgs args, int columnNumber) - { - columnNumberSetter(args, columnNumber); - } - private static MethodInfo enumerateItemsPerType; public static MethodInfo GetEnumerateItemsPerTypeMethod(Type itemDictionary) { @@ -114,4 +102,4 @@ public static Action GetFieldSetter(string fieldName) return compiled; } } -} \ No newline at end of file +} From 92890bab0a4af7b20a0c64c532231ced2dbaf37f Mon Sep 17 00:00:00 2001 From: Ladi Prosek Date: Wed, 15 May 2024 13:29:28 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Add=20TaskParameterEventArgs2=20?= =?UTF-8?q?=F0=9F=A4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BinaryLogger/TaskParameterEventArgs2.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/StructuredLogger/BinaryLogger/TaskParameterEventArgs2.cs diff --git a/src/StructuredLogger/BinaryLogger/TaskParameterEventArgs2.cs b/src/StructuredLogger/BinaryLogger/TaskParameterEventArgs2.cs new file mode 100644 index 00000000..a986fce3 --- /dev/null +++ b/src/StructuredLogger/BinaryLogger/TaskParameterEventArgs2.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections; + +namespace Microsoft.Build.Framework +{ + public class TaskParameterEventArgs2 : TaskParameterEventArgs + { + public TaskParameterEventArgs2( + TaskParameterMessageKind kind, + string parameterName, + string propertyName, + string itemType, + IList items, + bool logItemMetadata, + DateTime eventTimestamp) + : base(kind, itemType, items, logItemMetadata, eventTimestamp) + { + ParameterName = parameterName; + PropertyName = propertyName; + } + + // Added in MSBuild 17.11 + public string ParameterName { get; set; } + public string PropertyName { get; set; } + + // the properties in the base class have an internal setter + public new int LineNumber { get; set; } + public new int ColumnNumber { get; set; } + } +}