Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 66 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskRouter_IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,72 @@ public void ExplicitTaskHostFactory_OverridesRoutingLogic()
logger.FullLog.ShouldContain("TaskWithAttribute executed");
}

/// <summary>
/// Regression test: a task routed to a TaskHost must not log <c>TaskAssemblyLocationMismatch</c>.
/// <c>TaskHostTask</c> is only an in-proc proxy for a task that is loaded in a separate process, so
/// its own assembly location (Microsoft.Build.dll) can never match the resolved task assembly path
/// and the diagnostic carries no information. In multi-threaded mode this is the common path, so a
/// stale check produced one spurious message per task execution.
/// The registered path deliberately contains a redundant "." segment so that it is not byte-identical
/// to <c>Assembly.Location</c> - the same normalization difference real builds hit.
/// </summary>
[Theory]
[InlineData(true, "")] // Routed to the sidecar TaskHost because multi-threaded mode is on.
[InlineData(false, @" TaskFactory=""TaskHostFactory""")] // Routed to a TaskHost by explicit request.
public void TaskHostRoutedTask_DoesNotLogAssemblyLocationMismatch(bool multiThreaded, string taskFactoryAttribute)
{
// Arrange
string assemblyPath = Assembly.GetExecutingAssembly().Location;
string nonNormalizedAssemblyPath = Path.Combine(Path.GetDirectoryName(assemblyPath), ".", Path.GetFileName(assemblyPath));

string projectContent = $@"
<Project>
<UsingTask TaskName=""NonEnlightenedTestTask"" AssemblyFile=""{nonNormalizedAssemblyPath}""{taskFactoryAttribute} />

<Target Name=""TestTarget"">
<NonEnlightenedTestTask />
</Target>
</Project>";

string projectFile = Path.Combine(_testProjectsDir, $"NoAssemblyLocationMismatch_{multiThreaded}.proj");
File.WriteAllText(projectFile, projectContent);

var logger = new MockLogger(_output);
var buildParameters = new BuildParameters
{
MultiThreaded = multiThreaded,
Loggers = new[] { logger },
Comment thread
JanProvaznik marked this conversation as resolved.
Resolved
DisableInProcNode = false,
EnableNodeReuse = false
};

var buildRequestData = new BuildRequestData(
projectFile,
new Dictionary<string, string>(),
null,
new[] { "TestTarget" },
null);

// Act
var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData);

// Assert
result.OverallResult.ShouldBe(BuildResultCode.Success);
TaskRouterTestHelper.AssertTaskUsedTaskHost(logger, "NonEnlightenedTestTask");
logger.FullLog.ShouldContain("NonEnlightenedTask executed");

// Assert on the localized text preceding the first format argument so the test is locale-safe
// and does not depend on the exact paths that would have been reported.
const string ArgumentSentinel = "<<arg>>";
string mismatchMessage = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(
"TaskAssemblyLocationMismatch",
ArgumentSentinel,
ArgumentSentinel);
string mismatchMessagePrefix = mismatchMessage.Substring(0, mismatchMessage.IndexOf(ArgumentSentinel, StringComparison.Ordinal));

logger.FullLog.ShouldNotContain(mismatchMessagePrefix);
}

private string CreateTestProject(string taskName, string taskClass)
{
return $@"
Expand Down
16 changes: 7 additions & 9 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,13 @@ public bool InitializeForBatch(TaskLoggingContext loggingContext, ItemBucket bat
// meaningless) in a single-file/Native AOT host - and a registered task is already the loaded
// type. On .NET, guard the read on dynamic-code support so ILC dead-strips it (and its IL3000)
// under Native AOT while the JIT keeps the diagnostic; .NET Framework (no AOT) always runs it.
// A TaskHostTask is only an in-proc proxy - the task assembly it stands for is loaded in the task
// host process, so the proxy's own location (always Microsoft.Build.dll) says nothing about where
// the task came from and comparing it would report a mismatch for every out-of-proc task.
#if NET
if (RuntimeFeature.IsDynamicCodeSupported)
if (RuntimeFeature.IsDynamicCodeSupported && TaskInstance is not TaskHostTask)
#else
if (TaskInstance is not TaskHostTask)
#endif
Comment thread
JanProvaznik marked this conversation as resolved.
Resolved
{
// When MSBuild loads a task assembly, it uses Assembly.LoadFrom() with a specific path, but
Expand All @@ -530,10 +535,7 @@ public bool InitializeForBatch(TaskLoggingContext loggingContext, ItemBucket bat
string realTaskAssemblyLocation = TaskInstance.GetType().Assembly.Location;
if (!string.IsNullOrWhiteSpace(realTaskAssemblyLocation) && realTaskAssemblyLocation != _taskFactoryWrapper.TaskFactoryLoadedType.Path)
{
if (!IsTaskAssemblyMatchFactoryType())
{
_taskLoggingContext.LogComment(MessageImportance.Normal, "TaskAssemblyLocationMismatch", realTaskAssemblyLocation, _taskFactoryWrapper.TaskFactoryLoadedType.Path);
}
_taskLoggingContext.LogComment(MessageImportance.Normal, "TaskAssemblyLocationMismatch", realTaskAssemblyLocation, _taskFactoryWrapper.TaskFactoryLoadedType.Path);
}
}

Expand All @@ -546,10 +548,6 @@ public bool InitializeForBatch(TaskLoggingContext loggingContext, ItemBucket bat
}

return true;

// Function to validate that if this is a TaskHostTask, the assembly it loaded is the same one we found in the registry.
bool IsTaskAssemblyMatchFactoryType() => TaskInstance is not TaskHostTask tht
|| tht.LoadedTaskAssemblyInfo.AssemblyLocation == _taskFactoryWrapper.TaskFactoryLoadedType.Path;
}

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions src/Build/Instance/TaskFactories/TaskHostTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,6 @@ public ITaskHost HostObject
}
}

/// <summary>
/// Gets information about the assembly from which the task type was loaded.
/// </summary>
public AssemblyLoadInfo LoadedTaskAssemblyInfo => _taskType.Assembly;

/// <summary>
/// Sets the requested task parameter to the requested value.
/// </summary>
Expand Down
Loading