Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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
9 changes: 5 additions & 4 deletions src/Build.UnitTests/BackEnd/AssemblyTaskFactory_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Build.Construction;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using Xunit;
using Shouldly;

namespace Microsoft.Build.UnitTests.BackEnd
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public void NullLoadInfo()
{
Assert.Throws<ArgumentNullException>(() =>
{
AssemblyTaskFactory taskFactory = new AssemblyTaskFactory();
AssemblyTaskFactory taskFactory = new();
taskFactory.InitializeFactory(null, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, null, false, null, ElementLocation.Create("NONE"), String.Empty);
}
);
Expand Down Expand Up @@ -255,8 +256,8 @@ public void VerifyGoodTaskInstantiation()
new AppDomainSetup(),
#endif
false);
Assert.NotNull(createdTask);
Assert.False(createdTask is TaskHostTask);
createdTask.ShouldNotBeNull();
createdTask.ShouldNotBeOfType<TaskHostTask>();
}
finally
{
Expand Down Expand Up @@ -699,7 +700,7 @@ private void SetupTaskFactory(IDictionary<string, string> factoryParameters, boo
#else
_loadInfo = AssemblyLoadInfo.Create(typeof(TaskToTestFactories).GetTypeInfo().Assembly.FullName, null);
#endif
_loadedType = _taskFactory.InitializeFactory(_loadInfo, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, factoryParameters, explicitlyLaunchTaskHost, null, ElementLocation.Create("NONE"), String.Empty);
_loadedType = _taskFactory.InitializeFactory(_loadInfo, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, factoryParameters, explicitlyLaunchTaskHost, null, ElementLocation.Create("NONE"), String.Empty).LoadedType;
Assert.True(_loadedType.Assembly.Equals(_loadInfo)); // "Expected the AssemblyLoadInfo to be equal"
}

Expand Down
4 changes: 2 additions & 2 deletions src/Build.UnitTests/BackEnd/BuildManager_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,11 +1679,11 @@ public void CancelledBuildWithDelay40()
[Fact]
public void CancelledBuildInTaskHostWithDelay40()
{
string contents = CleanupFileContents(@"
string contents = CleanupFileContents(@$"
<Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
<UsingTask TaskName='Microsoft.Build.Tasks.Exec' AssemblyName='Microsoft.Build.Tasks.Core, Version=msbuildassemblyversion, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' TaskFactory='TaskHostFactory' />
<Target Name='test'>
<Exec Command='" + Helpers.GetSleepCommand(TimeSpan.FromSeconds(10)) + @"'/>
<Exec Command='{Helpers.GetSleepCommand(TimeSpan.FromSeconds(10))}'/>
<Message Text='[errormessage]'/>
</Target>
</Project>
Expand Down
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/TaskExecutionHost_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ private void InitializeHost(bool throwOnExecute)
TaskBuilderTestTask.TaskBuilderTestTaskFactory taskFactory = new TaskBuilderTestTask.TaskBuilderTestTaskFactory();
taskFactory.ThrowOnExecute = throwOnExecute;
string taskName = "TaskBuilderTestTask";
(_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper = new TaskFactoryWrapper(taskFactory, loadedType, taskName, null);
(_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper = new TaskFactoryWrapper(taskFactory, new TypeInformation(loadedType), loadInfo, taskName, null);
_host.InitializeForTask
(
this,
Expand Down
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/TaskHostFactory_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TaskNodesDieAfterBuild()
</Target>
</Project>";
TransientTestFile project = env.CreateFile("testProject.csproj", pidTaskProject);
ProjectInstance projectInstance = new ProjectInstance(project.Path);
ProjectInstance projectInstance = new(project.Path);
projectInstance.Build().ShouldBeTrue();
string processId = projectInstance.GetPropertyValue("PID");
string.IsNullOrEmpty(processId).ShouldBeFalse();
Expand Down
8 changes: 4 additions & 4 deletions src/Build.UnitTests/EscapingInProjects_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Xunit;
using Xunit.Abstractions;
using Microsoft.Build.Shared;
using Shouldly;

namespace Microsoft.Build.UnitTests.EscapingInProjects_Tests
{
Expand Down Expand Up @@ -715,14 +716,14 @@ public void EscapedWildcardsShouldNotBeExpanded()
[Trait("Category", "mono-osx-failing")]
public void EscapedWildcardsShouldNotBeExpanded_InTaskHost()
{
MockLogger logger = new MockLogger();
MockLogger logger = new();

try
{
// Populate the project directory with three physical files on disk -- a.weirdo, b.weirdo, c.weirdo.
EscapingInProjectsHelper.CreateThreeWeirdoFiles();
Project project = ObjectModelHelpers.CreateInMemoryProject(@"
<Project ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`http://schemas.microsoft.com/developer/msbuild/2003`>
<Project>
<UsingTask TaskName=`Message` AssemblyFile=`$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll` TaskFactory=`TaskHostFactory` />

<Target Name=`t`>
Expand All @@ -734,8 +735,7 @@ public void EscapedWildcardsShouldNotBeExpanded_InTaskHost()
</Project>
");

bool success = project.Build(logger);
Assert.True(success); // "Build failed. See test output (Attachments in Azure Pipelines) for details"
project.Build(logger).ShouldBeTrue(); // "Build failed. See test output (Attachments in Azure Pipelines) for details"
logger.AssertLogContains("[*]");
}
finally
Expand Down
48 changes: 17 additions & 31 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ void ITaskExecutionHost.InitializeForTask(IBuildEngine2 buildEngine, TargetLoggi

TaskRequirements requirements = TaskRequirements.None;

if (_taskFactoryWrapper.TaskFactoryLoadedType.HasSTAThreadAttribute())
if (_taskFactoryWrapper.TaskFactoryTypeInformation.HasSTAThreadAttribute)
{
requirements |= TaskRequirements.RequireSTAThread;
}

if (_taskFactoryWrapper.TaskFactoryLoadedType.HasLoadInSeparateAppDomainAttribute())
if (_taskFactoryWrapper.TaskFactoryTypeInformation.HasLoadInSeparateAppDomainAttribute)
{
requirements |= TaskRequirements.RequireSeparateAppDomain;

Expand Down Expand Up @@ -297,7 +297,7 @@ bool ITaskExecutionHost.InitializeForBatch(TaskLoggingContext loggingContext, It
if (_resolver == null)
{
_resolver = new TaskEngineAssemblyResolver();
_resolver.Initialize(_taskFactoryWrapper.TaskFactoryLoadedType.Assembly.AssemblyFile);
_resolver.Initialize(_taskFactoryWrapper.TaskFactoryAssemblyLoadInfo.AssemblyFile);
_resolver.InstallHandler();
}
#endif
Expand Down Expand Up @@ -916,15 +916,16 @@ private TaskFactoryWrapper FindTaskInRegistry(IDictionary<string, string> taskId
}
}

string taskFactoryFullName = returnClass.TaskFactory is AssemblyTaskFactory atf ? atf.TaskName : returnClass.TaskFactory.TaskType.FullName;
// Map to an intrinsic task, if necessary.
if (String.Equals(returnClass.TaskFactory.TaskType.FullName, "Microsoft.Build.Tasks.MSBuild", StringComparison.OrdinalIgnoreCase))
if (String.Equals(taskFactoryFullName, "Microsoft.Build.Tasks.MSBuild", StringComparison.OrdinalIgnoreCase))
{
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(MSBuild)), new LoadedType(typeof(MSBuild), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null)), _taskName, null);
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(MSBuild)), new TypeInformation(new LoadedType(typeof(MSBuild), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null))), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null), _taskName, null);
_intrinsicTasks[_taskName] = returnClass;
}
else if (String.Equals(returnClass.TaskFactory.TaskType.FullName, "Microsoft.Build.Tasks.CallTarget", StringComparison.OrdinalIgnoreCase))
else if (String.Equals(taskFactoryFullName, "Microsoft.Build.Tasks.CallTarget", StringComparison.OrdinalIgnoreCase))
{
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(CallTarget)), new LoadedType(typeof(CallTarget), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null)), _taskName, null);
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(CallTarget)), new TypeInformation(new LoadedType(typeof(CallTarget), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null))), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null), _taskName, null);
_intrinsicTasks[_taskName] = returnClass;
}
}
Expand Down Expand Up @@ -1095,30 +1096,15 @@ out parameterSet
else
{
// flag an error if we find a parameter that has no .NET property equivalent
if (_taskFactoryWrapper.TaskFactoryLoadedType.LoadedAssembly is null)
{
_taskLoggingContext.LogError
(
new BuildEventFileInfo( parameterLocation ),
"UnexpectedTaskAttribute",
parameterName,
_taskName,
_taskFactoryWrapper.TaskFactoryLoadedType.Type.Assembly.FullName,
_taskFactoryWrapper.TaskFactoryLoadedType.Type.Assembly.Location
);
}
else
{
_taskLoggingContext.LogError
(
new BuildEventFileInfo( parameterLocation ),
"UnexpectedTaskAttribute",
parameterName,
_taskName,
_taskFactoryWrapper.TaskFactoryLoadedType.LoadedAssembly.FullName,
_taskFactoryWrapper.TaskFactoryLoadedType.LoadedAssembly.Location
);
}
_taskLoggingContext.LogError
(
new BuildEventFileInfo( parameterLocation ),
"UnexpectedTaskAttribute",
parameterName,
_taskName,
_taskFactoryWrapper.TaskFactoryAssemblyLoadInfo.AssemblyName,
_taskFactoryWrapper.TaskFactoryAssemblyLoadInfo.AssemblyLocation
);
}
}
catch (AmbiguousMatchException)
Expand Down
28 changes: 24 additions & 4 deletions src/Build/Instance/ReflectableTaskPropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ internal class ReflectableTaskPropertyInfo : TaskPropertyInfo
/// <summary>
/// The type of the generated tasks.
/// </summary>
private Type _taskType;
private readonly Func<string, BindingFlags, PropertyInfo> getProperty;
private readonly string taskName;

/// <summary>
/// Initializes a new instance of the <see cref="ReflectableTaskPropertyInfo"/> class.
Expand All @@ -33,7 +34,16 @@ internal ReflectableTaskPropertyInfo(TaskPropertyInfo taskPropertyInfo, Type tas
: base(taskPropertyInfo.Name, taskPropertyInfo.PropertyType, taskPropertyInfo.Output, taskPropertyInfo.Required)
{
ErrorUtilities.VerifyThrowArgumentNull(taskType, nameof(taskType));
_taskType = taskType;
getProperty = taskType.GetProperty;
taskName = taskType.FullName;
}

internal ReflectableTaskPropertyInfo(TaskPropertyInfo taskPropertyInfo, TypeInformation typeInformation)
: base(taskPropertyInfo.Name, taskPropertyInfo.PropertyType, taskPropertyInfo.Output, taskPropertyInfo.Required)
{
ErrorUtilities.VerifyThrowArgumentNull(typeInformation, nameof(typeInformation));
getProperty = typeInformation.GetProperty;
taskName = typeInformation.TypeName;
}

/// <summary>
Expand All @@ -50,6 +60,16 @@ internal ReflectableTaskPropertyInfo(PropertyInfo propertyInfo)
_propertyInfo = propertyInfo;
}

internal ReflectableTaskPropertyInfo(TypeInformationPropertyInfo propertyInfo) :
base(
propertyInfo.Name,
propertyInfo.PropertyType,
propertyInfo.OutputAttribute,
propertyInfo.RequiredAttribute)
{

}

/// <summary>
/// Gets or sets the reflection-produced PropertyInfo.
/// </summary>
Expand All @@ -59,8 +79,8 @@ internal PropertyInfo Reflection
{
if (_propertyInfo == null)
{
_propertyInfo = _taskType.GetProperty(Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
ErrorUtilities.VerifyThrow(_propertyInfo != null, "Could not find property {0} on type {1} that the task factory indicated should exist.", Name, _taskType.FullName);
_propertyInfo = getProperty(Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
ErrorUtilities.VerifyThrow(_propertyInfo != null, "Could not find property {0} on type {1} that the task factory indicated should exist.", Name, taskName);
}

return _propertyInfo;
Expand Down
Loading