Skip to content

Commit

Permalink
Added task assembly location to TaskStartedEventArgs (#9948)
Browse files Browse the repository at this point in the history
* Added task assembly location to TaskStartedEventArgs
* Removed ITaskExecutionHost
* Emitting message when expected task assembly location does not match loaded assembly location
  • Loading branch information
MichalPavlik committed Apr 19, 2024
1 parent c1c863e commit 10db417
Show file tree
Hide file tree
Showing 30 changed files with 231 additions and 186 deletions.
15 changes: 9 additions & 6 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(taskBuildEventContext: null, "MyTask", "ProjectFile", "ProjectFileOfTask", taskAssemblyLocation: null);
});
}

Expand Down Expand Up @@ -1443,14 +1444,15 @@ private void TestProjectFinishedEvent(string projectFile, bool success)
private void TestTaskStartedEvent(string taskName, string projectFile, string projectFileOfTask)
{
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword("TaskStarted", taskName);
string taskAssemblyLocation = Assembly.GetExecutingAssembly().Location;

ProcessBuildEventHelper service = (ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask);
VerifyTaskStartedEvent(taskName, projectFile, projectFileOfTask, message, service);
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask, taskAssemblyLocation);
VerifyTaskStartedEvent(taskName, projectFile, projectFileOfTask, message, service, taskAssemblyLocation);

service.ResetProcessedBuildEvent();
service.OnlyLogCriticalEvents = true;
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask);
service.LogTaskStarted(s_buildEventContext, taskName, projectFile, projectFileOfTask, taskAssemblyLocation);
Assert.Null(service.ProcessedBuildEvent);
}

Expand Down Expand Up @@ -1631,15 +1633,16 @@ private void VerifyTaskFinishedEvent(string taskName, string projectFile, string
/// <param name="projectFileOfTask">ProjectFileOfTask to create the comparison event with.</param>
/// <param name="message">Message to create the comparison event with.</param>
/// <param name="service">LoggingService mock object which overrides ProcessBuildEvent and can provide a ProcessedBuildEvent (the event which would have been sent to the loggers)</param>
private void VerifyTaskStartedEvent(string taskName, string projectFile, string projectFileOfTask, string message, ProcessBuildEventHelper service)
private void VerifyTaskStartedEvent(string taskName, string projectFile, string projectFileOfTask, string message, ProcessBuildEventHelper service, string taskAssemblyLocation)
{
TaskStartedEventArgs taskEvent = new TaskStartedEventArgs(
message,
null, // no help keyword
projectFile,
projectFileOfTask,
taskName,
service.ProcessedBuildEvent.Timestamp);
service.ProcessedBuildEvent.Timestamp,
taskAssemblyLocation);
taskEvent.BuildEventContext = s_buildEventContext;
Assert.True(((TaskStartedEventArgs)service.ProcessedBuildEvent).IsEquivalent(taskEvent));
}
Expand Down
6 changes: 4 additions & 2 deletions src/Build.UnitTests/BackEnd/MockLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,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="taskAssemblyLocation">>The location of the assembly containing the implementation of the task.</param>
public void LogTaskStarted(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, string taskAssemblyLocation)
{
}

Expand All @@ -584,8 +585,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="taskAssemblyLocation">>The location of the assembly containing the implementation of the task.</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 taskAssemblyLocation)
{
return new BuildEventContext(0, 0, 0, 0);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Build.UnitTests/BackEnd/TaskExecutionHost_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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 @@ -1245,7 +1245,7 @@ private void InitializeHost(bool throwOnExecute)
#else
AssemblyLoadInfo loadInfo = AssemblyLoadInfo.Create(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory).GetTypeInfo().FullName, null);
#endif
LoadedType loadedType = new LoadedType(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory), loadInfo, typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory).GetTypeInfo().Assembly, typeof(ITaskItem));
LoadedType loadedType = new LoadedType(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory), loadInfo, typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory).Assembly, typeof(ITaskItem));

TaskBuilderTestTask.TaskBuilderTestTaskFactory taskFactory = new TaskBuilderTestTask.TaskBuilderTestTaskFactory();
taskFactory.ThrowOnExecute = throwOnExecute;
Expand All @@ -1266,7 +1266,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).Assembly.GetName().FullName);

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,
"TaskAssemblyLocation");
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.TaskAssemblyLocation);
}

[Fact]
Expand Down
6 changes: 4 additions & 2 deletions src/Build/BackEnd/Components/Logging/ILoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,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="taskAssemblyLocation">>The location of the assembly containing the implementation of the task.</param>
void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, string taskAssemblyLocation);

/// <summary>
/// Log that a task is about to start
Expand All @@ -581,8 +582,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="taskAssemblyLocation">>The location of the assembly containing the implementation of the task.</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 taskAssemblyLocation);

/// <summary>
/// Log that a task has just completed
Expand Down
12 changes: 8 additions & 4 deletions src/Build/BackEnd/Components/Logging/LoggingServiceLogMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,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="taskAssemblyLocation">>The location of the assembly containing the implementation of the task.</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 taskAssemblyLocation)
{
ErrorUtilities.VerifyThrow(taskBuildEventContext != null, "targetBuildEventContext is null");
if (!OnlyLogCriticalEvents)
Expand All @@ -699,7 +700,8 @@ public void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskN
helpKeyword: null,
projectFile,
projectFileOfTaskNode,
taskName);
taskName,
taskAssemblyLocation);
buildEvent.BuildEventContext = taskBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
Expand All @@ -714,9 +716,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="taskAssemblyLocation">>The location of the assembly containing the implementation of the task.</param>
/// <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 taskAssemblyLocation)
{
ErrorUtilities.VerifyThrow(targetBuildEventContext != null, "targetBuildEventContext is null");
BuildEventContext taskBuildEventContext = new BuildEventContext(
Expand All @@ -734,7 +737,8 @@ public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventConte
helpKeyword: null,
projectFile,
projectFileOfTaskNode,
taskName);
taskName,
taskAssemblyLocation);
buildEvent.BuildEventContext = taskBuildEventContext;
buildEvent.LineNumber = line;
buildEvent.ColumnNumber = column;
Expand Down
4 changes: 2 additions & 2 deletions src/Build/BackEnd/Components/Logging/TargetLoggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,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 taskAssemblyLocation)
{
ErrorUtilities.VerifyThrow(IsValid, "Should be valid");

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

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Build/BackEnd/Components/Logging/TaskLoggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 taskAssemblyLocation)
: base(targetLoggingContext)
{
_targetLoggingContext = targetLoggingContext;
Expand Down Expand Up @@ -72,7 +72,8 @@ internal TaskLoggingContext(TargetLoggingContext targetLoggingContext, string pr
projectFullPath,
task.Location.File,
task.Location.Line,
task.Location.Column);
task.Location.Column,
taskAssemblyLocation);
this.IsValid = true;
}

Expand Down
Loading

0 comments on commit 10db417

Please sign in to comment.