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

Added task assembly location to TaskStartedEventArgs #9948

Merged
merged 20 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 4 additions & 3 deletions src/Build.UnitTests/BackEnd/LoggingServicesLogMethod_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using Microsoft.Build.BackEnd;
using Microsoft.Build.BackEnd.Logging;
Expand Down Expand Up @@ -1089,7 +1090,7 @@ public void TaskStartedNullBuildEventContext()
Assert.Throws<InternalErrorException>(() =>
{
ProcessBuildEventHelper service = (ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.LogTaskStarted(null, "MyTask", "ProjectFile", "ProjectFileOfTask");
service.LogTaskStarted(null, "MyTask", "ProjectFile", "ProjectFileOfTask", null);
MichalPavlik marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down Expand Up @@ -1445,12 +1446,12 @@ private void TestTaskStartedEvent(string taskName, string projectFile, string pr
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword("TaskStarted", taskName);

ProcessBuildEventHelper service = (ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask);
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask, Assembly.GetExecutingAssembly().GetName().FullName);
VerifyTaskStartedEvent(taskName, projectFile, projectFileOfTask, message, service);

service.ResetProcessedBuildEvent();
service.OnlyLogCriticalEvents = true;
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask);
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask, Assembly.GetExecutingAssembly().GetName().FullName);
Assert.Null(service.ProcessedBuildEvent);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Build.UnitTests/BackEnd/MockLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Profiler;
Expand Down Expand Up @@ -573,7 +574,8 @@ public void LogTargetFinished(BuildEventContext targetBuildEventContext, string
/// <param name="taskName">The name of the task</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTaskNode">The project file containing the task node.</param>
public void LogTaskStarted(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode)
/// <param name="taskAssemblyName">An assembly's unique identity where the task is implemented.</param>
public void LogTaskStarted(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, string taskAssemblyName)
{
}

Expand All @@ -584,8 +586,9 @@ public void LogTaskStarted(BuildEventContext targetBuildEventContext, string tas
/// <param name="taskName">The name of the task</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTaskNode">The project file containing the task node.</param>
/// <param name="taskAssemblyName">An assembly's unique identity where the task is implemented.</param>
/// <returns>The task logging context</returns>
public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, int line, int column)
public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, int line, int column, string taskAssemblyName)
{
return new BuildEventContext(0, 0, 0, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Build.UnitTests/BackEnd/TaskExecutionHost_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TaskExecutionHost_Tests : ITestTaskHost, IBuildEngine2, IDisposable
/// <summary>
/// The task execution host
/// </summary>
private ITaskExecutionHost _host;
private TaskExecutionHost _host;

/// <summary>
/// The mock logging service
Expand Down Expand Up @@ -1190,7 +1190,7 @@ private void InitializeHost(bool throwOnExecute)
CancellationToken.None);

ProjectTaskInstance taskInstance = project.Targets["foo"].Tasks.First();
TaskLoggingContext talc = tlc.LogTaskBatchStarted(".", taskInstance);
TaskLoggingContext talc = tlc.LogTaskBatchStarted(".", taskInstance, typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory).GetTypeInfo().Assembly.GetName().FullName);
MichalPavlik marked this conversation as resolved.
Show resolved Hide resolved

ItemDictionary<ProjectItemInstance> itemsByName = new ItemDictionary<ProjectItemInstance>();

Expand Down
7 changes: 5 additions & 2 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ public void RoundtripTaskStartedEventArgs()
null,
projectFile: "C:\\project.proj",
taskFile: "C:\\common.targets",
taskName: "Csc");
taskName: "Csc",
DateTime.Now,
"TaskAssemblyName");
args.LineNumber = 42;
args.ColumnNumber = 999;

Expand All @@ -200,7 +202,8 @@ public void RoundtripTaskStartedEventArgs()
e => e.TaskFile,
e => e.TaskName,
e => e.LineNumber.ToString(),
e => e.ColumnNumber.ToString());
e => e.ColumnNumber.ToString(),
e => e.TaskAssemblyName);
}

[Fact]
Expand Down
7 changes: 5 additions & 2 deletions src/Build/BackEnd/Components/Logging/ILoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
MichalPavlik marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Profiler;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -570,7 +571,8 @@ BuildEventContext LogProjectStarted(
/// <param name="taskName">The name of the task</param>
/// <param name="projectFile">The project file which is being built</param>
/// <param name="projectFileOfTaskNode">The file in which the task is defined - typically a .targets file</param>
void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode);
/// <param name="taskAssemblyName">An assembly's unique identity where the task is implemented</param>
void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, string taskAssemblyName);

/// <summary>
/// Log that a task is about to start
Expand All @@ -581,8 +583,9 @@ BuildEventContext LogProjectStarted(
/// <param name="projectFileOfTaskNode">The file in which the task is defined - typically a .targets file</param>
/// <param name="line">The line number in the file where the task invocation is located.</param>
/// <param name="column">The column number in the file where the task invocation is located.</param>
/// <param name="taskAssemblyName">An assembly's unique identity where the task is implemented.</param>
/// <returns>The task build event context</returns>
BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, int line, int column);
BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, int line, int column, string taskAssemblyName);

/// <summary>
/// Log that a task has just completed
Expand Down
13 changes: 9 additions & 4 deletions src/Build/BackEnd/Components/Logging/LoggingServiceLogMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
MichalPavlik marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Profiler;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -688,8 +689,9 @@ public void LogTargetFinished(BuildEventContext targetBuildEventContext, string
/// <param name="taskName">Task Name</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="projectFileOfTaskNode">Project file which contains the task</param>
/// <param name="taskAssemblyName">An assembly's unique identity where the task is implemented</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode)
public void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, string taskAssemblyName)
{
ErrorUtilities.VerifyThrow(taskBuildEventContext != null, "targetBuildEventContext is null");
if (!OnlyLogCriticalEvents)
Expand All @@ -699,7 +701,8 @@ public void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskN
helpKeyword: null,
projectFile,
projectFileOfTaskNode,
taskName);
taskName,
taskAssemblyName);
buildEvent.BuildEventContext = taskBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
Expand All @@ -714,9 +717,10 @@ public void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskN
/// <param name="projectFileOfTaskNode">Project file which contains the task</param>
/// <param name="line">The line number in the file where the task invocation is located.</param>
/// <param name="column">The column number in the file where the task invocation is located.</param>
/// <param name="taskAssemblyName">An assembly's unique identity where the task is implemented</param>
MichalPavlik marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>The build event context for the task.</returns>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, int line, int column)
public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, int line, int column, string taskAssemblyName)
{
ErrorUtilities.VerifyThrow(targetBuildEventContext != null, "targetBuildEventContext is null");
BuildEventContext taskBuildEventContext = new BuildEventContext(
Expand All @@ -734,7 +738,8 @@ public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventConte
helpKeyword: null,
projectFile,
projectFileOfTaskNode,
taskName);
taskName,
taskAssemblyName);
buildEvent.BuildEventContext = taskBuildEventContext;
buildEvent.LineNumber = line;
buildEvent.ColumnNumber = column;
Expand Down
5 changes: 3 additions & 2 deletions src/Build/BackEnd/Components/Logging/TargetLoggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -108,11 +109,11 @@ internal void LogTargetBatchFinished(string projectFullPath, bool success, IEnum
/// <summary>
/// Log that a task is about to start
/// </summary>
internal TaskLoggingContext LogTaskBatchStarted(string projectFullPath, ProjectTargetInstanceChild task)
internal TaskLoggingContext LogTaskBatchStarted(string projectFullPath, ProjectTargetInstanceChild task, string taskAssemblyName)
{
ErrorUtilities.VerifyThrow(IsValid, "Should be valid");

return new TaskLoggingContext(this, projectFullPath, task);
return new TaskLoggingContext(this, projectFullPath, task, taskAssemblyName);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Build/BackEnd/Components/Logging/TaskLoggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -34,7 +35,7 @@ internal class TaskLoggingContext : BuildLoggingContext
/// <summary>
/// Constructs a task logging context from a parent target context and a task node.
/// </summary>
internal TaskLoggingContext(TargetLoggingContext targetLoggingContext, string projectFullPath, ProjectTargetInstanceChild task)
internal TaskLoggingContext(TargetLoggingContext targetLoggingContext, string projectFullPath, ProjectTargetInstanceChild task, string taskAssemblyName)
: base(targetLoggingContext)
{
_targetLoggingContext = targetLoggingContext;
Expand Down Expand Up @@ -72,7 +73,8 @@ internal TaskLoggingContext(TargetLoggingContext targetLoggingContext, string pr
projectFullPath,
task.Location.File,
task.Location.Line,
task.Location.Column);
task.Location.Column,
taskAssemblyName);
this.IsValid = true;
}

Expand Down
12 changes: 7 additions & 5 deletions src/Build/BackEnd/Components/RequestBuilder/TaskBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal class TaskBuilder : ITaskBuilder, IBuildComponent
/// <summary>
/// The task execution host for in-proc tasks.
/// </summary>
private ITaskExecutionHost _taskExecutionHost;
private TaskExecutionHost _taskExecutionHost;

/// <summary>
/// The object used to synchronize access to the task execution host.
Expand Down Expand Up @@ -423,10 +423,12 @@ private async Task<WorkUnitResult> ExecuteBucket(TaskHost taskHost, ItemBucket b
{
// We need to find the task before logging the task started event so that the using task statement comes before the task started event
IDictionary<string, string> taskIdentityParameters = GatherTaskIdentityParameters(bucket.Expander);
TaskRequirements? requirements = _taskExecutionHost.FindTask(taskIdentityParameters);
(TaskRequirements? requirements, TaskFactoryWrapper taskFactoryWrapper) = _taskExecutionHost.FindTask(taskIdentityParameters);
string taskAssemblyName = taskFactoryWrapper?.TaskFactoryLoadedType?.LoadedAssemblyName?.Name;

if (requirements != null)
{
TaskLoggingContext taskLoggingContext = _targetLoggingContext.LogTaskBatchStarted(_projectFullPath, _targetChildInstance);
TaskLoggingContext taskLoggingContext = _targetLoggingContext.LogTaskBatchStarted(_projectFullPath, _targetChildInstance, taskAssemblyName);
MSBuildEventSource.Log.ExecuteTaskStart(_taskNode?.Name, taskLoggingContext.BuildEventContext.TaskId);
_buildRequestEntry.Request.CurrentTaskContext = taskLoggingContext.BuildEventContext;

Expand Down Expand Up @@ -734,7 +736,7 @@ private void UpdateContinueOnError(ItemBucket bucket, TaskHost taskHost)
/// <param name="bucket">The batching bucket</param>
/// <param name="howToExecuteTask">The task execution mode</param>
/// <returns>The result of running the task.</returns>
private async Task<WorkUnitResult> ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
private async Task<WorkUnitResult> ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
{
UpdateContinueOnError(bucket, taskHost);

Expand Down Expand Up @@ -1062,7 +1064,7 @@ private List<string> GetUndeclaredProjects(MSBuild msbuildTask)
/// <param name="howToExecuteTask">The task execution mode</param>
/// <param name="bucket">The bucket to which the task execution belongs.</param>
/// <returns>true, if successful</returns>
private bool GatherTaskOutputs(ITaskExecutionHost taskExecutionHost, TaskExecutionMode howToExecuteTask, ItemBucket bucket)
private bool GatherTaskOutputs(TaskExecutionHost taskExecutionHost, TaskExecutionMode howToExecuteTask, ItemBucket bucket)
{
bool gatheredTaskOutputsSuccessfully = true;

Expand Down
Loading